🎨CSS & Color

Flexbox Centering Patterns

The easiest ways to center any element vertically and horizontally.

Explanation

Centering used to be hard; Flexbox makes it a three-line solution.

Examples

Perfect Center
Output
display: flex, justify-content: center, align-items: center

Code Examples

CSS
.center-all {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100px;
}

Try it Now

💡 Tips

  • Use flex-direction: column if you have multiple stacked items
  • gap: 1rem is better than margins for spacing flex items
  • Use place-items: center for a shorthand version

⚠️ Common Pitfalls

  • Container must have a height for vertical centering to work