[英]How can I assign a height to an element, such that the height is equal to the height of the screen?
[英]How can I set the width of an element in CSS to be equal to 10% of the height?
我希望我的元素成为正方形。 我希望此元素正方形的边长为屏幕/窗口高度的10%。 但是,我不知道该怎么做。 这是我的代码:
.Element {
width: /*How can I make the width equal to: [height: 10%]?*/;
height: 10%;
/*I wanted the element to be a square with a side length of [height: 10%].*/
}
您可以使用视口百分比长度来获得所需的效果。
vh
单位长度基于视口/屏幕高度的1%,我认为这是您拥有的视口/屏幕高度的1%。
或者, vw
单位长度基于视口/屏幕宽度的1%。
您可以选择任何一个, width
和height
都使用相同的长度单位。
最后,您还可以尝试使用vb
和vi
长度单位。
请参阅: https : //developer.mozilla.org/en-US/docs/Web/CSS/length
.makeSquare { background-color: yellow; width: 10vh; height: 10vh; }
<div class="makeSquare"></div> <p>This is a square.</p>
这应该工作
.Element {
width: 20vw;
height: 20vw;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.