How does margin-top: auto; work?

Hey All, I was looking for a solution to keep my footer either pinned to the bottom of the page or the content, whichever is greater, and found the following solution. It works, but I can't wrap my head around why
margin-top: auto;
margin-top: auto;
is working the way it is? When I don't have enough content the margin-top is some calculated value, and when the content is bigger than the page the margin-top is 0. Here's the basic code:
<html>
<head></head>
<body>
<header></header>
<main>
<!-- Content -->
</main>
<footer></footer>
</body>
</html>
<html>
<head></head>
<body>
<header></header>
<main>
<!-- Content -->
</main>
<footer></footer>
</body>
</html>
body {
display: flex;
flex-direction: column;
}

footer {
margin-top: auto;
}
body {
display: flex;
flex-direction: column;
}

footer {
margin-top: auto;
}
11 Replies
Jochem
Jochem11mo ago
afaik, for margin-top and margin-bottom setting them to auto is equivalent to setting them to 0 what are you trying to accomplish with setting margin-top to auto?
b1mind
b1mind11mo ago
only time I ever use it xD
TheNoviceTrader
TheNoviceTrader11mo ago
Yeah, I was reading mdm regarding margin, auto and margin-top: auto and it loosely said that it sets it to 0. But, when I use it as I have and the page doesn't have enough content (i.e usually the footer is not on the bottom), then it is correctly calculating the required value to push the footer to the bottom of the page. And if there is enough content then the value is 0 as expected.
b1mind
b1mind11mo ago
So you have to get your body to be 100%/dvh and / or the wrapper first. otherwise its going to not take up the space and the auto margin won't matter cause its got nothing to fill. That codepen should give you a good start. I personally prefer the grid method though.
TheNoviceTrader
TheNoviceTrader11mo ago
Like I said, what I've done works, I just don't understand why margin-top: auto calculates the correct value when in the mdm docs it says it would be 0?
b1mind
b1mind11mo ago
because its a flex child
b1mind
b1mind11mo ago
Chris Coyier
CSS-Tricks
How Auto Margins Work in Flexbox | CSS-Tricks
Robin has covered this before, but I've heard some confusion about it in the past few weeks and saw another person take a stab at explaining it, and I wanted
b1mind
b1mind11mo ago
I thought that article explained it better sry lol Something to do with flex children becoming dense/shrinking and how setting margin to auto push to fill the remaining space. But goes back to a great principle and that is Parent > Child relationships. Like grid/flex children margins will also not collapse I thought Kevin covered it in the flex course but can't find the reference atm.
b1mind
b1mind11mo ago
Sidenote the reason I like Grid method more is you can toss it back to the intrinsic nature of grid and do something like this. If more content is added and still not enough to fill but you want it bottom. https://codepen.io/b1mind/pen/VwVVBNB?editors=0100
TheNoviceTrader
TheNoviceTrader11mo ago
Thanks for all your help. I understand now and more importantly after reading a different part of mdm found the reasoning: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_flexible_box_layout/Aligning_items_in_a_flex_container#alignment_and_flex-direction
Aligning items in a flex container - CSS: Cascading Style Sheets | MDN
One of the reasons that flexbox quickly caught the interest of web developers is that it brought proper alignment capabilities to the web for the first time. It enabled proper vertical alignment, so we can at last easily center a box. In this guide, we will take a thorough look at how the alignment and justification properties work in Flexbox.