List Grid View With HTML CSS (Very Simple Example)

Welcome to a tutorial on how to create a responsive list/grid view. Once upon a time in the Dark Ages of the Internet, we have to use all sorts of CSS hacks and fight digital dragons to transform a list into a grid. But rest easy, modern CSS is very easy to work with. Let us walk through a simple list/grid example in this guide, read on!

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

 

 

TLDR – QUICK SLIDES

Fullscreen Mode – Click Here

 

TABLE OF CONTENTS

 

DOWNLOAD & DEMO

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

 

QUICK NOTES

If you spot a bug, feel free to comment below. I try to answer short 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.

 

EXAMPLE CODE DOWNLOAD

Click here to download the source code in a zip file – I have released it under the MIT License, so feel free to build on top of it if you want to.

 

LIST/GRID VIEW DEMO

Item A
Item B
Item C
Item D
Item E

 

 

 

HTML CSS LIST GRID VIEW

All right, let us now get into more details on how the list/grid view works in this section.

 

PART 1) THE HTML

list-grid.html
<!-- (A) ITEMS LIST/GRID -->
<ul id="demo">
  <li>Item A</li>
  <li>Item B</li>
  <li>Item C</li>
  <li>Item D</li>
  <li>Item E</li>
</ul>
 
<!-- (B) VIEW SWITCHER -->
<button onclick="document.getElementById('demo').className=''">
  &#8863;
</button>
<button onclick="document.getElementById('demo').className='grid'">
  &#8862;
</button>

Yep, that’s all we need for the HTML.

  1. Just a “regular” HTML list.
  2. Buttons to switch between list and grid view.

 

 

PART 2) LIST VIEW CSS

list-grid.css
/* (A) LIST VIEW BY DEFAULT */
#demo {
  /* (A1) REMOVE BULLETS & IDENTATION */
  list-style: none;
  padding: 0; margin: 0;
 
  /* (A2) LIST VIEW - 1 ITEM PER ROW */
  display: grid;
  grid-template-columns: 100%;
  grid-gap: 5px;
}
 
/* (B) LIST ITEMS */
#demo li {
  padding: 10px;
  border: 1px solid #ddd;
}
#demo li:nth-child(odd) { background: #f7f7f7; } 
  • (A1) The default list will come with bullet points and indentations, we simply remove them here.
  • (A2) The magic behind the list/grid view is actually display: grid. This is set to grid-template-columns: 100%, effectively a single-column list.
  • (B) Just some cosmetics for the list items.

 

 

PART 3) GRID VIEW CSS

list-grid.css
/* (C) GRID VIEW - 3 ITEMS PER ROW */
#demo.grid {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}
  • Remember the HTML buttons above? They toggle a .grid CSS class on the list.
  • To transform the list into a grid, we simply set #demo.grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }. This turns it into three columns per row, and that’s all to the “grid view”.

 

EXTRA BITS & LINKS

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

 

LINKS & REFERENCES

 

TUTORIAL VIDEO

 

INFOGRAPHIC CHEAT SHEET

List Grid View With HTML CSS (click to enlarge)

 

THE END

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

1 thought on “List Grid View With HTML CSS (Very Simple Example)”

Leave a Comment

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