【发布时间】:2017-07-10 13:30:16
【问题描述】:
我希望红色背景的 div 根据浏览器宽度调整大小,同时保持其纵横比为 16:9 但不超过 300px 的高度。我还希望 div 中写入的文本居中对齐;垂直和水平。通过下面的 CSS 代码,div 正在根据纵横比调整大小,并且文本是水平对齐的。但是 div 并没有将其高度限制为 300px,并且文本没有垂直对齐。
.container {
background-color: red;
position: relative;
width: 100%;
max-height: 300px;
padding-top: 56.25%;
/* 16:9 Aspect Ratio */
display: table;
}
.text {
display: table-cell;
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
text-align: center;
vertical-align: middle;
font-size: 20px;
color: white;
}
<h2>Maintain Aspect Ratio 16:9</h2>
<div class="container">
<div class="text">This is some text...</div>
</div>
【问题讨论】:
-
做了同样的事情。纵横比保持不变,但除法不遵循最大高度。另外,我想将文本垂直居中对齐
标签: html css twitter-bootstrap vertical-alignment aspect-ratio