DEV Community

Cover image for CSS Flexbox Cheat Sheet: From Beginner to Advanced
Daley Mottley
Daley Mottley

Posted on

CSS Flexbox Cheat Sheet: From Beginner to Advanced

CSS Flexbox revolutionizes the way developers build responsive and flexible web layouts.

It’s a must-know tool whether you’re new to CSS or aiming to sharpen your design skills.

This cheat sheet will take you step-by-step from Flexbox basics to advanced techniques.


Table of Contents

  1. What is CSS Flexbox?
  2. Basic Concepts and Properties
  3. Common Flexbox Use Cases
  4. Advanced Flexbox Techniques
  5. Flexbox Tricks for Responsive Design
  6. Conclusion

What is CSS Flexbox?

The CSS Flexible Box Layout (Flexbox) module is a layout model designed for one-dimensional content. It provides a more efficient way to design layouts compared to traditional methods like floats or inline-block.

Key Benefits:

  • Simplifies alignment and spacing.
  • Supports responsive designs without media queries for basic adjustments.

Basic Concepts and Properties

Flexbox works through a parent container (the flex container) and its children (flex items). Below are the key properties and their usage:

Flex Container Properties

  • display: flex;: Defines a flex container.
  • flex-direction: Determines the direction of flex items (row, column, etc.).
.container {
  display: flex;
  flex-direction: row; /* default */
}
Enter fullscreen mode Exit fullscreen mode
  • justify-content: Aligns items along the main axis.
.container {
  justify-content: center; /* Options: flex-start, flex-end, center, space-between, space-around, space-evenly */
}
Enter fullscreen mode Exit fullscreen mode
  • align-items: Aligns items along the cross axis.
.container {
  align-items: stretch; /* Options: flex-start, flex-end, center, baseline, stretch */
}
Enter fullscreen mode Exit fullscreen mode

Flex Item Properties

  • flex: Shorthand for flex-grow, flex-shrink, and flex-basis.
.item {
  flex: 1; /* Item takes up equal space */
}
Enter fullscreen mode Exit fullscreen mode
  • order: Changes the order of items.
.item {
  order: 2; /* Higher numbers appear later */
}
Enter fullscreen mode Exit fullscreen mode

Common Flexbox Use Cases

Centering Content

Centering elements horizontally and vertically is a breeze with Flexbox:

.container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh; /* Full viewport height */
}
Enter fullscreen mode Exit fullscreen mode

Creating a Navigation Bar

Build a responsive navigation bar with evenly spaced items:

.navbar {
  display: flex;
  justify-content: space-between;
}
Enter fullscreen mode Exit fullscreen mode

Advanced Flexbox Techniques

Nested Flex Containers

Use nested containers to create complex layouts:

.parent {
  display: flex;
  flex-direction: column;
}

.child {
  display: flex;
  justify-content: space-between;
}
Enter fullscreen mode Exit fullscreen mode

Flex Basis vs Width

Control item size with flex-basis instead of width:

.item {
  flex-basis: 200px;
}
Enter fullscreen mode Exit fullscreen mode

Flexbox Tricks for Responsive Design

Reordering for Small Screens

Reorder items without changing the HTML structure:

.item:nth-child(2) {
  order: -1; /* Moves the second item to the top */
}
Enter fullscreen mode Exit fullscreen mode

Flexible Grids

Create a flexible grid layout without relying on CSS Grid:

.container {
  display: flex;
  flex-wrap: wrap;
}

.item {
  flex: 1 1 200px; /* Flex-grow, flex-shrink, flex-basis */
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

Mastering CSS Flexbox opens the door to designing modern, responsive layouts with ease. Start with the basics, experiment with advanced techniques, and apply them to your projects for optimal results. With practice, Flexbox will become an indispensable tool in your web design arsenal.

For further exploration, check out the MDN Flexbox Guide.


Happy coding!

Top comments (0)