Lesson 3: Modern Layouts with Flexbox and CSS Grid

“Build responsive 1D and 2D web layouts with modern CSS tools.”

Lesson 3: Modern Layouts with Flexbox and CSS Grid

Layout Evolution in CSS

Historically, web developers structured page layouts using hacks like HTML tables and floats. Today, modern layouts are built using two native CSS modules: Flexbox (Flexible Box Layout) and CSS Grid.

These layout systems make it easy to position elements, align content, and build responsive designs that adapt to any screen size without relying on complex calculations.

---

CSS Flexbox (1D Layouts)

[Flexbox] is designed for one-dimensional layouts—either a single row or a single column. It excels at distributing space, aligning elements within navigation bars, and centering items horizontally and vertically.

Let's build a responsive header navigation bar using Flexbox.

#### Flexbox Navigation Code Example
<nav class="main-nav">
<div class="logo">FlexBrand</div>
<ul class="nav-links">
<li><a href="#">Home</a></li>
<li><a href="#">Features</a></li>
<li><a href="#">Contact</a></li>
</ul>
<button class="nav-cta">Sign In</button>
</nav>

.main-nav {
display: flex;
justify-content: space-between;
align-items: center;
background-color: #0f172a;
padding: 1rem 2rem;
color: #ffffff;
}

.nav-links {
display: flex;
list-style: none;
gap: 1.5rem; /* modern gap property for dynamic flex spaces */
}

.nav-links a {
color: #cbd5e1;
text-decoration: none;
transition: color 0.2s;

&:hover {
color: #38bdf8;
}
}

.nav-cta {
background-color: #38bdf8;
color: #0f172a;
border: none;
padding: 0.5rem 1rem;
border-radius: 0.25rem;
font-weight: 600;
cursor: pointer;
}

Visual Output: The navigation bar is distributed across a single row. The logo sits on the left side, the link cluster sits centered in the middle, and the sign-in action button aligns to the right. The elements remain aligned vertically, regardless of variations in height.

---

CSS Grid (2D Layouts)

While Flexbox manages single rows or columns, [CSS Grid] is built for two-dimensional layouts—handling rows and columns simultaneously. It is the best choice for styling complex page layouts, product showcases, and gallery grids.

Let's design a responsive multi-card feature grid.

#### CSS Grid Code Example
<section class="grid-container">
<div class="grid-card"><h3>Feature 1</h3><p>Modern grid layouts are fast to build.</p></div>
<div class="grid-card"><h3>Feature 2</h3><p>Automatically adjust column spans.</p></div>
<div class="grid-card"><h3>Feature 3</h3><p>Create clean gutters without manual margins.</p></div>
<div class="grid-card"><h3>Feature 4</h3><p>Perfect for responsive layouts.</p></div>
</section>

.grid-container {
display: grid;
/* Auto-fit columns based on screen width, scaling between 250px and 1 fraction of free space */
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 1.5rem;
padding: 2rem;
}

.grid-card {
background-color: #ffffff;
border: 1px solid #e2e8f0;
border-radius: 0.5rem;
padding: 1.5rem;
box-shadow: 0 1px 3px rgba(0,0,0,0.05);
}

.grid-card h3 {
margin-bottom: 0.5rem;
color: #1e293b;
}

.grid-card p {
color: #64748b;
font-size: 0.95rem;
}

Visual Output: On large desktop screens, this rule creates four columns of equal width. As the screen size shrinks, the grid automatically drops cards to new rows, shifting to two columns on tablets and a single column on mobile devices—all without requiring complex media queries.

---

Choosing Between Flexbox and Grid

  • Use this simple rule of thumb to decide which layout system to use:
  • Use Flexbox when you want to align elements in a single dimension (e.g., horizontal menu bars, vertical button stacks, centering a single element).
  • Use CSS Grid when you need to align elements across both dimensions simultaneously (e.g., card dashboards, photo galleries, entire web page layouts).

---

Frequently Asked Questions (FAQs)

#### Q1: Can I use Flexbox and CSS Grid together on the same page?
Absolutely. In fact, most modern websites combine both. You can use CSS Grid to structure your overall page layout (e.g., header, sidebar, main content area, footer) and then use Flexbox to align elements inside those individual sections.

#### Q2: What does 1fr mean in CSS Grid?
1fr represents one fractional unit of the free space available in the grid container. If you write grid-template-columns: 1fr 2fr 1fr, the middle column will receive twice as much of the available space as the outer two columns.

#### Q3: What is the benefit of using gap over margins for grid spacing?
The gap property applies spacing exclusively *between* grid or flex items. Unlike margin, it does not add unwanted space to the outer edges of your container, keeping your layout boundaries clean.

Shanawar AliFounder and developer at S Pro Coder, sharing practical coding and technology guides.