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
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
LIST GRID VIEW
All right, let us not get into how the list/grid view works in this section.
STEP 1) THE HTML
<!-- (A) CSS & JS -->
<link rel="stylesheet" href="list-grid.css"/>
<script src="list-grid.js"></script>
<!-- (B) VIEW SWITCHER -->
<button id="sList">⊟</button>
<button id="sGrid">⊞</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.
- Load the CSS and Javascript files. Doh.
- Create 2 buttons to switch between list and grid view.
- The items to display in a list or grid.
STEP 2) THE JAVASCRIPT
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
/* (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.
- The magic behind the list/grid view is actually
display: grid
. This is set togrid-template-columns: 100%
by default, effectively making it a list view with only a single column. - 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
- CSS Grid – MDN
- Designing A Product Page Layout with Flexbox – CSS Tricks
- Example on CodePen – List/Grid View
INFOGRAPHIC CHEAT SHEET

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!
thank you for this article, really helped me