简体   繁体   English

CSS3宽高比

[英]CSS3 width and height ratio

So, I've got confused over this and then realized I actually don't have an idea what should I type as a query in Google so I'd get the answer: 因此,我对此感到困惑,然后意识到我实际上不知道应该在Google查询中键入什么内容,以便得到答案:

If I wanted to define height and width in CSS3 using percentages, for example: 如果我想使用百分比在CSS3中定义高度和宽度,例如:

#wrap{
  width: 100%;
  height: 250%;
}

Is height calculated according to the full height of the view port, or is it calculated according to defined width (as if - the set width becomes a base value for everything)? 高度是根据视口的整个高度计算的,还是根据定义的宽度计算的(好像-设置的宽度成为所有内容的基础值)? For example: 例如:

width = 1000px
height = 1200px

Is then: 然后是:

width = 100% <=> width = 1000px
height = 250% <=> height = 2.5 * 1200px = 3000px

or is: 或为:

width = 100% <=> width = 1000px
height = 250% <=> height = 2.5 * width = 2.5 * 1000px = 2500px

?

Also, does the same rule, whichever it may be, apply to all other elements as well? 而且,相同的规则(无论可能是什么)也适用于所有其他元素吗? Do I calculate the height according to the height of the parent and width according to the width of the parent, or does width become THE 100% which everything relates to? 我是根据父对象的高度来计算高度,还是根据父对象的宽度来计算宽度,还是宽度变成所有相关的100%?

Height is not necessarily calculated to the full height of the view port, it's calculated by the defined witdth. 高度不一定是视口的整个高度,而是由定义的witdth计算的。 If you have #wrap nested inside #container and defined it as follows: 如果您将#wrap嵌套在#container并按以下方式定义:

#container{
height: 1000px;
width: 200px;
}

#wrap{
height:100%;
width: 50%;
}

Then by declaring the wrap height as 100% that's 100% of my 1000px (which is 1000px), however the width of 50% is 50% of 200px (100px). 然后,通过将自动换行高度声明为100%,即是我的1000px(即1000px)的100%,但是50%的宽度是200px(100px)的50%。 Though if it wasn't nested then it is still calculated by the "defined width" which is set to by default the whole browser window, unless specified otherwise. 尽管如果未嵌套,则除非另有说明,否则它仍由默认情况下设置为整个浏览器窗口的“定义的宽度”来计算。

Yes, the same rule applies to all other elements and is calculated to the height and width of the parent. 是的,相同规则适用于所有其他元素,并根据父元素的高度和宽度进行计算。

For viewports and the like and sizing it to the full size of the browser window I recommend looking at this post by James on Make div 100% height of browser window . 对于视口等,并将其调整为浏览器窗口的完整大小,我建议您查看James的这篇文章, 将div设为浏览器窗口的高度为100% He goes into great depth about how viewports work and their advantages over the normal 100% 他深入介绍了视口如何工作以及它们相对于正常100%的优势

for instance if you have this : 例如,如果您有:

<div id="outer">

<div id="inner"></div>

</div>

and this css 和这个CSS

#outer {
width: 1000px;
height: 1000px;
}

#inner {
width: 50%;  /* is 500 */
height: 200%; /* is 2000 */
}

here's a scaled fiddle: http://jsfiddle.net/q6ux91x9/ 这是一个缩放的小提琴: http : //jsfiddle.net/q6ux91x9/

The percentage system works in relation to the containing element or document/window if no containing element exists. 如果不存在包含元素,则百分比系统相对于包含元素或文档/窗口起作用。

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

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