Flexbox Align: When to Use Auto Margins Instead of Flexbox Center
Flexbox alignment properties are superb and they solve lots of layout problems, especially where the common “vertical and horizontal center” layout is needed. However, there are times when aligning with auto margins is safer and more flexible. This tutorial will show you when!
Get Started with Flexbox
If you’re just stepping into the world of flexbox we have a series of comprehensive beginner’s tutorials to get you up to speed. Learn the basics of flexbox alignment, flexbox ordering, and flexbox sizing, and you’ll be perfectly equipped for more intermediate tutorials later on!


A Comprehensive Guide to Flexbox Alignment

Anna Monus23 May 2021

A Comprehensive Guide to Flexbox Ordering & Reordering

Anna Monus05 Jun 2021

A Comprehensive Guide to Flexbox Sizing

Anna Monus06 Jun 2021
The Project
For this demonstration, we’ll assume that we’re building a minimal blog website for a client. The designer provided us the following page layout:



When the menu toggle button is clicked, the fullscreen menu will appear and look like this:



Pay attention to the following:
- The header will be fixed and remain as we scroll.
- The menu will be fullscreen and fixed. Its items will be vertically and horizontally centered. However, if their total height exceeds the menu height, a scrollbar will appear.
1. Define the HTML Markup
Let’s begin by setting out the required markup:
1 |
<header class="page-header"> |
2 |
<nav>
|
3 |
<div class="page-header-inner"> |
4 |
<a href="" class="logo">Brand</a> |
5 |
<button type="button" class="toggle-menu">MENU</button> |
6 |
</div>
|
7 |
|
8 |
<div class="menu-wrapper"> |
9 |
<ul>
|
10 |
<li>
|
11 |
<a href="">...</a> |
12 |
</li>
|
13 |
|
14 |
<!-- more list items here -->
|
15 |
</ul>
|
16 |
|
17 |
<!-- helper buttons here -->
|
18 |
</div>
|
19 |
</nav>
|
20 |
</header>
|
Within the nav element, we’ll define two helper buttons that will allow us to add or remove items by clicking them. The minimum number of items will be three, but we can add and remove items to make our demo clearer.

2. Specify Some CSS Styles
Here’s a part of the styles we’ll use:
1 |
/*CUSTOM VARIABLES HERE*/
|
2 |
|
3 |
.page-header, |
4 |
.page-header .menu-wrapper { |
5 |
position: fixed; |
6 |
top: 0; |
7 |
left: 0; |
8 |
bottom: 0; |
9 |
right: 0; |
10 |
}
|
11 |
|
12 |
.page-header .page-header-inner { |
13 |
display: flex; |
14 |
align-items: center; |
15 |
justify-content: space-between; |
16 |
height: 60px; |
17 |
padding: 0 20px; |
18 |
background: var(--beige); |
19 |
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.15); |
20 |
}
|
21 |
|
22 |
.page-header .page-header-inner * { |
23 |
z-index: 1; |
24 |
}
|
25 |
|
26 |
.page-header .menu-wrapper { |
27 |
display: none; |
28 |
align-items: center; |
29 |
justify-content: center; |
30 |
overflow-y: auto; |
31 |
background: var(--white); |
32 |
}
|
By default, the .menu-wrapper element will be hidden and receive display: flex as soon as we click the toggle button.
Flexbox Center
To answer a classic layout question, we’re going to use flexbox to center the menu vertically and horizontally. Flexbox makes it really easy: to align the items in the middle of the page we’ll use justify-content: center and align-items: center.
However, there’s an overflow problem. If we add enough items to make the scrollbar appear, we’ll notice that the top ones are being cut off—they disappear completely and we can’t reach them by scrolling.



Test the demo to see yourself; add 20 or so items to the menu:
The safe Keyword
One way to overcome this issue is to take advantage of a new CSS keyword which we can use when we align items: safe.
Here’s its exact definition as taken from Mozilla Developer Network (MDN) Docs:
“Used alongside an alignment keyword. If the chosen keyword means that the item overflows the alignment container causing data loss, the item is instead aligned as if the alignment mode were
start.”
To test this new value we’ll replace align-items: center with align-items: safe center.
In plain English we would translate the safe center values as follow:
“The elements will be vertically centered, but if their height exceeds the flex container’s one, the alignment mode will change to
start. Of course, to see all the items, the flex container should have an appropriateoverflow-yvalue likeauto.”
Unfortunately, at the time of this writing, it isn’t considered a stable solution. In actual fact, it has very limited browser support and only works in recent versions of Firefox.
Check the following demo to see how the safe keyword works (be sure to do so in Firefox):
Auto Margins
Thankfully, there’s another safe solution that we can use: we’ll take advantage of auto margins. This isn’t actually a flexbox property, in fact you’ve probably already used it via the margin: 0 auto rule to center a fixed-width element inside a parent container.
In our case, the trick is to remove the align-items: center and justify-content: center rules from the flex container and give the flex item margin: auto instead.
By setting all margins to auto, all sides of our flex item will try to occupy any available space. If this space exists, they will push it into the center.
This solves our specific problem because auto margins won’t cut off parts of the flex item in the event it exceeds the size of the container.
Note: keep in mind that as soon as you use this technique, the flexbox alignment properties will have no effect.
Here’s the related demo:
3. Toggle Menu
Finally, we’ll use some straightforward JavaScript code to toggle the menu:
1 |
const toggleMenu = document.querySelector(".toggle-menu"); |
2 |
const pageHeader = document.querySelector(".page-header"); |
3 |
|
4 |
toggleMenu.addEventListener("click", function () { |
5 |
pageHeader.classList.toggle("menu-opened"); |
6 |
document.body.classList.toggle("overflow-y-hidden"); |
7 |
});
|
The corresponding styles:
1 |
body.overflow-y-hidden { |
2 |
overflow-y: hidden; |
3 |
}
|
4 |
|
5 |
.page-header.menu-opened .page-header-inner { |
6 |
box-shadow: none; |
7 |
}
|
8 |
|
9 |
.page-header.menu-opened .menu-wrapper { |
10 |
display: flex; |
11 |
}
|
Conclusion
Done! Today we discussed the effective use of auto margins for controlling the alignment of a flex layout. We covered just one specific case, but hopefully you get the idea and will be able to apply it to your own situations.
Two things you have to remember:
- Unlike flexbox alignment properties, auto margins should be defined in the flex item.
- Auto margins take precedence over the flexbox alignment properties.
As always, thanks a lot for reading!
More Flexbox Tutorials
We have lots of practical tutorials to help you learn flexbox–dive in!


How to Build a Responsive Navigation Bar With Flexbox

Anna Monus29 May 2024

How to Build a Full-Screen Responsive Page With Flexbox

George Martsoukos20 Nov 2018

How to Build a News Website Layout with Flexbox

Jeremy Thomas01 Jun 2016

CSS Grid vs. Flexbox: Which Should You Use and When?

Anna Monus01 Jan 2024

How to Build a Responsive, Multi-Level, Sticky Footer With Flexbox

Anna Monus05 Jun 2019

How to Add RTL Support to Flexbox and CSS Grid

Anna Monus16 May 2019

How to Build a Responsive Form With Flexbox

George Martsoukos04 Jul 2016

How to Build a Custom Mega Menu with Flexbox

Anna Monus05 Aug 2019
- Safe/unsafe alignment in CSS flexbox by Stefan Judis
- align-items on MDN



