DEV Community

Cover image for 20 Useful Flexbox CSS Snippets to Check Out
Silvia O'Dwyer
Silvia O'Dwyer

Posted on

2

20 Useful Flexbox CSS Snippets to Check Out

๐ŸŽฏ 20 Useful Flexbox CSS Snippets You'll Use Again and Again

Flexbox is one of the most powerful layout tools in CSS. In this guide, I'll be covering 20 useful CSS snippets for utilising Flexbox effectively. It'll include common scenarios and lots of tips n' tricks.

Let's get started!


๐Ÿ“Œ Anatomy of a Flexbox Rule

Before we dive in, letโ€™s break down a Flexbox rule:

.container {
  display: flex; /* Defines a flex container */
  justify-content: center; /* Aligns items horizontally */
  align-items: center; /* Aligns items vertically */
}
Enter fullscreen mode Exit fullscreen mode
  • Selector (.container): Targets the element.
  • Property (display: flex): Turns it into a flex container.
  • Values (center, space-between, etc.): Adjust the layout.

๐Ÿ’ก Top Tip: When in doubt, add outline: 1px solid red; to see what's happening!


1. Center Anything (Literally)

No more margin: auto tricksโ€”this centers anything inside a flex container.

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

Centered Flexbox square

2. Space Out Items Evenly

Want equal spacing between items? Use justify-content: space-between.

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

Row of squares with space between each using Flexbox


3. Wrap Items to the Next Line

Allows items to wrap when they exceed the container width.

.container {
  display: flex;
  flex-wrap: wrap;
}
Enter fullscreen mode Exit fullscreen mode

Two rows of squares using Flexbox

This is great for creating responsive columns without the need for extra media queries.

4. Make a Sticky Footer

Ensure your footer sticks to the bottom when there's little content.

body {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}
.footer {
  margin-top: auto;
}
Enter fullscreen mode Exit fullscreen mode

5. Add 3 Items per Row

A common scenario involves adding a certain number of items per row, such as 3 images per row for a gallery.
To do this, just create a parent container and then set the width of the elements within to a percentage. For 3 items, it would be 33%, for 4 items it would be 25% and so forth.

.container {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    max-width: 70vw;
    justify-content: center;
}

.container img {
    width: calc(33.333% - 10px); /* Three images per row */
    height: auto;
}
Enter fullscreen mode Exit fullscreen mode

Gallery of images


6. Gap Property Support in Flexbox

Modern browsers now support the gap property in Flexbox layouts, eliminating the need for margins between items.

.container {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
}
Enter fullscreen mode Exit fullscreen mode

7. Flex-grow

You can use fractional values with flex-grow to create more nuanced distributions of space.

.container {
  display: flex;
}

.item-primary {
  flex-grow: 2; /* Gets twice as much extra space */
}

.item-secondary {
  flex-grow: 1; /* Gets normal amount of extra space */
}

.item-minimal {
  flex-grow: 0.5; /* Gets half as much extra space */
}
Enter fullscreen mode Exit fullscreen mode

Flexbox row of rectangles

8. Align Items to the Bottom

Great for chat bubbles or sticky elements.

.bottom-align {
  display: flex;
  align-items: flex-end;
}
Enter fullscreen mode Exit fullscreen mode

9. Reverse Column Order

Flip the order of items without touching the HTML.

.reverse {
  display: flex;
  flex-direction: column-reverse;
}
Enter fullscreen mode Exit fullscreen mode

Reverse order items with Flexbox, containing squares going in descending order rather than ascending order

10. Equal-Width Flex Items

Make all child elements take up equal space.

.equal {
  display: flex;
}
.equal > * {
  flex: 1;
}
Enter fullscreen mode Exit fullscreen mode

11. Center Align Items

You can ensure the contents in the container stay center-aligned along the cross-axis by setting align-items to center.

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

12. Make One Item Grow, Others Stay Fixed

Perfect for layouts with sidebars or main content.

.grow {
  display: flex;
}
.grow > .main {
  flex: 1;
}
Enter fullscreen mode Exit fullscreen mode

13. Align Item to End of Row

You can also align an item to the end of a row by setting the margin-left property to auto.

.container {
  display: flex;
  flex-direction: row;
}

.item-third {          
  margin-left: auto;
}
Enter fullscreen mode Exit fullscreen mode

Squares with last square at end of row

14. Prevent Items from Shrinking

Prevents items from shrinking when the container size changes. To ensure the items wrap to the next line, add the flex-wrap property also.

.item {
  flex-shrink: 0;
  flex-wrap: wrap;
}
Enter fullscreen mode Exit fullscreen mode

15. Align Content When Wrapping

Controls the alignment of flex items when they wrap to a new line.

.container {
  display: flex;
  flex-wrap: wrap;
  align-content: center;
}
Enter fullscreen mode Exit fullscreen mode

16. Wrap-reverse

If you'd like to reverse the order of the items and also ensure that the items wrap too, just set flex-wrap to wrap-reverse.

.container {
  display: flex;
  flex-wrap: wrap-reverse;
}
Enter fullscreen mode Exit fullscreen mode

17. Align One Specific Item Differently

Overrides alignment for a specific flex item.

.item {
  align-self: flex-end;
}
Enter fullscreen mode Exit fullscreen mode

18. Controlling Flex Item Growth Precisely

Using auto margins on flex items creates space between groups of items without extra wrapper elements. This is perfect for creating navigation bars with separated sections.

.navbar {
  display: flex;
  flex-direction: row;
}

.logo {
  margin-right: auto; /* Pushes everything else to the right */
}

.nav-links {
  /* These will be pushed to the right side */
}
Enter fullscreen mode Exit fullscreen mode

Navbar with image on left and links on right-hand side

19. Ordering Items in Responsive Layouts

The order property can rearrange flex items without changing your HTML structure. This is especially useful for responsive designs.

.container {
  display: flex;
}

.sidebar {
  order: 2; /* Will appear second visually */
}

.main-content {
  order: 1; /* Will appear first visually */
}
Enter fullscreen mode Exit fullscreen mode

20. Align to End of Column

You can align an item to the end of a column by setting margin-top to auto. This is useful for sidebar layouts, for instance.

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

.item-third {          
  margin-top: auto;
}
Enter fullscreen mode Exit fullscreen mode

Flexbox column with square at end


Follow Me for More Tips

If you want to get more resources like this, be sure to follow me on X/Twitter where I provide more resources like this. :-)

Got Any Other Tips to Share?

Thanks for reading! I hope that you've found this resource useful. What are your thoughts? Do you have any other Flexbox tips that you'd like to share? Be sure to let me know in the comments section below.

These snippets are super useful for common scenarios that you'll encounter as a developer. Flexbox is an incredibly powerful CSS layout tool for your toolkit. Copy these snippets, tweak them as needed, and use these to start building awesome layouts.

Thanks for reading!

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

๐Ÿ‘‹ Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someoneโ€™s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay