简体   繁体   English

CSS宽度100%高度

[英]CSS 100% height on width

Is it even possible to make my width 100% of height using only CSS? 是否甚至可以仅使用CSS使我的宽度达到100%的高度? Without using Javascript or the like. 不使用Javascript等。

{
    width: /*100% of height*/
}

There are new css units for this 有新的css单位

width: 100vh;

This means that the width of the element will be 100% of the viewport height. 这意味着元素的宽度将是视口高度的100%。

CSS3 has some new values for sizing things relative to the current viewport size: vw, vh, and vmin. CSS3有一些新值可用于调整与当前视口大小相关的内容:vw,vh和vmin。 One unit on any of the three values is 1% of the viewport axis. 三个值中的任何一个上的一个单位是视口轴的1%。 "Viewport" == browser window size == window object. “视口”==浏览器窗口大小==窗口对象。 If the viewport is 40cm wide, 1vw == 0.4cm. 如果视口宽40厘米,1vw == 0.4厘米。

1vw = 1% of viewport width 1vh = 1% of viewport height 1vmin = 1vw or 1vh, whichever is smaller 1vmax = 1vw or 1vh, whichever is larger ( css-tricks post ) 1vw =视口宽度的1%1vh =视口高度的1%1vmin = 1vw或1vh,取较小的1vmax = 1vw或1vh,取较大者( css-tricks post

Reference: https://developer.mozilla.org/en-US/docs/Web/CSS/length 参考: https//developer.mozilla.org/en-US/docs/Web/CSS/length

PS: Support for these new properties is actually quite good in modern browsers. PS:在现代浏览器中,对这些新属性的支持实际上相当不错。 See here 看这里

This question Height equal to dynamic width (CSS fluid layout) seems to have a good CSS-only answer by Nathan Ryan. 这个问题高度等于动态宽度(CSS流体布局)似乎有一个很好的CSS答案由Nathan Ryan。 It's also worth reading the comments below his answer for additional explanation. 同样值得阅读下面的评论,以获得更多解释。 I hope this helps. 我希望这有帮助。

If you find the solution too complex, it might be worth giving more context to what you are trying to achieve in case someone has a good, alternative solution. 如果您发现解决方案过于复杂,那么在有人拥有良好的替代解决方案的情况下,可能值得为您尝试实现的目标提供更多背景信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM