How to Create a Grid in HTML & CSS – Simple Examples

Welcome to a quick tutorial on how to create a grid in HTML and CSS. Still using tables, CSS hacks, and complicated Javascript code to create grids? It’s time to stop.

To create a simple grid in HTML and CSS:

  1. Define the HTML container – <div class="grid"> <div>Cell</div> <div>Cell</div> </div>
  2. Create a grid container and specify the number of columns in CSS – .grid { display: grid; grid-template-columns: auto auto; }

Yes, that’s all it takes in modern HTML CSS to create a simple grid. But let us walk through more examples in this guide – Read on!

ⓘ I have included a zip file with all the example source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

TABLE OF CONTENTS

Download & Notes Creating Grids Useful Bits & Links
The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

EXAMPLE CODE DOWNLOAD

Click here to download the source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

QUICK NOTES

If you spot a bug, please feel free to comment below. I try to answer questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

 

CREATING GRIDS

Alright, let us now dive into more examples of creating grids in HTML and CSS.

 

1) GRID COLUMNS

 

THE HTML

1a-col.html
<div id="grid-col">
  <div class="cell">Cell</div>
  <div class="cell">Cell</div>
  <div class="cell">Cell</div>
  <div class="cell">Cell</div>
</div>

Yep, the HTML of a basic grid is as simple as that – Just create a <div id="grid-col"> container, and put the cells <div class="cell"> inside.

 

THE CSS

1b-col.css
/* (A) GRID CONTAINER */
#grid-col {
  display: grid;
  grid-template-columns: auto auto;

  /* WE CAN ALSO SPECIFY EXACT PIXELS, PERCENTAGE, REPEAT
  grid-template-columns: 200px 40%;
  grid-template-columns: repeat(2, 250px); */
}
 
/* (B) GRID CELLS */
div.cell {
  background: LightBlue;
  border: 1px solid Blue;
  padding: 10px;
}
 
/* (C) RESPONSIVE - 1 COLUMN ON SMALL SCREENS */
@media screen and (max-width: 640px) {
  #grid-col { grid-template-columns: 100%; }
}

This may seem complicated at first, but the raw essentials are:

  • Set a grid container #grid-col { display: grid }
  • Define the number of columns with grid-template-columns. Take note that auto will leave it to the browser to determine the width of the columns. We can also specify exact px or %.
  • Finally and optionally, we use @media media query to add responsive support for mobile devices. Change grid-template-columns so that there are fewer columns on a small screen.

 

 

2) GRID ROWS

 

THE HTML

2a-row.html
<div id="grid-row">
  <div class="cell">Cell</div>
  <div class="cell">Cell</div>
  <div class="cell">Cell</div>
  <div class="cell">Cell</div>
</div>

 

THE CSS

2b-row.css
/* (A) GRID CONTAINER */
#grid-row {
  display: grid;
  grid-template-columns: auto auto;
  /* NOTE: ROW 3 ONWARDS WILL BE AUTO HEIGHT */
  grid-template-rows: 50px 100px;
 
  /* SAME AS COLUMNS, WE CAN USE REPEAT AS WELL
  grid-template-rows: repeat(4, 50px); */
}
 
/* (B) GRID CELLS */
div.cell {
  background: LightBlue;
  border: 1px solid Blue;
  padding: 10px;
}

We can use grid-template-rows to control the height of each row. By default, the height of rows is set to auto. So don’t bother to define grid-template-rows unless you want to set a specific height for a certain row.

 

3) CELL GAPS

 

THE HTML

3a-gap.html
<div id="grid-gap">
  <div class="cell">Cell</div>
  <div class="cell">Cell</div>
  <div class="cell">Cell</div>
  <div class="cell">Cell</div>
</div>

 

THE CSS

3b-gap.css
/* (A) SET CONTAINER TO GRID */
#grid-gap {
  display: grid;
  grid-template-columns: auto auto;

  /* SET SPACING IN-BETWEEN CELLS */
  grid-column-gap: 10px;
  grid-row-gap: 30px;

  /* OR SIMPLY SET A UNIFORM SPACING
  grid-gap: 20px; */
}

/* (B) GRID CELLS */
div.cell {
  background: LightBlue;
  border: 1px solid Blue;
  padding: 10px;
}

Want to space out the cells?

  • Use grid-column-gap to control the spacing in-between cells horizontally.
  • Use grid-row-gap to control the spacing in-between cells horizontally.
  • Or simply apply a uniform spacing using grid-gap.

 

 

4) GRID LINES

THE BASIC IDEA

We have only created an “equal grid” so far, but what if we want a cell to span across more than one row or column? Introducing grid lines:

  • Imagine “invisible” lines running across the grid.
  • The lines that run from left to right are called the column lines – Starting from line number 1 on the far left.
  • The lines that run from top to bottom are called the row lines – Starting from line number 1 on the topmost.
  • To span a cell across many columns, we specify grid-column-start and grid-column-end.
  • To span a cell across many rows, we specify grid-row-start and grid-row-end.

 

SPANNING ACROSS COLUMNS

4a-lines-col.html
<div id="grid-line">
  <div class="cell">Cell</div>
  <div class="cell">Cell</div>
  <div class="cell">Cell</div>
  <div class="cell spancol">
    This one will span from column line 1 to 4.
  </div>
  <div class="cell">Cell</div>
  <div class="cell">Cell</div>
  <div class="cell">Cell</div>
</div>
4a-lines-col.css
/* (A) GRID CONTAINER */
#grid-line {
  display: grid;
  grid-template-columns: auto auto auto;
  grid-gap: 10px;
}
 
/* (B) GRID CELL */
div.cell {
  background: #feffc4;
  border: 1px solid #aaa;
  padding: 10px;
}

/* (C) SPAN MULTIPLE COLUMNS */
.spancol {
  grid-column-start: 1;
  grid-column-end: 4;
}

 

SPANNING ACROSS ROWS

4b-lines-row.html
<div id="grid-line">
  <div class="cell">Cell</div>
  <div class="cell">Cell</div>
  <div class="cell">Cell</div>
  <div class="cell spanrow">
    This will span across row line 2 to 4.
  </div>
  <div class="cell">Cell</div>
  <div class="cell">Cell</div>
  <div class="cell">Cell</div>
  <div class="cell">Cell</div>
</div>
4b-lines-row.css
/* (A) GRID CONTAINER */
#grid-line {
  display: grid;
  grid-template-columns: auto auto auto; /* 3 COL LAYOUT */
  grid-gap: 10px;
}
 
/* (B) GRID CELL */
div.cell {
  background: #feffc4;
  border: 1px solid #aaa;
  padding: 10px;
}

/* (C) SPAN MULTIPLE ROWS */
.spanrow {
  grid-row-start: 2;
  grid-row-end: 4;
}

 

 

5) GRID TEMPLATE AREAS

THE BASICS

So far so good? You should be able to do plenty of “good enough” CSS grid yoga by now. But here is one last piece to the basics of CSS grid – An alternate way to create a grid by defining a template and specifying areas. For example:

Yep, that is essentially a 4 columns 3 rows grid… But we have specified which cell is in which area.

 

THE CSS

5a-template-area.css
/* (A) THE CONTAINER */
#grid-layout {
  display: grid;
  grid-gap: 10px;
  /* DESIGN YOUR GRID TEMPLATE */
  grid-template-areas:
  'header header header header'
  'main main main side'
  'footer footer footer footer';
}

/* (B) ASSIGN THE AREAS */
.cell {
  padding: 10px;
}
.head {
  grid-area: header;
  background: #ffe5db;
}
.main {
  grid-area: main;
  background: #e3ffdb;
}
.side {
  grid-area: side;
  background: #dbf8ff;
}
.foot {
  grid-area: footer;
  background: #ebebeb;
}

Yep, we can “design” a grid template using grid-template-areas – You can name the area whatever you wish. Then simply “assign” the areas to which cell using grid-area.

 

THE HTML

5b-template-area.html
<div id="grid-layout">
  <div class="cell head">Header</div>
  <div class="cell main">Main</div>
  <div class="cell side">Side</div>
  <div class="cell foot">Footer</div>
</div>

This last part is self-explanatory.

 

 

USEFUL BITS & LINKS

That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.

 

SUMMARY – ALL THE BASIC CSS GRID PROPERTIES

Property Description
display: grid The container will display as a grid.
grid-template-columns: auto | px | % Set the width of the grid columns.
grid-template-rows: auto | px | % Set the width of the grid rows.
grid-column-gap: px | % Spacing in-between the cells horizontally.
grid-row-gap: px | % Spacing in-between the cells vertically.
grid-gap: px | % Sets spacing in-between the cells horizontally and vertically.
grid-column-start: N and grid-column-end: N How many columns to span across.
grid-row-start: N and grid-row-end: N How many rows to span across.
grid-template-areas: 'AREA AREA AREA' Create a grid template with area names.
grid-area: AREA Assign this cell to this area in the template.

 

CHEAT SHEET

How to Create a Basic CSS Grid (Click to enlarge)

 

LINKS & REFERENCES

 

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

1 thought on “How to Create a Grid in HTML & CSS – Simple Examples”

Leave a Comment

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