A responsive product grid in pure CSS with a sort bar above it: a results count on the left and a "Sort by" dropdown on the right that reorders the cards by price, rating or date with a few lines of vanilla JavaScript. Use it for shop category pages, new-arrival sections and catalog listings.
How it works
The root section.psb-shop holds a heading, the .psb-bar and a ul.psb-grid. The bar contains .psb-count (with .psb-count-num and .psb-count-sort) and a native select.psb-select wrapped in .psb-select-wrap, which draws a custom chevron with a pseudo-element. Each product is an li.psb-item with an article.psb-card: a .psb-media box with the photo and an optional .psb-badge, then .psb-body with category, name, .psb-price and .psb-rating. The product name link .psb-link uses a stretched ::after, so the whole card is clickable while the markup keeps a single link.
The sort data lives on each li.psb-item as data-order (featured position), data-price, data-rating and data-date (ISO date). On DOMContentLoaded the script sets the count from the number of items. When the select fires change, it sorts the items with the matching compare function, falls back to data-order for ties, re-appends them in one fragment, briefly adds .is-sorting for a fade-in and updates the "sorted by" text. It also dispatches a psb:sort event with detail.sort, so you can load server-side results instead. Without JavaScript the grid simply stays in the featured order.
Customization
--psb-colson.psb-shop: the number of columns. It is2on phones,3from 720px and4from 1024px; change the values in the media queries.--psb-accent: badge, eyebrow, star and focus color;--psb-salecolors the.psb-badge--salediscount badge.--psb-bg,--psb-cardand--psb-media-bg: page, card and image placeholder backgrounds.--psb-max: the maximum content width; the side padding grows on wide screens so the background stays full width.aspect-ratio: 5 / 4on.psb-media: use1 / 1for square photos or3 / 4for portrait fashion shots.- Add an
<option>to.psb-selectand a function with the same key in thesortersobject inscript.jsto support a new sort such as name.
The photos come from Unsplash; replace them with your own product images and keep the width and height attributes.
Use cases
- Online shops: photo product cards show in a responsive grid with a results count
- Category pages: a sort dropdown reorders items by price, rating or date
- Marketplaces: shoppers browse large catalogs with quick client-side sorting
- Store prototypes: vanilla JavaScript sorting works without a framework or backend
Accessibility
The sort control is a real select with a visible label, so it works with the keyboard and screen readers on every platform. .psb-count is an aria-live="polite" region, so the new order is announced after a change. Ratings include visually hidden text ("out of 5 stars") and the old price is marked "was". Each card shows a clear focus ring through the stretched link, and the select has its own outline. Hover lifts and the sorting fade are disabled with prefers-reduced-motion: reduce.
Usage notes
Several grids can share a page; the script scopes each one to its own .psb-shop. Give each select a unique id.