


Why Doesn't `height: 100%` Work as Expected in Flexbox Columns in Chrome?
Nov 09, 2024 pm 10:03 PMHeight Problems in Flexbox Columns: A Chrome Quirk
In flexbox columns, you might encounter difficulties when using height percentages to size child elements. This issue stems from a quirk in Chrome's handling of the flexbox specification.
Consider the following example:
<div class='flexbox'> <div class='flex'> <div class='flex-child'></div> </div> <div class='static'></div> </div>
.flexbox { position: absolute; background: black; width: 100%; height: 100%; display: flex; flex-direction: column; } .flex { background: blue; flex: 1; } .flex-child { background: red; height: 100%; width: 100%; display: block; } .static { background: green; width: 100%; height: 5rem; }
You would expect the red .flex-child element to occupy the entire height of its blue .flex parent. However, height:100% doesn't function as intended.
Solution:
To resolve this issue:
- Modify .flex to display: flex.
- Remove height:100% from .flex-child.
This adjustment ensures that .flex-child fills the entire space within .flex.
Technical Background:
In flexbox, align-self:stretch (the default) determines the cross-size property (here, height) of a flexed element. However, percentages use the specified cross-size value, not its used value. In this case, .flex-child tries to occupy 100% of .flex's specified height, which is auto by default. This confuses the layout engine.
Setting .flex to display: flex resolves the problem by overriding the default behavior and allowing the higher-level CSS to determine the sizing of .flex.
Exception:
This solution won't work if you want to partially fill .flex. To achieve this, you can add a .spacer element within .flex with flex:1 and set flex-child to flex:9 (and flex-direction:column on .flex). However, this is a hack and it's hoped that future browser updates will address this quirk.
The above is the detailed content of Why Doesn't `height: 100%` Work as Expected in Flexbox Columns in Chrome?. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Adding Box Shadows to WordPress Blocks and Elements

Create a JavaScript Contact Form With the Smart Forms Framework

Making Your First Custom Svelte Transition

Demystifying Screen Readers: Accessible Forms & Best Practices

Comparing the 5 Best PHP Form Builders (And 3 Free Scripts)
