我有以下设置:
#container {
width: 100vw;
height: calc(100vw / 1.77);
display: block;
background-color: black;
}
<div id="container">
</div>
我想始终保持16/9的长宽比。
但是它不起作用!我怎么了
答案 0 :(得分:4)
#container {
display: block;
width: 100vw;
max-width: 177.78vh;
/* 16/9 = 1.778 */
height: 56.25vw;
/* height:width ratio = 9/16 = .5625 */
max-height: 100vh;
background-color: black;
}
<div id="container">
</div>
答案 1 :(得分:0)
这是一个Sass mixin,可以简化数学运算。
@mixin aspect-ratio($width, $height) {
position: relative;
&:before {
display: block;
content: "";
width: 100%;
padding-top: ($height / $width) * 100%;
}
> .content {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
}
mixin假定您将在初始块内嵌套具有内容类的元素。像这样:
<div class="sixteen-nine">
<div class="content">
insert content here
this will maintain a 16:9 aspect ratio
</div>
</div>
使用mixin很简单:
.sixteen-nine {
@include aspect-ratio(16, 9);
}
结果:
.sixteen-nine {
position: relative;
}
.sixteen-nine:before {
display: block;
content: "";
width: 100%;
padding-top: 56.25%;
}
.sixteen-nine > .content {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
}