Design+Code logo

Quick links

Suggested search

illustration

Grid Layout

icon

Add to favorites

The best way to create a beautiful and responsive layout

CodeSandbox link

You can find the full code for this tutorial at https://codesandbox.io/s/grid-layout-l472n.

Layout

The first method we tried was tables, positioning, and inline-block, but all of these methods were essentially hacks, leaving out a lot of important functionality. Flexbox is also a very good layout tool, but it is one-directional. We've had to hack our way around layout problems for as long as we've been building websites, so Grid was the first module created specifically to fix them.

Display

A container element must be defined as a grid.

display: grid;

Template

Columns and rows of the grid are defined by space-separated values. The value can be a length, a percentage or a fraction.

grid-template-columns: ...  ...;

grid-template-rows: ... ...;

Spacing

Sets the grid line size. This is similar to setting the gutter width between columns/rows.

column-gap: 20x;
row-gap: 20x;
gap: 20px;

Container

Let's resize the browser container and you can see that it takes up the entire width of the browser. You should normally set a maximum size for your container so that it will adapt to all screen sizes.

max-width: 1200px;
margin: 0 auto;

Justify Content

This property sets how the grid will be aligned within the grid container horizontally.

  • start – aligns the grid to the left
  • end – aligns the grid to the right
  • center – aligns the grid in the center
  • space-evenly – places an even amount of space between each grid item, including the far ends
  • space-around – places an even amount of space between each grid item, with half-sized spaces on the far ends
  • space-between – places an even amount of space between each grid item, with no space at the far ends

Align Content

This property sets how the grid will be aligned within the grid container vertically.

  • start – aligns the grid to the top
  • end – aligns the grid to the bottom
  • center – aligns the grid in the center
  • space-evenly – places an even amount of space between each grid item, including the far ends
  • space-around – places an even amount of space between each grid item, with half-sized spaces on the far ends
  • space-between – places an even amount of space between each grid item, with no space at the far ends

Place Content

Using place-content will shorten your code. The first value is align-content, while the second value is justify-content.

place-content: center space-between;

Justify Items

This property sets how the items will be aligned within the grid horizontally.

  • start – aligns the item to the left
  • end – aligns the item to the right
  • center – aligns the item to the center

Align Items

This property sets how the items will be aligned within the grid vertically.

  • start – aligns the item to the top
  • end – aligns the item to the bottom
  • center – aligns the item to the center

Place Items

Using place-items will shorten your code. The first value is align-items, while the second value is justify-items.

place-items: center;

Responsive Columns

Let's adapt the grid now that you know how it works.

grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));

Learn with videos and source files. Available to Pro subscribers only.

Purchase includes access to 50+ courses, 320+ premium tutorials, 300+ hours of videos, source files and certificates.

BACK TO

Styled Components

READ NEXT

Media Query

Templates and source code

Download source files

Download the videos and assets to refer and learn offline without interuption.

check

Design template

check

Source code for all sections

check

Video files, ePub and subtitles

Assets

Videos

Subtitles

Meet the instructor

We all try to be consistent with our way of teaching step-by-step, providing source files and prioritizing design in our courses.

Willie Yam

Front-end/UI developer at Design+Code

I do UI coding. HTML/CSS/JS/SWIFTUI dev.

icon

10 courses - 37 hours

course logo

Design and Prototype an App with Play

Build a completely functional prototype without writing a single line of code from your phone

3 hrs

course logo

Create a 3D site with game controls in Spline

Build an interactive 3D scene implemented on a ReactJS site using Figma and Spline

2 hrs

course logo

Build a Movie Booking App in SwiftUI

Learn how to create an iOS app based on a beautiful UI design from Figma with interesting animations and interactions, create a custom tab bar and use navigation views to build a whole flow

1 hrs

course logo

Build Quick Apps with SwiftUI

Apply your Swift and SwiftUI knowledge by building real, quick and various applications from scratch

11 hrs

course logo

CSS Handbook

A comprehensive series of tutorials that encompass styled-components, CSS, and all layout and UI developments

1 hrs

course logo

Advanced React Hooks

Learn how to build a website with Typescript, Hooks, Contentful and Gatsby Cloud

5 hrs

course logo

Unity for Designers

If you want to make a game and don't know where to start, you are in the right place. I will teach you how to use Unity, code in C# and share essential tips and tricks to make your first game.

5 hrs

course logo

Create a Javascript Game

Learn how to create a web game using Phaser 3, a popular javascript game engine. Draw a map using an editor, implement the player, make the player move, apply physics, collisions, and implement the enemies.

2 hrs

course logo

Build an ARKit 2 App

Introduction to ARKit and learn how to make your own playground. You will be able to add models or even your own designs into the app and play with them

4 hrs

course logo

Create a SpriteKit Game

Overview of SpriteKit a powerful 2D sprite-based framework for games development from Apple and learn how to create your very own platform

3 hrs