[英]Keep aspect ratio of div using css [duplicate]
这个问题已经在这里有了答案:
我正在尝试创建一个div
并使用CSS将其长宽比保持在16:9。 我进行了一次Google搜索,并很快发现了这一点: 如何保持纵横比 。 所以我尝试自己做,但是遇到了问题。 当我在其中添加文字时,该比例会发生变化,不再是16:9的比例。
当您删除所有li's
,则该比率是正确的。 即使在其中添加文本,如何保持长宽比?
代码段:
div { width: 70%; } ul { padding-bottom: 56.25%; background: green; }
<div> <ul> <li>1</li> <li>2</li> <li>3</li> <li>4</li> </ul> </div>
top
, bottom
, left
和right
设置为0。该子级始终具有外部div相同的纵横比,无论其内容是什么。 overflow:hidden
。 HTML:
<div class="outer">
<ul class="inner">
<li>List items go here</li>
<li>...</li>
</ul>
</div>
CSS:
.outer {
padding-bottom: 56.25%; // 9/16
position: relative; // or absolute or fixed, but not static
}
.inner {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
overflow: hidden; // or scroll, auto
}
您需要将ul
完全定位在绿色div
以免影响填充。 这是您需要的CSS:
div {
width: 70%;
position: relative;
}
ul {
padding-bottom: 56.25%;
background: green;
position: absolute;
top: 0; bottom: 0; left: 0; right: 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.