I am new to web coding, and met a strange behavior when using flexbox.
When a parent is using flex-grow: 1
, the child element height: 100%
doesn’t work.
I saw several solutions but doesn’t work for me. Does anyone could explain the reason and share some solutions? Thanks!
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
html,
body {
min-height: 100vh;
}
body {
display: flex;
flex-direction: column;
}
header {
flex: 0 0 auto;
background-color: aqua;
}
main {
flex: 1 0 auto;
/* Enable flex grow */
background-color: blanchedalmond;
}
footer {
flex: 0 0 auto;
background-color: brown;
}
div#this_should_fill_main {
height: 100%;
/*This doesn't work!*/
background-color: cornflowerblue;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<header>This is a header</header>
<main>
<div id="this_should_fill_main">This is main content</div>
</main>
<footer>This is a footer</footer>
</body>
</html>
I tried to add height: 0
to <main>
, but when the content is overflow, the footer will not be sticky.
I also tried other solutions mentioned in this queestion How can I make Flexbox children 100% height of their parent?, but make no sense for me.
- add
align-items: stretch
to<main>
; no change at all - add
align-self: stretch
to<div id="this_should_fill_main">
; no change at all - use
display: flex
again in<div id="this_should_fill_main">
; it works, but why doesn’t onlyheight: 100%
work?
Could you please help me to solve:
- Why
height: 100%
doesn’t work in this case? - How to solve this without using flexbox again?
Thanks for all your answers!
2
Answers
Add
height: 0;
ormin-height: 0;
tomain
, and it works.As for the reason why
height: 100%
doesn’t work, you can check this Github issue.(Code was updated for sticky footer)
This issue you are facing is because of how flexbox is handling the height of items. When you set
height: 100%
to thediv#this_should_fill_main
, it will try to take the 100 percent of its parent which ismain
. But issue starts here because main does not have a defined height because in a flex container, height of the child elements is defined by their content, except you say otherwise.Another solution could be using properties of flexbox;
display: flex
to mainflex-direction: column;
to your main if you want your content to displayed vertically.flex-grow: 1
todiv#this_should_fill_main
so that it takes all available place of parent