Back to all articles
September 14, 2026
1 min read
KDLen Team

Top 5 CSS Flexbox Tricks Every Web Dev Needs

KDLen Learning Blog

Top 5 CSS Flexbox Tricks Every Web Dev Needs

CSS Flexible Box Layout (Flexbox) is one of the most powerful layout modules in modern web design. Whether you are crafting complex navigation bars or building responsive cards, mastering these five tricks will accelerate your frontend development workflow.

1. Perfect Centering in Two Lines

Centering an element both vertically and horizontally used to be notoriously difficult in CSS. With Flexbox, it requires only two properties on the container:

css
.container {
  display: flex;
  justify-content: center;
  align-items: center;
}

Alternatively, you can also set margin: auto on the flex child element!

2. Using the Gap Property

Say goodbye to messy negative margins and :last-child selectors. The gap property provides uniform spacing between flex items:

css
.card-grid {
  display: flex;
  gap: 1.5rem;
}

3. Auto Margins for Navbar Alignment

When creating application navigation bars, you frequently want brand items on the left and user action buttons on the right. An auto margin on the target element pushes all subsequent items to the opposite side:

css
.nav-item-user {
  margin-left: auto;
}

4. Understanding flex: 1 1 auto

The flex shorthand combines flex-grow, flex-shrink, and flex-basis. Setting flex: 1 1 0% allows elements to grow and shrink proportionally while ignoring intrinsic content size:

css
.col-equal {
  flex: 1 1 0%;
}

5. Responsive Grids with flex-wrap

When combined with min-width or flex-basis, flex-wrap: wrap gives you a responsive grid that automatically wraps onto multiple rows on smaller mobile screens without requiring complicated media queries:

css
.responsive-row {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
}