webdevpuneet.com

Grid / List View Toggle — Free HTML CSS JS Layout Switcher Snippet

Grid / List View Toggle · Layouts · Plain HTML, CSS & JS · Live preview

CategoryLayouts

Copy & paste

Source Code

<div class="toolbar">
  <span class="toolbar-label">4 items</span>
  <div class="view-toggle" role="group" aria-label="Switch view layout">
    <button class="view-btn active" id="gridBtn" aria-pressed="true" aria-label="Grid view" onclick="setView('grid')">
      <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="3" y="3" width="7" height="7"/><rect x="14" y="3" width="7" height="7"/><rect x="3" y="14" width="7" height="7"/><rect x="14" y="14" width="7" height="7"/></svg>
    </button>
    <button class="view-btn" id="listBtn" aria-pressed="false" aria-label="List view" onclick="setView('list')">
      <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><line x1="3" y1="6" x2="21" y2="6"/><line x1="3" y1="12" x2="21" y2="12"/><line x1="3" y1="18" x2="21" y2="18"/></svg>
    </button>
  </div>
</div>
<div class="item-container grid-view" id="itemContainer">
  <div class="item-card">
    <div class="item-thumb"></div>
    <div class="item-info"><h4>Wireless Headphones</h4><p>Noise-cancelling, 30hr battery</p><span class="item-price">$129</span></div>
  </div>
  <div class="item-card">
    <div class="item-thumb"></div>
    <div class="item-info"><h4>Mechanical Keyboard</h4><p>Hot-swappable switches</p><span class="item-price">$89</span></div>
  </div>
  <div class="item-card">
    <div class="item-thumb"></div>
    <div class="item-info"><h4>4K Webcam</h4><p>Auto-focus, wide angle</p><span class="item-price">$59</span></div>
  </div>
  <div class="item-card">
    <div class="item-thumb"></div>
    <div class="item-info"><h4>USB-C Hub</h4><p>7-in-1 dock</p><span class="item-price">$39</span></div>
  </div>
</div>

Extend with AI

Take This Snippet Further With AI

The AI prompt for this one is for Patreon supporters ($3 a month), along with a site with no ads. Supporters enter the passcode once and it is remembered in this browser.

· Become a supporter

What's included

Features

Single container class swap re-lays-out identical card markup with zero re-rendering
Grid mode uses CSS grid; list mode uses flex column — both driven by one class change
Thumbnail size and card direction both respond automatically to the active mode
Toggle buttons use role="group" and aria-pressed for correct assistive technology semantics
Active button gets a raised white background against a muted toggle-group track
No data loss or scroll-position reset when switching views since cards are never re-created
Easy to extend with a third or fourth layout mode by adding another CSS class
Works with any card content — text, price, image — with no per-item JavaScript logic

About this UI Snippet

Grid / List View Toggle — HTML, CSS & JavaScript Layout Switcher

Screenshot of the Grid / List View Toggle snippet rendered live

Product listings, file browsers, and search results commonly let users choose between a visual grid layout and a denser list layout. This snippet implements that switch with a single class swap on the shared container — the same card markup renders completely differently depending on whether .grid-view or .list-view is applied, with no JavaScript re-rendering of the actual items.

How one class controls two layouts

Every item uses identical markup: a .item-card containing a .item-thumb and an .item-info block. The *container* (#itemContainer) carries either .grid-view or .list-view. CSS descendant selectors like .item-container.grid-view .item-card { flex-direction: column } versus .item-container.list-view .item-card { flex-direction: row } change how each identical card lays out its own children, and the container itself switches between display: grid (grid mode) and display: flex; flex-direction: column (list mode). Because only the container's class changes, setView(view) never touches the individual item elements — no cloning, no re-rendering, no risk of losing scroll position or event listeners bound to specific cards.

How the thumbnail resizes between modes

In grid mode, the thumbnail is full width with a fixed aspect-ratio: 1.4 so every card's image area is proportional regardless of card width. In list mode, the thumbnail shrinks to a fixed 64px square and the card's flex-direction switches to row, turning what was a vertical stack (image on top, text below) into a horizontal row (small thumbnail on the left, text filling the rest) — again purely through the container-level class controlling which descendant selectors apply.

How the toggle buttons stay in sync

setView toggles the .active class and the aria-pressed attribute on both buttons together, so exactly one is always shown as active and screen readers correctly announce a toggle-button-group pattern (role="group" on the wrapper, aria-pressed per button) rather than an ambiguous pair of plain buttons.

Step by step

How to Use

  1. 1
    Load the snippetClick "Grid / List View Toggle" in the sidebar Library tab to load the item collection.
  2. 2
    Switch viewsClick the grid and list icons in the toolbar in the preview to see the layout swap instantly.
  3. 3
    Swap in real contentReplace the .item-thumb gradients with real <img> tags and update the title/description/price text.
  4. 4
    Persist the chosen viewStore the selected view in localStorage inside setView and restore it on page load.
  5. 5
    Add a third view modeAdd a new CSS class like .compact-view with its own descendant rules, and extend setView to accept and apply it.
  6. 6
    Export and saveExport as HTML/JSX/Tailwind or click "Save as" to reuse this toggle in a real product or file listing page.

Real-world uses

Common Use Cases

SHOP
Product listing pages
Let shoppers switch between a visual grid for browsing and a dense list for quick scanning and comparison.
File and document browsers
Toggle between thumbnail grid view and detailed list view, similar to a desktop file manager.
Search results and directories
Apply the same toggle to search results, contact directories, or media libraries.
Learn container-level layout switching
Study how descendant CSS selectors keyed off a single parent class can re-lay-out identical markup with no JS DOM manipulation of the items.
Accessible toggle button groups
See the role="group" plus aria-pressed pattern that correctly communicates a segmented view-switcher to screen readers.
Related: Resizable Sidebar with Persisted Width
See the Resizable Sidebar with Persisted Width for a related layouts pattern worth pairing with this one.

Got questions?

Frequently Asked Questions

Every card uses identical HTML markup regardless of the active view. Only the container's class changes between .grid-view and .list-view, and CSS descendant selectors scoped to that class control how each card's children are laid out — no JavaScript ever touches the individual item elements.

The .item-thumb rule is overridden separately for grid mode (full width, fixed aspect-ratio) and list mode (fixed 64px square), both scoped under .item-container.grid-view and .item-container.list-view respectively.

setView toggles the .active class and the aria-pressed attribute on both buttons in the same call, so exactly one button always shows the active/raised styling and screen readers hear the correct pressed state for both.

Yes — save the view name to localStorage inside setView, and on page load read that value back and call setView with it before the user interacts with the toggle.

Add a new class (e.g. .compact-view) with its own container and descendant CSS rules, add a third toggle button, and update setView to remove all view classes before adding whichever one was selected.

No — it is a single class swap driven by one small JavaScript function. It works equally well as a static HTML/CSS/JS component or embedded inside a React/Vue component's render output.

Yes — the wrapping div has role="group" with an aria-label describing its purpose, and each button reports its own pressed state via aria-pressed, which is the standard pattern for a mutually-exclusive toggle button group.

Design your website