gpt4 book ai didi

html - 在具有滚动的 div 内垂直对齐的 div

转载 作者:行者123 更新时间:2023-11-28 03:19:11 25 4
gpt4 key购买 nike

看看这个 fiddle

HTML:

<div id="outer">
<div id="inner">
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
<li>9</li>
<li>10</li>
</ul>
</div>
</div>
<button onClick="toggleOuterHeight();">toggle outer div height</button>

JS:

toggleOuterHeight = function() {
if ($('#outer').css('height') == '400px') {
$('#outer').css('height','200px');
} else {
$('#outer').css('height','400px');
}
}

CSS:

#outer {
background-color: #1abc9c;
height: 400px;
position: relative;
overflow: auto;
}

#inner {
background-color: #16a085;
height: 250px;
width: 250px;
position: absolute;
margin: auto;
top: 0;
left: 0;
right: 0;
bottom: 0;
}

#inner ul {
margin: 0;
}

#inner ul li {
line-height: 25px;
}

我有一个 div(内部)在另一个 div(外部)内垂直对齐...它就像一个魅力!

如果外层 div 小于内层 div,则必须滚动。您可以在示例底部看到这个点击按钮。

存在问题:滚动隐藏了内部 div 的顶部。您无法滚动以显示第一行!!!

我更改了 css 以使 div 居中 using translate , 没有运气!

有什么建议吗?

最佳答案

您可以避免对垂直对齐使用绝对定位,这会将元素从正常流中移除,并使用 this approach 1 改为:

EXAMPLE HERE

#outer { text-align: center; }

#outer:after {
content: "";
display: inline-block;
height: 100%;
vertical-align: middle;
}

#inner {
background-color: #16a085;
height: 250px;
width: 250px;
display: inline-block;
vertical-align: middle;
}

1。您可以引用该答案的“垂直对齐”部分,以实现盒子的水平和垂直对齐。

关于html - 在具有滚动的 div 内垂直对齐的 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25456474/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com