Logo

dev-resources.site

for different kinds of informations.

Flexbox vs Grid: A Guide to Choosing the Best Layout with Examples

Published at
11/9/2024
Categories
css
css3
flexbox
grid
Author
eleftheriabatsou
Categories
4 categories in total
css
open
css3
open
flexbox
open
grid
open
Author
16 person written this
eleftheriabatsou
open
Flexbox vs Grid: A Guide to Choosing the Best Layout with Examples

Hello, amazing developers! 👋

Introduction

Today, we're exploring two powerful CSS layout tools: Flexbox and Grid. Understanding these can transform how you design web layouts. By the end of this article, you'll not only understand the fundamental differences between these two systems but also:

  • Grasp the basics of Flexbox and Grid, including when to use each.

  • Explore practical examples of how both can be applied in real-world scenarios.

  • Learn how to combine Flexbox and Grid for even more complex layouts.

  • Discover common pitfalls to avoid and tips for mastering these tools.

  • Gain insights into resources for further learning.

Below, you will also find a short introduction to what Flexbox and Grid are, but if you want more details, dont forget to check these articles:

Advanced Flexbox: Techniques and Best Practices

Explore advanced CSS Flexbox techniques, real-world applications, and best practices to optimize your web layouts for any project

favicon eleftheriabatsou.hashnode.dev

CSS Grid: A Friendly Guide with Examples

Learn CSS Grid with Eleftheria for easy, responsive, and dynamic two-dimensional layouts through practical examples and tips

favicon eleftheriabatsou.hashnode.dev

What is Flexbox?

Flexbox, or Flexible Box Layout, is your one-dimensional organizer. Imagine it as a single line where items can align either horizontally or vertically.

Example:

.container {
  display: flex;
  flex-direction: row; /* or column */
  justify-content: center; /* align items horizontally */
  align-items: center; /* align items vertically */
}

.item {
  margin: 10px;
}

Enter fullscreen mode Exit fullscreen mode
<div class="container">
  <div class="item">Item 1</div>
  <div class="item">Item 2</div>
  <div class="item">Item 3</div>
</div>

Enter fullscreen mode Exit fullscreen mode

Result:

Flexbox in Action

Here's how you might use Flexbox to create a simple navigation bar:

.nav {
  display: flex;
  justify-content: space-between; /* space items evenly */
  background-color: #333;
  padding: 10px;
}

.nav a {
  color: white;
  text-decoration: none;
  padding: 15px;
}

Enter fullscreen mode Exit fullscreen mode
<nav class="nav">
  <a href="#">Home</a>
  <a href="#">About</a>
  <a href="#">Contact</a>
</nav>

Enter fullscreen mode Exit fullscreen mode

Result:

💡Note: For more details, check out this* article.

Introducing Grid

CSS Grid is like a two-dimensional chessboard. It lets you place items in rows and columns at the same time, giving you more control over your layout.

Example:

.grid-container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr; /* three equal columns */
  grid-gap: 10px;
}

.grid-item {
  background-color: #f0f0f0;
  border: 1px solid #ccc;
  padding: 20px;
  text-align: center;
}

Enter fullscreen mode Exit fullscreen mode
<div class="grid-container">
  <div class="grid-item">1</div>
  <div class="grid-item">2</div>
  <div class="grid-item">3</div>
  <div class="grid-item">4</div>
  <div class="grid-item">5</div>
  <div class="grid-item">6</div>
</div>
Enter fullscreen mode Exit fullscreen mode

Result:

Grid Layout Examples

Let's create a simple gallery layout using Grid:

.gallery {
  display: grid;
  grid-template-columns: repeat(3, 1fr); /* three equal columns */
  gap: 1rem;
}

.gallery img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

Enter fullscreen mode Exit fullscreen mode
<div class="gallery">
  <img src="img1.jpg" alt="Image 1">
  <img src="img2.jpg" alt="Image 2">
  <img src="img3.jpg" alt="Image 3">
  <img src="img4.jpg" alt="Image 4">
  <img src="img5.jpg" alt="Image 5">
  <img src="img6.jpg" alt="Image 6">
</div>
Enter fullscreen mode Exit fullscreen mode

Result:

💡Note: For more details, check out this* article.

When to Use Flexbox vs Grid

Use Flexbox for:

  • Simple layouts where items are in one direction.
  • Menus, navigation bars, or form layouts.

Use Grid for:

  • Complex layouts where you need precise control over rows and columns.
  • Responsive designs where you might change the grid structure based on screen size.

Combining Flexbox and Grid

Sometimes, the magic happens when you combine both! Here's how you can nest Flexbox within Grid:

.layout {
  display: grid;
  grid-template-columns: 2fr 1fr; /* Main content and sidebar */
  gap: 20px;
}

.main-content {
  display: flex;
  flex-wrap: wrap; /* Allow items to wrap if needed */
  gap: 10px;
}

.sidebar {
  display: flex;
  flex-direction: column; /* Stack items vertically */
}

.main-content div {
  flex: 1 1 calc(50% - 10px); /* Responsive flex items */
}

Enter fullscreen mode Exit fullscreen mode
<div class="layout">
  <div class="main-content">
    <div>Content 1</div>
    <div>Content 2</div>
    <div>Content 3</div>
    <div>Content 4</div>
  </div>
  <aside class="sidebar">
    <div>Sidebar Item 1</div>
    <div>Sidebar Item 2</div>
  </aside>
</div>
Enter fullscreen mode Exit fullscreen mode

Result:

💡Note: To test the results on your own, or to make some practice feel free to copy-paste the code (HTML and CSS) to Codepen!

Common Pitfalls and Tips

  • Order of Flex Items: Remember, order property in Flexbox can disrupt the natural flow. Use with caution!

  • Grid Template Areas: For complex layouts, grid-template-areas can be your friend but might confuse if overused.

Tips: 🚀

  • Start simple. Don't use Grid for every layout just because it's powerful.

  • Always test your layouts across different screen sizes.

  • Use browser developer tools to tweak and visualize your layouts in real-time.

Resources for Further Learning

For those eager to learn more:

  • MDN Web Docs for detailed documentation on Flexbox and Grid.

  • CSS Tricks has amazing articles and guides.

  • Practice on sites like CodePen or JSFiddle.

Conclusion

In wrapping up, Flexbox and Grid complement each other, offering flexibility and precision in creating robust, responsive layouts. Whether you're aligning a simple navigation bar or crafting an intricate dashboard, understanding when to use each will elevate your web development skills. Keep practicing, keep exploring, and most importantly, have fun creating!


👋 Hello, I'm Eleftheria, Community Manager, developer, public speaker, and content creator.

🥰 If you liked this article, consider sharing it.

🔗 All links | X | LinkedIn

grid Article's
30 articles in total
Favicon
GridLookout: Building Viewport-Aware Multi-Layer Grid Positioning Of React Components
Favicon
Easily Integrate Syncfusion UI Components into PowerApps
Favicon
Mastering Flutter DataGrid with Pagination: Build Efficient Data Tables
Favicon
Flexbox vs Grid: A Guide to Choosing the Best Layout with Examples
Favicon
Effectively Visualize Data: Add Grids and Charts in JavaScript Pivot Field List
Favicon
Learn CSS Grid: Simple Guide with Plenty of Examples
Favicon
Grid Layout: The Ultimate Guide for Beginners
Favicon
Unlocking the Power of CSS Grid for Modern Web Design
Favicon
Make a grid element span to the full width of the parent
Favicon
Balance strategy and grid strategy
Favicon
From Requirements to Code: Implementing the Angular E-commerce Product Listing Page
Favicon
Bootstrap Tutorials: Grid system
Favicon
🎲 Criando um Dado com Flexbox e CSS Grid Layout
Favicon
Simple grid strategy in Python version
Favicon
How To Set Up Docker Selenium GRID
Favicon
React feature rich Table/Grid Libraries every developer should know.
Favicon
Adding Gif Canvas Features : Grid Snap
Favicon
Flex vs. Grid: Choosing the Right CSS Layout
Favicon
How to Create Instagram Explore Grid Layout with React Native
Favicon
#Fashion #Hero #Container #Using #Grid and #Flex
Favicon
Grid de 8 pontos uma técnica que torna seu projeto escalável
Favicon
Quick guide to CSS Grid
Favicon
VueJS + CSS: grid template based on variable
Favicon
Learn Css Grid Style Layout In 10 Minutes
Favicon
Get in to the Grid: Style Elements Made Easy
Favicon
React Grid Collection
Favicon
How To Center An Elements With CSS Grid
Favicon
Moving a Square with CSS Grid and Minimal JavaScript
Favicon
Introducing the AG Grid Figma Design System
Favicon
AWS Grid computing for Capital Markets (FSI)

Featured ones: