CSS Grid: Build a Responsive Project Gallery

By CampCodes Administrator

Updated on:

A project gallery should not need a separate layout for every phone and laptop width. CSS Grid can choose the number of columns from the space available, while the HTML remains a simple list of articles.

This example has three cards with different amounts of text. Wide screens show them together. Narrow screens stack them in reading order. The link stays near the bottom of each card without fixing the card’s height.

Create the gallery markup

Use a current browser and save two files in a folder named css-grid-gallery. There is no JavaScript, framework, font download, or package installation. Save the following as index.html.

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>A shelf of small projects</title>
<link rel="stylesheet" href="style.css">
<main>
  <header><p class="eyebrow">Make something you understand</p>
    <h1>A shelf of small projects.</h1>
    <p>Three exercises for a quiet afternoon. Start with the one that interests you.</p>
  </header>
  <ul class="gallery" aria-label="Practice projects">
    <li><article><div class="project-mark" aria-hidden="true">01 / JS</div>
      <div class="card-body"><p class="eyebrow">Browser basics</p><h2>A task list that reads JSON</h2>
        <p>Load a file and keep the page useful when a request goes wrong.</p>
        <a href="https://www.campcodes.com/tutorials/javascript-how-to-use-fetch-api-in-a-real-project/">Build the task list</a></div></article></li>
    <li><article><div class="project-mark" aria-hidden="true">02 / PY</div>
      <div class="card-body"><p class="eyebrow">Working with files</p><h2>A cleaner for contact CSVs</h2>
        <p>Trim names, check rows, and write a separate clean file.</p>
        <a href="https://www.campcodes.com/mini-projects/python-mini-projects/python-build-a-python-csv-cleaner-for-beginners/">Build the CSV cleaner</a></div></article></li>
    <li><article><div class="project-mark" aria-hidden="true">03 / PHP</div>
      <div class="card-body"><p class="eyebrow">First database</p><h2>Tasks stored with SQLite</h2>
        <p>Save and search records using PDO and prepared statements.</p>
        <a href="https://www.campcodes.com/tutorials/php-how-to-use-pdo-in-a-real-project/">Build the database example</a></div></article></li>
  </ul>
</main>
</html>

The numbered color panels are typographic markers, not screenshots of finished applications. They give the cards a consistent visual start without pretending to document an interface that was never run.

The same three cards form three columns in a wide container and one column in a narrow container.
The container determines how many tracks fit. The document order stays the same at every width.

Let the available width choose the columns

Save this as style.css. The key declaration is grid-template-columns, but the surrounding rules matter too: box sizing accounts for borders and padding, the list removes its default spacing, and long text is allowed to wrap.

* { box-sizing: border-box; }
body { margin: 0; background: #f5f3eb; color: #183c31;
  font: 1rem/1.6 system-ui, sans-serif; }
main { max-width: 1120px; margin: auto; padding: 48px 24px; }
header { max-width: 680px; margin-bottom: 36px; }
h1 { font-size: clamp(2.2rem, 5vw, 4rem); line-height: 1.1; margin: 12px 0; }
.eyebrow { font-size: .85rem; font-weight: 700; }
.gallery { display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 260px), 1fr));
  gap: 24px; list-style: none; padding: 0; margin: 0; }
.gallery > li { min-width: 0; }
article { height: 100%; display: flex; flex-direction: column;
  background: #fff; border: 1px solid #bac9bf; border-radius: 8px; overflow: hidden; }
.project-mark { display: flex; align-items: end; padding: 24px;
  aspect-ratio: 16 / 9; background: #dbe8da; font-size: 2.4rem; font-weight: 750; }
.gallery > li:nth-child(2) .project-mark { background: #eee0bc; }
.gallery > li:nth-child(3) .project-mark { background: #e4dce9; }
.card-body { display: flex; flex-direction: column; flex: 1; padding: 24px;
  overflow-wrap: anywhere; }
h2 { font-size: 1.35rem; line-height: 1.25; margin: 0; }
.card-body a { margin-top: auto; padding-top: 12px; color: #14533c;
  text-underline-offset: 3px; font-weight: 650; }
a:focus-visible { outline: 3px solid #af4b17; outline-offset: 4px; }

Read the track rule from the inside out

The value min(100%, 260px) normally asks for a 260-pixel minimum. In a container narrower than that, it allows the track to shrink to the container’s width. This avoids forcing a small phone to scroll sideways just to fit a card.

The surrounding minmax lets each column grow from that minimum to a share of the remaining space. The 1fr maximum distributes available room evenly. Repeat and auto-fit then make as many columns as fit, collapsing unused tracks when there are fewer cards than available columns.

These choices are starting points for this content, not universal card dimensions. If your titles are longer, your images need more detail, or your layout has a sidebar, adjust the minimum after testing those actual conditions.

Keep the inside of each card flexible

Grid handles the arrangement of cards. Flexbox handles the vertical content inside each article. The card body grows, and margin-top: auto pushes the link toward its bottom. There is no fixed paragraph height or line clamp hiding part of the description.

The article fills its grid item’s height, so cards in a row line up. Rows can still grow naturally when content or text size changes. The aspect-ratio on the marker reserves a predictable area before the rest of the card is painted.

Open it and check the awkward widths

You can open this HTML file directly because it has no module or network data request. For a local HTTP preview, run python3 -m http.server 8000 --bind 127.0.0.1 inside the folder and visit http://127.0.0.1:8000. Stop that server with Ctrl+C afterward.

  • Make the browser wide enough for three columns, then gradually narrow it. The layout should change to two columns and then one.
  • Check a 320-pixel viewport. The cards and text should fit without a horizontal page scrollbar.
  • Increase the browser’s text size or zoom. The cards should grow rather than clip their descriptions.
  • Use Tab to reach every link. The focus outline must remain visible.
  • Replace a title with a very long word or URL. The wrapping rules should keep it inside the card.

Preserve the reading order

The cards appear in the same order in the HTML, the visual layout, and keyboard navigation. Avoid using grid placement or order values just to move an important card to an unrelated visual position. A reader navigating by keyboard or assistive technology should not have to reconstruct a different sequence.

Each card has one descriptive link. A generic repeated label such as “Click here” would be harder to distinguish when scanning links. The numbered markers are hidden from assistive technology because the headings and descriptions already communicate the useful information.

Replace the markers with real project images

When you have screenshots from your own running projects, use img elements with appropriate alt text and explicit width and height. Resize and compress the source images rather than asking CSS to shrink large originals. Keep useful labels and instructions as HTML text.

A screenshot is helpful when it explains what someone will build. It does not establish that an entire downloadable project has been security-audited or maintained. Give each image a caption or nearby context that matches what it actually shows.

Download all the example files

The ZIP contains this original CampCodes example, a README, and an MIT license. No account, package installation, or paid service is needed.

References and verification

MDN’s CSS Grid introduction explains tracks and fractional space. This guide was rewritten with AI assistance on October 22, 2026. The supplied HTML and CSS were checked in Firefox at wide and narrow widths, including a 320-pixel responsive viewport, with keyboard navigation. The code contains no JavaScript dependencies.

Leave a Comment