Auto-margin Alignment

Beware of margin: auto alignment on flex items.

When you use margin: auto on flex-items, things can look quite weird. You do need to understand what’s going on. It may result in unexpected results, but I’m going to explain all that.

When you use margin:auto on a flex-item, the direction (left, right or both) that has the value auto will take up any empty spaces available.

That’s a difficult one to catch. Here’s what I mean:

Consider the navigation bar marked up and styled below:

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy