简体   繁体   English

为什么高度= 100%不起作用?

[英]Why height=100% doesn't work?

I use a third-party component that occupies all the available space, ie width=100% and height=100% . 我使用占用所有可用空间的第三方组件,即width=100%height=100% I don't have control over it. 我没有控制权。

I'm trying to fit it in the following layout, but its height=100% doesn't work (I expect the third-party component to occupy all the green space). 我正在尝试将其调整为以下布局,但height=100%不起作用(我希望第三方组件占据所有绿色空间)。

在此处输入图片说明

Why? 为什么? How would you fix that? 您将如何解决?

 .container { display: flex; flex-direction: column; width: 200px; height: 100px; } .header { display: flex; background-color: rgba(255, 0, 0, 0.5); } .content { flex-grow: 1; background-color: rgba(0, 255, 0, 0.5); } .third-party-component { height: 100%; width: 100%; background-color: rgba(0, 0, 255, 0.5); } 
 <div class="container"> <div class="header">Header</div> <div class="content"> <div class="third-party-component"> Third party component </div> </div> </div> 

In general, for an element using percent on height to pick up its parent's height, the parent need a height other than auto or being positioned absolute , or the height will be computed as auto . 通常,对于使用height百分比来获取其父级高度的元素,父级需要的高度不是auto或者是绝对位置 ,否则该height将被计算为auto

Based on those 2 options, and as you mentioned in a comment, your own header is dynamic in height, you are left with absolute positioning. 基于这两个选项,并且正如您在注释中提到的那样,您自己的标题的高度是动态的,您将得到绝对的定位。

The problem with adding absolute to the content , it will be taken out of flow and stop behaving as a normal flowed flex item, the good news, one can add a wrapper set to absolute. 将绝对值添加到content ,它将被淘汰,并且不再像正常流动的flex项那样运行,这是一个好消息,可以将包装器设置为绝对值。

Stack snippet 堆栈片段

 .container { display: flex; flex-direction: column; width: 200px; height: 100px; } .header { display: flex; background-color: rgba(255, 0, 0, 0.5); } .content { position: relative; /* added */ flex-grow: 1; background-color: rgba(0, 255, 0, 0.5); } .wrapper { position: absolute; /* added */ left: 0; /* added */ top: 0; /* added */ right: 0; /* added */ bottom: 0; /* added */ } .third-party-component { height: 100%; width: 100%; background-color: rgba(0, 0, 255, 0.5); } 
 <div class="container"> <div class="header">Header</div> <div class="content"> <div class="wrapper"> <div class="third-party-component"> Third party component </div> </div> </div> </div> 


Another option could be to update the Flexbox properties, to give the content a height, using flex: 1 1 100% and give header flex-shrink: 0; 另一种选择是使用flex: 1 1 100%并给header flex-shrink: 0;更新Flexbox属性,以使content具有高度flex-shrink: 0; so it doesn't shrink (as content got 100%). 因此它不会缩小(因为content达到100%)。

This might not work on Safari though, as I know it have had issues when the height property is not set, though can't test that now as I don't have access to Safari. 不过,这可能无法在Safari上使用,因为我知道未设置height属性时遇到了问题,但由于无法访问Safari,现在无法测试。

 .container { display: flex; flex-direction: column; width: 200px; height: 100px; } .header { display: flex; flex-shrink: 0; background-color: rgba(255, 0, 0, 0.5); } .content { flex: 1 1 100%; background-color: rgba(0, 255, 0, 0.5); } .third-party-component { height: 100%; width: 100%; background-color: rgba(0, 0, 255, 0.5); } 
 <div class="container"> <div class="header">Header</div> <div class="content"> <div class="third-party-component"> Third party component </div> </div> </div> 

Because .content haven't height (height = 0px) and .third-party-component have 100% of 0px. 因为.content没有高度(height = 0px),而.third-party-component具有100%的0px。 You can add propety height : calc (100% - <height of .header>) into .content 您可以将propety height : calc (100% - <height of .header>).content

 .container { display: flex; flex-direction: column; width: 200px; height: 100px; } .header { display: flex; background-color: rgba(255, 0, 0, 0.5); } .content { height: calc(100% - 18px); flex-grow: 1; background-color: rgba(0, 255, 0, 0.5); } .third-party-component { height: 100%; width: 100%; background-color: rgba(0, 0, 255, 0.5); } 
 <div class="container"> <div class="header">Header</div> <div class="content"> <div class="third-party-component"> Third party component </div> </div> </div> 

You can simply use another flex container in the .content element: 您可以简单地在.content元素中使用另一个flex容器:

 .container { display: flex; flex-direction: column; width: 200px; height: 100px; } .header { display: flex; background-color: rgba(255, 0, 0, 0.5); } .content { flex-grow: 1; display: flex; flex-direction: column; background-color: rgba(0, 255, 0, 0.5); } .third-party-component { flex-grow: 1; height: 100%; width: 100%; background-color: rgba(0, 0, 255, 0.5); } 
 <div class="container"> <div class="header">Header</div> <div class="content"> <div class="third-party-component"> Third party component </div> </div> </div> 

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

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