Simple List Grid View With HTML CSS (Free Download)

Welcome to a tutorial and example on how to create a responsive list/grid view with CSS and Javascript. Once upon a time in the Dark Ages of the Internet, we have to play with all sorts of CSS hacks and fight digital dragons to get a list to transform into a grid.

Thankfully, modern CSS has matured and is much easier 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.

 

 

QUICK SLIDES

 

TABLE OF CONTENTS

Download & Demo List Grid View Useful Bits & Links
The End

 

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

 

 

LIST GRID VIEW

All right, let us not get into how the list/grid view works in this section.

 

STEP 1) THE HTML

list-grid.html
<!-- (A) CSS & JS -->
<link rel="stylesheet" href="list-grid.css"/>
<script src="list-grid.js"></script>
 
<!-- (B) VIEW SWITCHER -->
<button id="sList">&#8863;</button>
<button id="sGrid">&#8862;</button>
 
<!-- (C) ITEMS LIST/GRID -->
<div id="lgDemo">
  <div class="item">Item A</div>
  <div class="item">Item B</div>
  <div class="item">Item C</div>
  <div class="item">Item D</div>
  <div class="item">Item E</div>
</div>

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

  1. Load the CSS and Javascript files. Doh.
  2. Create 2 buttons to switch between list and grid view.
  3. The items to display in a list or grid.

 

 

STEP 2) THE JAVASCRIPT

list-grid.js
window.addEventListener("DOMContentLoaded", () => {
  document.getElementById("sList").onclick = () => {
    document.getElementById("lgDemo").classList.remove("grid");
  };
  document.getElementById("sGrid").onclick = () => {
    document.getElementById("lgDemo").classList.add("grid");
  };
});

Dear beginners, no need to freak out. All this does is essentially attach onclick to the buttons on page load –

  • Click on the “grid view” button to add a .grid CSS class to #lgDemo.
  • Clicking on the “list view” button will remove the .grid CSS class.

 

 

STEP 3) THE CSS

list-grid.css
/* (A) LIST VIEW BY DEFAULT */
#lgDemo {
  display: grid;
  grid-template-columns: 100%;
  grid-gap: 5px;
}
#lgDemo .item {
  padding: 10px;
  border: 1px solid #ddd;
}
#lgDemo .item:nth-child(odd) { background: #f7f7f7; }

/* (B) TRANSFORM INTO GRID VIEW */
#lgDemo.grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
#lgDemo.grid .item:nth-child(odd) { background: none; }

The CSS is the one that actually drives the list/grid view mechanism.

  1. The magic behind the list/grid view is actually display: grid. This is set to grid-template-columns: 100% by default, effectively making it a list view with only a single column.
  2.  Remember that the .grid CSS class will be added when the user clicks on the “grid view” button? To transform the list into a grid, we simply set #lgDemo.grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }. That turns a single column into three-a-row, and that’s all to the “difficult mechanism”.

 

 

USEFUL 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

 

INFOGRAPHIC CHEAT SHEET

Product List Grid View With JS 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 “Simple List Grid View With HTML CSS (Free Download)”

Leave a Comment

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