Lar*_*rry 6 css css3 viewport-units
有时我会使用类似于此方法的方法创建一个正方形(或任何矩形,实际上),它将遵循任何大小的比例.
我想要的是:
提出的解决方案
max-width: 90vhCSS
.square {
position: relative;
height: 0;
padding-bottom: 100%;
overflow: hidden;
}
.square-inner {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
.mw { max-width: 90vh;} /* solution, but makes things break */
Run Code Online (Sandbox Code Playgroud)
HTML
<div class="square mw">
<div class="square-inner"></div>
</div>
Run Code Online (Sandbox Code Playgroud)
应该怎么做
实际发生了什么:
vh值约束vh规范说相对值是从'包含块'计算出来的,对我而言似乎应该是容器的当前宽度.
浏览器行为:
我是否错误地解释了规范,或者这是浏览器供应商实施的可能错误?
问题在于使用 % 和 vh 中的长度。
尝试这个:
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
font-family: sans-serif;
font-weight: 100;
}
.neat {
width: 50%;
max-width: 600px;
min-width: 320px;
margin: 0 auto;
}
.col {
float: left;
padding: 2rem;
width: 90vh; /*** Important bit changed width:50%; to width:90vh; ***/
max-width: 50%; /*** Important bit added max-width:50%; ***/
}
.square {
position: relative;
height: 0;
padding-bottom: 100%;
overflow: hidden;
}
.square-inner {
position: absolute;
background-color: #333333;
color: white;
top: 0;
bottom: 0;
left: 0;
right: 0;
padding: 1.5rem;
}
.mw {
max-width: 90vh;
}
Run Code Online (Sandbox Code Playgroud)