"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Why Does \"height: 100%\" on a Flex Child Break Flexbox?

Why Does \"height: 100%\" on a Flex Child Break Flexbox?

Posted on 2025-03-23
Browse:752

Why Does \

Stretching Flex Child to Fill Container Height Without Explicit Parent Height

In situations where we need to stretch a flex child to fill the container's entire height, ensuring the parent's height depends on the contents of another child, setting "height: 100%" on the child can lead to unexpected behavior in Flexbox.

Why Using "height: 100%" Can Break Flexbox

When using "height: 100%" in Flexbox:

  • The parent of the element being stretched requires a declared height, which goes against Flexbox's default behavior.
  • If there are other children above or below the stretched element, "height: 100%" will ignore their presence and lead to conflicts.

The Solution: Remove "height: 100%"

To achieve the desired stretching without breaking Flexbox, simply remove "height: 100%" from the child element.

Explanation

In Flexbox with row direction (the default), the "align-items" property controls the vertical behavior. Its default setting is "stretch," causing flex items to automatically stretch to fill the available height, provided the parent has no explicit height set.

Code Example

Consider the following code snippet:

some
cool
text

In this example, the yellow child will stretch to fill the entire height of its parent, while the parent's height will automatically adjust based on the blue child's text content.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3