CSS Layouts – Flex & Grid πŸ—οΈ

Ayush AgarwalAyush Agarwal
5 min read

Introduction: The Spreadsheet of Web Design πŸ“Š

Imagine you are working on a spreadsheet in Excel or Google Sheets. You have different columns for names, prices, and dates, and rows for each product or entry. Some columns might need to be wider, and some cells need to merge across multiple rows. The ability to structure and organize data efficiently is what makes spreadsheets so useful.

Now, think about how websites are designed. Similar to a spreadsheet, a webpage needs:

β€’ Flexible rows and columns to display content dynamically πŸ“Œ

β€’ Perfect alignment and spacing between elements πŸ“

β€’ A well-structured layout to ensure everything looks clean and readable ✨

This is where CSS Flexbox and CSS Grid come in!

β€’ Flexbox is like arranging data dynamically in a single row or column, adjusting as neededβ€”just like resizing cells in a spreadsheet.

β€’ Grid is like a full spreadsheet structure, allowing for complex, multi-row and multi-column layouts with precise control.

By mastering Flexbox and Grid, you can create web layouts that are both functional and visually appealing. Let’s explore how these two powerful tools work! πŸš€


Understanding Flexbox: Aligning and Distributing Items Easily πŸ”„

What is Flexbox?

Flexbox (Flexible Box Layout) is a one-dimensional layout model used for arranging elements either in a row or a column. It is perfect for:

βœ… Aligning items neatly in a row or column

βœ… Distributing space evenly among elements

βœ… Making elements automatically adjust based on screen size

Think of Flexbox as auto-adjusting spreadsheet columnsβ€”when you resize a column, the others adapt dynamically.

Setting Up Flexbox

To use Flexbox, simply add display: flex; to a container.

.container {
  display: flex;
  background-color: lightgray;
}
<div class="container">
  <div class="item">A</div>
  <div class="item">B</div>
  <div class="item">C</div>
</div>

Now, .item elements will automatically align within .container.

Flexbox Properties: Spreadsheet Formatting Rules πŸ“„

1️⃣ The Main Axis vs. Cross Axis

Flexbox arranges items along two axes:

β€’ Main Axis β†’ Defines the direction (row or column).

β€’ Cross Axis β†’ Perpendicular to the main axis (helps with vertical alignment).

2️⃣ Justify Content – Arranging Cells Horizontally πŸ“

Controls how elements are spaced along the main axis (like adjusting column width in a spreadsheet).

.container {
  display: flex;
  justify-content: center; /* Centers items */
}

Options:

β€’ flex-start β†’ Aligns items to the left

β€’ flex-end β†’ Aligns items to the right

β€’ center β†’ Aligns items in the middle

β€’ space-between β†’ Even space between items

β€’ space-around β†’ Space around each item

3️⃣ Align Items – Adjusting Rows πŸ—οΈ

Controls vertical alignment (like adjusting row height in a spreadsheet).

.container {
  display: flex;
  align-items: center; /* Centers vertically */
}

Options:

β€’ flex-start β†’ Aligns top

β€’ flex-end β†’ Aligns bottom

β€’ center β†’ Aligns middle

4️⃣ Flex Grow – Expanding Cells πŸ”

Allows elements to grow and take up extra space (like merging spreadsheet cells dynamically).

.item {
  flex-grow: 1; /* All items take equal space */
}

If an item has flex-grow: 2;, it will be twice the size of other items.

Example: Navigation Bar using Flexbox πŸ—οΈ

.navbar {
  display: flex;
  justify-content: space-between;
  background-color: #333;
  padding: 10px;
}

.navbar a {
  color: white;
  text-decoration: none;
  padding: 10px;
}
<div class="navbar">
  <a href="#">Home</a>
  <a href="#">About</a>
  <a href="#">Services</a>
  <a href="#">Contact</a>
</div>

Here, justify-content: space-between; ensures even spacing between links.


CSS Grid Basics: Creating Complex Layouts with Ease πŸ“Š

What is CSS Grid?

CSS Grid Layout is a two-dimensional system that allows you to position items both in rows and columns. Unlike Flexbox, which focuses on one direction, Grid provides a structured layout for web pages.

Think of Grid as a spreadsheet with multiple rows and columns, where elements are placed into specific cells.

Setting Up CSS Grid

To create a grid layout, apply display: grid; to a container:

.grid-container {
  display: grid;
  grid-template-columns: 100px 100px 100px;
  grid-template-rows: 100px 100px;
  gap: 10px;
}
<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>

This creates a structured grid layout.

CSS Grid Properties: Spreadsheet Rules πŸ“

1️⃣ grid-template-columns & grid-template-rows

Defines how many columns and rows exist.

.grid-container {
  grid-template-columns: 1fr 2fr 1fr;
}

2️⃣ grid-column & grid-row – Expanding Cells 🏒

Expands an element across multiple columns or rows (like merging spreadsheet cells).

.grid-item:nth-child(1) {
  grid-column: span 2;
}

3️⃣ grid-area – Assigning Named Blocks πŸ“¦

.grid-container {
  grid-template-areas:
    "header header"
    "sidebar content";
}

.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.content { grid-area: content; }

Example: Website Layout using Grid 🌍

.container {
  display: grid;
  grid-template-areas:
    "header header"
    "sidebar main"
    "footer footer";
  grid-template-columns: 1fr 3fr;
}

.header { grid-area: header; background: blue; }
.sidebar { grid-area: sidebar; background: gray; }
.main { grid-area: main; background: white; }
.footer { grid-area: footer; background: black; }
<div class="container">
  <div class="header">Header</div>
  <div class="sidebar">Sidebar</div>
  <div class="main">Main Content</div>
  <div class="footer">Footer</div>
</div>

Flexbox vs. Grid: When to Use Which? πŸ€”

FeatureFlexboxGrid
Layout TypeOne-dimensionalTwo-dimensional
Best forNavigation bars, alignmentComplex layouts
Row & Column ControlOne at a timeBoth simultaneously
ExampleMenus, buttonsFull webpage layouts


Conclusion: Designing the Perfect Spreadsheet Web Layout πŸ“Š

By mastering Flexbox and Grid, you gain full control over web layoutsβ€”just like designing a well-organized spreadsheet.

β€’ Use Flexbox for dynamic, row-based layouts.

β€’ Use Grid for structured, multi-column layouts.

Now, go build beautiful, responsive layouts like a pro! πŸš€βœ¨

10
Subscribe to my newsletter

Read articles from Ayush Agarwal directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ayush Agarwal
Ayush Agarwal