简体   繁体   English

Firefox忽略了Flexbox的最大高度

[英]Firefox ignores max-height with Flexbox

I have written the following Flexbox layout: 我编写了以下Flexbox布局:

https://jsfiddle.net/atb5yyez/4/ https://jsfiddle.net/atb5yyez/4/

 html, body { max-height: 300px; overflow: hidden; } .container1 { display: flex; max-height: 100%; flex-flow: column; } .container2 { display: flex; flex-flow: column; max-height: 100%; box-sizing: border-box; border: 3px solid black; } header { background-color: blue; } main { background-color: red; max-heigth: 100%; overflow: auto; } footer { background-color: green; } 
 <div class="container1"> <div class="container2"> <header>header</header> <main> content <br>content <br>content <br>content <br>content <br>content <br>content <br>content <br>content <br>content <br>content <br>content <br>content <br>content <br>content <br>content <br>content <br>content <br>content <br>content <br>content <br>content <br>content <br>content <br>content <br>content <br>content <br>content <br>content <br>content <br>content </main> <footer>footer</footer> <div> </div> 

Chrome renders this page correctly: header and footer are sticky and the content takes as much space as possible, while still not taking more than the height of the container. Chrome可以正确呈现此页面:页眉和页脚具有粘性,内容占用了尽可能多的空间,而占用的空间仍不超过容器的高度。 在此处输入图片说明

With other browsers (Firefox, Edge, Android Browser etc.) the header is still there but the footer is not and the container takes way too much space. 对于其他浏览器(Firefox,Edge,Android浏览器等),标头仍然存在,但页脚不存在,并且容器占用太多空间。 在此处输入图片说明

Why is this happening? 为什么会这样呢? Is Chrome rendering the thing correctly and other browsers have a bug? Chrome浏览器是否可以正确渲染内容,其他浏览器是否有错误? Or have I done something wrong and Chrome is the only browser which forgives me? 还是我做错了什么,Chrome是唯一可以原谅我的浏览器?

Now I know I can "solve" this problem by using only one Flexbox container. 现在,我知道可以仅使用一个Flexbox容器来“解决”此问题。 However, the problem occurred in a real web project and I needed to isolate it to producude the example code. 但是,该问题发生在一个真实的Web项目中,我需要将其隔离以产生示例代码。 In the real project I need to use two containers so it should be possible to solve this by keeping both containers in place. 在实际的项目中,我需要使用两个容器,因此应该可以通过将两个容器都放置在适当的位置来解决此问题。

Your problem is, as has been stated in a few comments (by GCyrillus), that you're trying to inherit the (max-)height values. 正如一些评论(由GCyrillus发表)所述,您的问题是,您试图继承(max-)height值。

I'm guessing, what you want to be doing, is setting the max-height to 100% of the viewport height? 我猜,您想做什么,是将max-height设置为视口高度的100%? In that case, you can change all instances of 100% to 在这种情况下,您可以将所有100%的实例更改为

max-height: 100vh;

This sets it to 100 procent of V iewport H eight. 这将其设置为视频端口H的 100%。

Also, your html and body have heights set to 500px and then max-heights set to 300px, which seems odd. 另外,您的html和body的高度设置为500px,然后max-heights设置为300px,这似乎很奇怪。

If you're trying to keep this max-height of 300px, why don't you just set the max-height of container1, container2 and main to 300px as well? 如果您尝试将最大高度保持为300px,为什么不将container1,container2和main的最大高度也设置为300px?

The problem was an incorrect attempt to inherit max-height values and was solved by GCyrillus by using flex and height properties. 该问题是继承max-height值的错误尝试,并由GCyrillus通过使用flex和height属性解决了。

See: 看到:

jsfiddle.net/atb5yyez/6
jsfiddle.net/atb5yyez/7

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

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