CSS Grid Advanced

Grid Item Spanning (Important)

By default, each grid item takes 1 column and 1 row.

But we can make items span multiple columns or rows.

Example:

.grid-container {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 20px;
}

.box1 {
    grid-column: 1 / 3;
}

πŸ‘‰ This means:

  • Start at column line 1
  • End at column line 3

So it spans 2 columns

Spanning Rows

.box2 {
    grid-row: 1 / 3;
}

Now this item spans 2 rows.

Using span Keyword (Cleaner Way)

Instead of writing:

grid-column: 1 / 3;

You can write:

grid-column: span 2;

This means:

  • Take 2 columns from current position

Much cleaner and beginner friendly.

grid-template-areas (Very Powerful πŸ”₯)

This is where Grid becomes amazing.

CSS

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

Now assign areas:

.header {
    grid-area: header;
}

.sidebar {
    grid-area: sidebar;
}

.content {
    grid-area: content;
}

.footer {
    grid-area: footer;
}

This creates a full website layout easily.

Real Website Layout Example

HTML

<div class="grid-container">
    <div class="header">Header</div>
    <div class="sidebar">Sidebar</div>
    <div class="content">Main Content</div>
    <div class="footer">Footer</div>
</div>

This is how modern layouts are built.

Auto-fill & Auto-fit (Professional Level)

grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));

What this does:

  • Minimum width = 200px
  • Maximum = 1fr
  • Automatically adjusts based on screen size

πŸ”₯ This is very powerful for responsive design.

grid-auto-flow (Advanced Placement Control)

By default, CSS Grid places items row by row.

But you can control this behavior using grid-auto-flow.

.grid-container {
display: grid;
grid-auto-flow: row;
}

Values:

  • row β†’ Default (fills rows first)
  • column β†’ Fills columns first
  • dense β†’ Fills empty gaps automatically

Example:

.grid-container {
grid-auto-flow: dense;
}

πŸ‘‰ dense tries to fill gaps created by larger items.
This is useful in dynamic layouts like image galleries.

justify-content vs justify-items (Important Difference)

These properties are often confusing for beginners.

justify-items

Controls alignment of items inside each grid cell.

.grid-container {
justify-items: center;
}

justify-content

Controls alignment of the entire grid inside the container.

.grid-container {
justify-content: center;
}

Quick Difference

PropertyWorks On
justify-itemsItems inside cells
justify-contentWhole grid

πŸ‘‰ Use justify-items for item alignment
πŸ‘‰ Use justify-content for grid positioning

place-items (Shortcut Property)

Instead of writing multiple alignment properties, you can use place-items.

.grid-container {
place-items: center;
}

πŸ‘‰ This is equivalent to:

align-items: center;
justify-items: center;

This makes your code shorter and cleaner.

Auto-Fill vs Auto-Fit (Deep Understanding)

You already used auto-fit, but let’s understand both properly.

grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));

Difference:

  • auto-fit
    β†’ Expands items to fill available space
  • auto-fill
    β†’ Keeps empty columns even if space is available

πŸ‘‰ In most real projects, auto-fit is preferred because it creates flexible layouts.

Common Mistakes (Avoid These)

  • ❌ Using Grid for simple alignment (use Flexbox instead)
  • ❌ Not understanding grid lines ( Leads to wrong placement )
  • ❌ Overusing fixed sizes (like px) ( Breaks responsiveness )
  • ❌ Ignoring minmax() ( Layout becomes less flexible )
  • ❌ Mixing up justify-items and justify-content
  • ❌ Not using gap and manually adding margins

Important Points to Remember

  • CSS Grid is two-dimensional (rows + columns)
  • Use fr units for flexible layouts
  • Use repeat() to write cleaner code
  • Use span when you don’t want to depend on exact grid lines
  • Combine Grid + Flexbox for best results
  • Use auto-fit + minmax() for responsive design
  • Use grid-template-areas for clean layout structure
  • Practice real layouts to truly understand Grid

🎯 Practice Task

Task 1: Grid Item Spanning

Create a grid with 3 equal columns using:

grid-template-columns: repeat(3, 1fr);

Now:

  • Make the first item span 2 columns
  • Make the second item span 2 rows

πŸ‘‰ Hint:
Use grid-column and grid-row properties with span.

Task 2: Using span Keyword

Create a grid and try both methods:

grid-column: 1 / 3;

and

grid-column: span 2;

πŸ‘‰ Observe:

  • How both give the same result
  • Which one feels easier to use

Task 3: Website Layout Using Grid Areas

Create a full layout with:

  • Header
  • Sidebar
  • Content
  • Footer

Use:

grid-template-areas

πŸ‘‰ Make sure:

  • Header and footer take full width
  • Sidebar stays on left
  • Content takes remaining space

Task 4: Responsive Grid with auto-fit

Create a card layout:

grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));

πŸ‘‰ Add at least 6–8 cards

Try resizing screen:

  • Cards should automatically adjust
  • No horizontal scroll should appear

Task 5: auto-fill vs auto-fit (Experiment Task)

Create two grids:

Grid 1:

repeat(auto-fit, minmax(200px, 1fr))

Grid 2:

repeat(auto-fill, minmax(200px, 1fr))

πŸ‘‰ Observe:

  • How empty space behaves
  • Which layout looks better

Task 6: Alignment Practice

Create a grid and apply:

justify-items: center;
align-items: center;

Then try:

justify-content: center;
align-content: center;

πŸ‘‰ Understand the difference between:

  • Item alignment
  • Grid alignment

Task 7: Dense Layout (Advanced)

Create a grid with different sized items.

Then apply:

grid-auto-flow: dense;

πŸ‘‰ Observe:

  • How empty gaps are filled automatically

Task 8: Mini Project – Blog Layout

Create a simple blog layout:

  • Header at top
  • Sidebar on left
  • Blog posts on right
  • Footer at bottom

πŸ‘‰ Inside content section:

  • Create multiple cards using Grid
  • Make it responsive

Task 9: Image Gallery (Real World Use Case)

Create a responsive image gallery:

grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));

πŸ‘‰ Requirements:

  • Equal spacing
  • Responsive layout
  • Images adjust automatically

Task 10: Combine Grid + Flexbox

Create a layout using Grid.

Inside one grid item:

  • Use Flexbox to align content (like navbar or buttons)

πŸ‘‰ This is how real-world layouts are built.

In the next tutorial, we’ll learn about Flexbox vs Grid – When to Use What?

Related Tutorials

  • CSS Introduction

    Introduction CSS stands for Cascading Style Sheets. It is used to style and design web pages created with HTML. While HTML is used to structure content, CSS is used to control how that content looks on the screen. With CSS, you can change colors, fonts, layouts, spacing, and overall appearance of a website. In this…

  • CSS Syntax & Selectors

    Introduction CSS syntax defines how styles are written and applied to HTML elements. Selectors are used to target specific elements on a webpage and apply styles to them. Understanding CSS syntax and selectors is essential for writing effective and clean CSS code. In this lesson, you will learn the basic structure of CSS and different…

  • Colors, Units & Backgrounds

    Introduction CSS allows you to control the appearance of web pages using colors, units, and background properties. These are essential for designing visually appealing and responsive websites. In this lesson, you will learn how to apply colors, use different units, and style backgrounds in CSS. What You’ll Learn These are core CSS skills used on…

  • CSS Borders & Box Model

    Introduction CSS borders and the box model are essential concepts for designing layouts and controlling spacing in web development. Every HTML element is treated as a rectangular box, and understanding how this box works helps you create clean and structured designs. In this lesson, you will learn how to style borders and understand the CSS…

  • CSS Display & Position

    Introduction CSS display and position properties control how elements are shown and placed on a web page. These properties are essential for creating layouts, aligning elements, and building modern web designs. In this lesson, you will learn how different display types work and how positioning helps control element placement. What You’ll Learn πŸ”Ή CSS Display…

  • CSS Flexbox

    Introduction CSS Flexbox is a powerful layout system used to design flexible and responsive layouts. It makes it easy to align items, distribute space, and create modern UI designs without using complex positioning or floats. Flexbox works on a one-dimensional layout (row or column) and is widely used in real-world web development. What You’ll Learn…

Leave a Reply

Your email address will not be published. Required fields are marked *