CSS Grid Layout

CSS Grid Layout is a powerful layout system that allows you to create complex, flexible grid-based designs with minimal effort. It provides a way to divide a page or container into rows and columns, making it easier to control the placement and alignment of elements.

Key Concepts of CSS Grid:

  1. Grid Container: The parent element that holds all the grid items.
  2. Grid Items: The child elements inside the grid container.
  3. Grid Tracks: The rows and columns of the grid.
  4. Grid Lines: The lines that separate rows and columns, referenced for positioning items.
  5. Grid Cells: The space between two grid lines, where grid items can be placed.
  6. Grid Areas: A named section of the grid that spans multiple rows and columns.

Basic Syntax

You define a grid container by setting the display property to grid (or inline-grid for inline grids). Then you can define rows and columns with grid-template-rows and grid-template-columns.

css
.container {
display: grid;
grid-template-columns: repeat(3, 1fr); /* 3 equal columns */
grid-template-rows: 100px auto 200px; /* 3 rows with different heights */
gap: 10px; /* space between grid items */
}

Defining Rows and Columns

You can specify the number and size of the rows and columns:

  • 1fr: Fractional unit. Divides available space into fractions.
  • px%emrem: Fixed or relative measurements.
  • auto: The size is determined by the content.

Example:

css
.container {
display: grid;
grid-template-columns: 200px 1fr 2fr; /* First column 200px, second takes 1 fraction, third takes 2 fractions */
grid-template-rows: auto 100px; /* First row auto-sized, second row 100px */
}

Grid Template Areas

You can visually divide the grid into areas with names and then assign items to those areas. This can make the layout more intuitive.

css
.container {
display: grid;
grid-template-columns: 1fr 2fr;
grid-template-rows: auto auto;
grid-template-areas:
"header header"
"main sidebar"
"footer footer";
}

.header {
grid-area: header;
}
.main {
grid-area: main;
}
.sidebar {
grid-area: sidebar;
}
.footer {
grid-area: footer;
}

  • In this example, the grid has named areas like headermainsidebar, and footer, and the items are placed in these areas using grid-area.

Placing Items in the Grid

You can place items in specific grid cells by using grid line numbers or grid areas.

1. Using Grid Line Numbers

You can specify the starting and ending points for grid items using line numbers. The lines are numbered from 1 to N (for both rows and columns).

css
.item {
grid-column: 1 / 3; /* Starts at line 1, ends at line 3 (spanning 2 columns) */
grid-row: 2 / 4; /* Starts at row 2, ends at row 4 (spanning 2 rows) */
}

2. Using Grid Areas

You can also use grid areas to position items.

css
.item {
grid-area: header; /* Places the item in the 'header' grid area */
}

Responsive Grid Layouts

Grid layouts are great for building responsive designs. You can use media queries to change the grid structure for different screen sizes.

Example:

css
.container {
display: grid;
grid-template-columns: 1fr 1fr; /* 2 columns by default */
gap: 20px;
}

@media (max-width: 768px) {
.container {
grid-template-columns: 1fr; /* 1 column on smaller screens */
}
}

In this example, on larger screens, the grid will have two columns, and on smaller screens, it will change to a single column.

Example: Full Grid Layout

Let’s build a simple layout with a header, sidebar, main content area, and footer using grid.

html
<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>
css
.container {
display: grid;
grid-template-columns: 1fr 3fr; /* 2 columns: first 1fr, second 3fr */
grid-template-rows: auto 1fr auto; /* 3 rows: first and third are auto-sized */
gap: 10px;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
}

.header {
grid-area: header;
background-color: lightblue;
}

.sidebar {
grid-area: sidebar;
background-color: lightgray;
}

.main {
grid-area: main;
background-color: lightgreen;
}

.footer {
grid-area: footer;
background-color: lightcoral;
}

This will create a layout like:

  • Header at the top (spanning both columns),
  • Sidebar on the left (in the first column),
  • Main Content on the right (in the second column),
  • Footer at the bottom (spanning both columns).

CSS Grid vs. Flexbox

  • Grid is better for two-dimensional layouts (rows and columns).
  • Flexbox is ideal for one-dimensional layouts (either rows or columns).

You can also combine both methods in one layout, depending on the complexity and needs of your design.

Grid Line Numbering (Implicit vs Explicit Grid)

  • Explicit Grid: Defined with grid-template-columns and grid-template-rows (what we covered above).
  • Implicit Grid: If you place an item outside the defined grid, CSS Grid will automatically create additional rows or columns to accommodate it.

Example:

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

.item {
grid-column: 1 / 4; /* This will create a 3-column grid because the item spans beyond the defined grid */
}

Conclusion

CSS Grid Layout is an incredibly powerful tool for building complex layouts. By using grid containers, grid tracks, lines, and areas, you can create flexible, responsive designs with less effort. It’s perfect for building both simple and complex layouts, especially when you need precise control over row and column behavior.

Let me know if you’d like to see more specific examples or dive deeper into any part of the CSS Grid system!

Share:

More Posts

Browser DevTools Tips

Browser DevTools are a powerful set of tools that allow developers to inspect, debug, and optimize web pages. Whether you’re a front-end developer or someone

ES6 Features

ES6 (ECMAScript 2015) introduced a number of significant updates to JavaScript, which made the language more powerful, easier to work with, and more consistent. Here’s

Responsive Design

Responsive design is a web design approach aimed at creating websites that provide optimal viewing and interaction experiences across a wide range of devices. Whether

Express Routing

Express Routing is a powerful feature of the Express.js framework, a popular web application framework for Node.js. It provides a way to define how your

SCSS Nesting

SCSS Nesting is a feature in Sass (Syntactically Awesome Style Sheets), which is a CSS preprocessor that extends CSS with features like variables, mixins, inheritance,

API Rate Limiting

API rate limiting is a technique used to control the amount of incoming requests to a server or service over a specific period of time.

CSS Animations

CSS animations allow you to animate the transitions of various properties, like colors, sizes, positions, or even more complex transformations. They can be keyframed or

SQL Joins Explained

SQL JOINS are used to combine rows from two or more tables based on a related column between them. They allow you to retrieve data

Semantic HTML

This post provides a brief overview of Semantic HTML in modern web development. More detailed content can be added here.

CSS Pseudo Elements

This post provides a brief overview of CSS Pseudo Elements in modern web development. More detailed content can be added here.

Progressive Web Apps

This post provides a brief overview of Progressive Web Apps in modern web development. More detailed content can be added here.

Axios vs Fetch

This post provides a brief overview of Axios vs Fetch in modern web development. More detailed content can be added here.

Babel Explained

This post provides a brief overview of Babel Explained in modern web development. More detailed content can be added here.

Async/Await in JS

This post provides a brief overview of Async/Await in JS in modern web development. More detailed content can be added here.

Fetch API Usage

This post provides a brief overview of Fetch API Usage in modern web development. More detailed content can be added here.

Firebase Authentication

This post provides a brief overview of Firebase Authentication in modern web development. More detailed content can be added here.

GraphQL Intro

This post provides a brief overview of GraphQL Intro in modern web development. More detailed content can be added here.

LocalStorage vs SessionStorage

This post provides a brief overview of LocalStorage vs SessionStorage in modern web development. More detailed content can be added here.

Nuxt.js Intro

This post provides a brief overview of Nuxt.js Intro in modern web development. More detailed content can be added here.

Tailwind CSS Basics

This post provides a brief overview of Tailwind CSS Basics in modern web development. More detailed content can be added here.

JS Unit Testing

This post provides a brief overview of JS Unit Testing in modern web development. More detailed content can be added here.

REST API Design

This post provides a brief overview of REST API Design in modern web development. More detailed content can be added here.

Web Accessibility (a11y)

This post provides a brief overview of Web Accessibility (a11y) in modern web development. More detailed content can be added here.

Microservices Overview

This post provides a brief overview of Microservices Overview in modern web development. More detailed content can be added here.

Service Workers

This post provides a brief overview of Service Workers in modern web development. More detailed content can be added here.

DOM Manipulation

This post provides a brief overview of DOM Manipulation in modern web development. More detailed content can be added here.

Intro to TypeScript

This post provides a brief overview of Intro to TypeScript in modern web development. More detailed content can be added here.

Authentication in Web Apps

This post provides a brief overview of Authentication in Web Apps in modern web development. More detailed content can be added here.

Svelte for Beginners

This post provides a brief overview of Svelte for Beginners in modern web development. More detailed content can be added here.

JavaScript Closures

This post provides a brief overview of JavaScript Closures in modern web development. More detailed content can be added here.

Vue Directives

This post provides a brief overview of Vue Directives in modern web development. More detailed content can be added here.

Node.js Modules

This post provides a brief overview of Node.js Modules in modern web development. More detailed content can be added here.

HTML Basics

This post provides a brief overview of HTML Basics in modern web development. More detailed content can be added here.

OAuth 2.0 Basics

This post provides a brief overview of OAuth 2.0 Basics in modern web development. More detailed content can be added here.

JWT Authentication

This post provides a brief overview of JWT Authentication in modern web development. More detailed content can be added here.

Flexbox Tricks

This post provides a brief overview of Flexbox Tricks in modern web development. More detailed content can be added here.

CORS Explained

This post provides a brief overview of CORS Explained in modern web development. More detailed content can be added here.

SEO for Developers

This post provides a brief overview of SEO for Developers in modern web development. More detailed content can be added here.

WebSockets Overview

This post provides a brief overview of WebSockets Overview in modern web development. More detailed content can be added here.

React Hooks

This post provides a brief overview of React Hooks in modern web development. More detailed content can be added here.

Debounce vs Throttle

This post provides a brief overview of Debounce vs Throttle in modern web development. More detailed content can be added here.

Bootstrap Grid System

This post provides a brief overview of Bootstrap Grid System in modern web development. More detailed content can be added here.

Next.js Routing

This post provides a brief overview of Next.js Routing in modern web development. More detailed content can be added here.

Webpack Basics

This post provides a brief overview of Webpack Basics in modern web development. More detailed content can be added here.

SASS Variables

This post provides a brief overview of SASS Variables in modern web development. More detailed content can be added here.

Cookies vs Tokens

This post provides a brief overview of Cookies vs Tokens in modern web development. More detailed content can be added here.

MongoDB Queries

This post provides a brief overview of MongoDB Queries in modern web development. More detailed content can be added here.

HTML5 Forms

This post provides a brief overview of HTML5 Forms in modern web development. More detailed content can be added here.

Web Security Tips

This post provides a brief overview of Web Security Tips in modern web development. More detailed content can be added here.

Ultimate Web Development Roadmap

Ultimate Web Development Roadmap

Ultimate Web Development Roadmap: Step-by-Step Guide to Building Modern, Scalable Websites Introduction Web technology changes fast. New tools and best practices appear all the time.

Web Development future in 2026

Web Development future in 2026

So yeah, I’ve been thinking a lot about where web development is heading. Not in the “buzzwordy LinkedIn post” kind of way, but more like…

Send Us A Message