Jomatter

CSS Grid Playground: Practical Examples for Building Website Layouts

CSS Grid is a powerful CSS layout system that helps developers create structured, responsive website layouts using rows and columns. It makes it easier to organize content, position elements, control spacing, and build complex designs without relying on excessive positioning or complicated CSS calculations.

A CSS Grid playground provides a practical way to experiment with these properties and understand how different values affect a layout. By changing column sizes, adjusting gaps, spanning grid items, and testing responsive settings, developers can learn how Grid works through real examples.

This guide explores the fundamental CSS Grid properties and demonstrates practical layouts you can copy, modify, and test in your own HTML and CSS projects.

What Is CSS Grid?

CSS Grid is a two-dimensional layout system that allows developers to arrange elements along horizontal rows and vertical columns.

Unlike traditional layouts that often require floats, positioning, or multiple nested containers, CSS Grid provides direct control over how elements occupy available space.

You can use it to create:

  • Website headers, sidebars, and main content areas.
  • Responsive product and service cards.
  • Image galleries and portfolio layouts.
  • Dashboard interfaces.
  • Magazine-style content sections.
  • Complex page structures with different column sizes.

A Grid layout starts by defining a container with display: grid. Its direct children automatically become grid items that can be positioned within the available rows and columns.

How to Start a CSS Grid Playground

The easiest way to experiment with CSS Grid is to create a basic HTML document and apply different Grid properties to its container.

You can use an online code editor or create an HTML file on your computer.

Start with the following HTML:

<!DOCTYPE html>

<html lang=”en”>

<head>

    <meta charset=”UTF-8″>

    <meta name=”viewport” content=”width=device-width, initial-scale=1.0″>

    <title>CSS Grid Playground</title>

    <link rel=”stylesheet” href=”style.css”>

</head>

<body>

    <h1>My CSS Grid Playground</h1>

    <div class=”grid-container”>

        <div class=”grid-item”>Item 1</div>

        <div class=”grid-item”>Item 2</div>

        <div class=”grid-item”>Item 3</div>

        <div class=”grid-item”>Item 4</div>

        <div class=”grid-item”>Item 5</div>

        <div class=”grid-item”>Item 6</div>

    </div>

</body>

</html>

Add the following CSS to style.css:

body {

    font-family: Arial, sans-serif;

    padding: 20px;

}

.grid-container {

    display: grid;

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

    gap: 15px;

}

.grid-item {

    background: #e6efff;

    padding: 30px;

    text-align: center;

    border: 1px solid #8caee8;

    border-radius: 6px;

}

Save both files in the same directory and open the HTML file in your browser.

You will see six items arranged into three equal-width columns. Each item occupies an available grid cell, and the gap property creates space between the items.

Now you can modify the CSS properties to see how the layout changes.

Important CSS Grid Properties to Experiment With

Understanding the main Grid properties makes it easier to build different layouts.

1. display: grid

The display: grid property turns an HTML element into a grid container.

.container {

    display: grid;

}

Its direct children become grid items and participate in the Grid layout.

Without this property, the container does not establish a CSS Grid formatting context.

2. grid-template-columns

The grid-template-columns property defines the number and size of the columns in an explicit grid.

For example:

.container {

    display: grid;

    grid-template-columns: 200px 300px 200px;

}

This creates three columns with widths of 200px, 300px, and 200px.

You can also use the fr unit to distribute available space proportionally.

.container {

    display: grid;

    grid-template-columns: 1fr 2fr 1fr;

}

In this example, the middle column receives twice the flexible share of either outer column.

3. grid-template-rows

The grid-template-rows property controls the height of explicitly defined rows.

.container {

    display: grid;

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

    grid-template-rows: 100px 200px;

}

This creates two columns and two explicitly sized rows.

You can use auto when the row should accommodate its content instead of using a fixed height.

4. gap

The gap property defines the spacing between grid rows and columns.

.container {

    display: grid;

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

    gap: 20px;

}

You can also control horizontal and vertical spacing independently:

.container {

    column-gap: 25px;

    row-gap: 15px;

}

This is particularly useful when building card layouts or image galleries.

5. repeat() and minmax()

The repeat() function simplifies repetitive track definitions, while minmax() sets minimum and maximum track sizes.

.container {

    display: grid;

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

}

This defines three columns, each with a minimum width of 150px and a flexible maximum size.

These functions become especially useful when creating responsive layouts.

CSS Grid Playground: Practical Examples

The following examples demonstrate how CSS Grid can be used for common website design requirements. You can copy each example into your project and experiment with the values.

Example 1: Create a Responsive Card Layout

A responsive card layout is useful for displaying products, blog posts, services, or portfolio projects.

Instead of manually creating separate layouts for every screen size, you can use auto-fit and minmax() to let CSS Grid calculate how many columns can fit.

HTML:

<div class=”card-grid”>

    <div class=”card”>

        <h3>Web Design</h3>

        <p>Create clean and responsive website layouts.</p>

    </div>

    <div class=”card”>

        <h3>Development</h3>

        <p>Build functional websites using modern technologies.</p>

    </div>

    <div class=”card”>

        <h3>SEO Services</h3>

        <p>Improve website visibility in search engines.</p>

    </div>

    <div class=”card”>

        <h3>Digital Marketing</h3>

        <p>Reach potential customers through digital channels.</p>

    </div>

</div>

CSS:

.card-grid {

    display: grid;

    grid-template-columns:

        repeat(auto-fit, minmax(min(100%, 15rem), 1fr));

    gap: 20px;

}

.card {

    padding: 20px;

    background: #f4f4f4;

    border-radius: 8px;

    border: 1px solid #ddd;

}

How it works:

The auto-fit keyword automatically adjusts the number of columns based on available space. The minmax() function establishes a minimum card width while allowing columns to expand.

The result is a flexible layout that can display multiple columns on wider screens and fewer columns on narrower screens without requiring a separate media query for every column count.

Example 2: Build a Website Layout Using Grid Areas

CSS Grid also makes it possible to create complete website layouts using named grid areas.

This approach is useful when a page contains a header, sidebar, main content section, and footer.

HTML:

<div class=”website-layout”>

    <header class=”header”>

        Website Header

    </header>

    <aside class=”sidebar”>

        Sidebar Navigation

    </aside>

    <main class=”main-content”>

        Main Website Content

    </main>

    <footer class=”footer”>

        Website Footer

    </footer>

</div>

CSS:

.website-layout {

    display: grid;

    grid-template-columns: 16rem minmax(0, 1fr);

    grid-template-areas:

        “header header”

        “sidebar main”

        “footer footer”;

    gap: 15px;

}

.header {

    grid-area: header;

    background: #dceaff;

    padding: 20px;

}

.sidebar {

    grid-area: sidebar;

    background: #e8e8e8;

    padding: 20px;

}

.main-content {

    grid-area: main;

    background: #f4f4f4;

    padding: 20px;

}

.footer {

    grid-area: footer;

    background: #dceaff;

    padding: 20px;

}

@media (max-width: 700px) {

    .website-layout {

        grid-template-columns: minmax(0, 1fr);

        grid-template-areas:

            “header”

            “sidebar”

            “main”

            “footer”;

    }

}

How it works:

The grid-template-areas property assigns names to different sections of the layout. Individual elements use grid-area to occupy their assigned positions.

On wider screens, the sidebar and main content appear next to each other. On smaller screens, the layout changes to a single column.

This method makes complex page layouts easier to understand because the overall structure is visible directly in the CSS.

Example 3: Create an Image Gallery with Different Grid Sizes

CSS Grid can create attractive image galleries where some items occupy more space than others.

The grid-column and grid-row properties allow individual elements to span multiple tracks.

HTML:

<div class=”gallery”>

    <div class=”gallery-item featured”>Featured Image</div>

    <div class=”gallery-item”>Image 2</div>

    <div class=”gallery-item”>Image 3</div>

    <div class=”gallery-item”>Image 4</div>

    <div class=”gallery-item”>Image 5</div>

</div>

CSS:

.gallery {

    display: grid;

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

    grid-auto-rows: minmax(120px, auto);

    gap: 12px;

}

.gallery-item {

    background: #dce8f8;

    display: grid;

    place-items: center;

    padding: 20px;

    border-radius: 8px;

}

.featured {

    grid-column: span 2;

    grid-row: span 2;

}

@media (max-width: 600px) {

    .gallery {

        grid-template-columns: repeat(2, minmax(0, 1fr));

    }

    .featured {

        grid-column: span 2;

        grid-row: span 1;

    }

}

How it works:

The featured item occupies two columns and two rows on wider screens. Other items automatically fill the remaining available grid cells.

On smaller screens, the gallery switches to two columns and the featured item occupies a single row.

You can change the spanning values to experiment with different gallery arrangements.

Example 4: Align Grid Items in the Center

CSS Grid includes alignment properties that control where items appear inside their grid areas.

For example, place-items is a shorthand for align-items and justify-items.

HTML:

<div class=”alignment-grid”>

    <div>Box 1</div>

    <div>Box 2</div>

    <div>Box 3</div>

    <div>Box 4</div>

</div>

CSS:

.alignment-grid {

    display: grid;

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

    gap: 15px;

    place-items: center;

    min-height: 250px;

}

.alignment-grid div {

    width: 80%;

    padding: 25px;

    text-align: center;

    background: #dceaff;

}

How it works:

The place-items: center declaration centers each grid item within its assigned grid area along both alignment axes.

You can experiment with other values such as start, end, and stretch to see how item alignment changes.

How to Make CSS Grid Layouts Responsive

Responsive design ensures that website elements adjust to different screen sizes without creating horizontal scrolling or overlapping content.

CSS Grid provides several ways to build responsive layouts.

Use auto-fit for Flexible Columns

A common pattern is:

.container {

    display: grid;

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

    gap: 20px;

}

This creates as many columns as can fit, with a minimum track width of 220px.

When fewer items are present than available columns, auto-fit collapses empty repeated tracks so occupied tracks can expand.

For extremely narrow containers, consider using min(100%, 220px) as the minimum track size to prevent overflow.

Use Media Queries for Complex Layout Changes

Sometimes automatic columns are not enough. A website may need a sidebar on desktop but a stacked content layout on mobile.

You can use media queries to redefine the grid:

.container {

    display: grid;

    grid-template-columns: 250px 1fr;

    gap: 20px;

}

@media (max-width: 768px) {

    .container {

        grid-template-columns: 1fr;

    }

}

This changes the layout from two columns to one column when the viewport becomes narrower than 768px.

Use minmax() to Prevent Content Overflow

Fixed dimensions can cause problems when content becomes longer than expected.

For example:

.container {

    display: grid;

    grid-template-columns: minmax(0, 1fr) 2fr;

}

The minmax() function allows the first track to shrink to zero when necessary while maintaining a flexible maximum size.

In practical layouts, this can help prevent long text or wide content from forcing a grid column beyond its available space.

CSS Grid vs. Flexbox: Which Should You Use?

CSS Grid and Flexbox are both useful layout systems, but they solve slightly different problems.

FeatureCSS GridCSS Flexbox
Layout directionTwo-dimensionalPrimarily one-dimensional
Rows and columnsControls bothControls one main axis at a time
Item placementSupports row and column positioningUses flexible item distribution
Best suited forPage layouts, galleries, dashboardsNavigation bars, buttons, aligned components
SpanningItems can span multiple rows and columnsNo equivalent grid-track spanning
Nested layoutsSupports nested grids and subgridSupports nested flex containers

Use CSS Grid when you need to control both rows and columns together. Use Flexbox when you mainly need to distribute or align elements along a single direction.

You can also combine them. For example, use Grid for a product listing and Flexbox inside each product card.

Common CSS Grid Problems and How to Fix Them

Even though CSS Grid simplifies layout development, some common problems can occur when experimenting with different properties.

Grid Items Are Not Appearing in the Expected Positions

Grid automatically places items into available cells when explicit positions are not defined.

If you want an element to occupy a specific location, use grid-column, grid-row, or named grid areas.

Also remember that only direct children of a grid container become grid items.

Columns Are Wider Than Expected

Long text, images, or other content can create unwanted overflow.

Try using:

.container {

    grid-template-columns: repeat(2, minmax(0, 1fr));

}

.grid-item {

    min-width: 0;

    overflow-wrap: anywhere;

}

.grid-item img {

    max-width: 100%;

}

This allows flexible tracks and items to shrink while helping long text and images fit within their available space.

Grid Rows Have Unexpected Heights

When the number of grid rows is not explicitly defined, CSS automatically creates additional rows when needed. These are called implicit grid tracks.

You can control their sizing with grid-auto-rows.

.container {

    display: grid;

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

    grid-auto-rows: minmax(100px, auto);

}

This gives automatically generated rows a minimum height of 100px while allowing them to expand to accommodate content.

Named Grid Areas Are Not Working

The grid-template-areas property requires a valid rectangular grid definition.

Every row must contain the same number of cells, and named areas must form rectangular shapes. Use a period (.) to represent an empty cell.

Check the spelling of each area name and make sure the corresponding element uses the correct grid-area value.

Visual Order Does Not Match Keyboard Navigation

CSS Grid can visually reposition elements without changing their document order.

This means that screen readers and keyboard navigation may still follow the original HTML sequence.

Keep the HTML in a logical reading order and use CSS Grid primarily for visual layout rather than rearranging the meaning or reading sequence of content.

How to Experiment with CSS Grid Using Browser Developer Tools

Browser developer tools make it easier to understand how CSS Grid works without repeatedly changing your HTML.

In Chrome, you can inspect a grid container and examine its layout through the Elements panel.

A useful workflow is:

  1. Open your HTML page in Chrome.
  2. Right-click the grid container and select Inspect.
  3. Find the element containing display: grid.
  4. Use the Grid badge or layout controls in Developer Tools to display grid overlays.
  5. Inspect the column and row boundaries.
  6. Modify properties such as grid-template-columns, gap, and grid-auto-rows.
  7. Observe how the items respond to the changes.

You can also resize the browser window to test whether your responsive Grid settings work correctly.

This approach helps you understand the relationship between CSS properties and the actual layout displayed by the browser.

Frequently Asked Questions

What is a CSS Grid playground used for?

A CSS Grid playground provides an environment for experimenting with rows, columns, gaps, alignment, and responsive layouts. It helps developers understand Grid properties through practical examples.

Is CSS Grid better than Flexbox?

Neither is universally better. CSS Grid is suitable for two-dimensional layouts, while Flexbox is generally more appropriate for arranging items along one direction.

Can CSS Grid create responsive layouts without media queries?

Yes. Properties such as repeat(), auto-fit, and minmax() can create flexible layouts that adapt automatically to available space. More complex design changes may still require media queries.

What does the fr unit mean in CSS Grid?

The fr unit represents a fraction of the available flexible space in a grid container. For example, 1fr 2fr distributes flexible space in a one-to-two ratio.

What is the difference between auto-fit and auto-fill?

Both keywords create as many repeated tracks as can fit. However, auto-fit collapses empty repeated tracks after item placement, while auto-fill preserves those empty tracks.

Can CSS Grid be used inside another Grid layout?

Yes. A grid item can also become a grid container by using display: grid. For more advanced layouts, subgrid allows nested grids to use the parent’s defined row or column tracks.

Conclusion

A CSS Grid playground is a useful learning environment for understanding modern website layout techniques. By experimenting with properties such as grid-template-columns, grid-template-rows, gap, minmax(), and grid-template-areas, developers can create flexible layouts for different website components.

Start with simple equal-width columns, then experiment with responsive cards, named grid areas, image galleries, and alignment settings. Use browser developer tools to inspect the grid and understand how individual properties affect its structure.

With regular practice, CSS Grid makes it easier to build organized, responsive, and maintainable website layouts without relying on complicated positioning techniques.

Must Read This Ones

Using CSS Formatter for Large Stylesheets

How to Organize CSS for Better Maintainability

Common CSS Mistakes That Break Layouts

How to Create Responsive Layouts with Flexbox

CSS Grid Playground: Practical Examples

How to Optimize CSS for Performance

Difference Between Inline, Internal and External CSS

How to Debug CSS Layout Issues Quickly

Best Way to Generate Complex Gradients

How to Keep CSS Code Clean and Readable