Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 29

Responsive Card Grid with Modern CSS

Recommended resources

Hello! Welcome to the next lesson in our journey of building complex websites with "vibe coding."

In the last lesson, we successfully created custom taxonomy list pages. We now have dedicated pages for each tag, like /tags/seo/, that list all the relevant content. While this is functionally correct and great for site structure, the result is a simple, unstyled list. As an SEO manager, you know that user engagement is a key factor, and a visually compelling layout can significantly improve metrics like time on page and bounce rate.

Today, we'll transform that plain list into a professional, responsive grid of content cards. Our learning outcome is to prompt an AI to generate a responsive, card-based grid layout using modern CSS. We will learn the vocabulary to ask for exactly what we want, and then integrate the AI's output into the Hugo template we built previously.

The Goal: From Plain List to Responsive Grid

Our objective is to go from a simple HTML list to a layout that adapts gracefully to any screen size.

Responsive Card Layout using CSS Grid
This is the core concept: a layout that looks great on a desktop (multiple columns) and automatically reflows into a single column on a mobile device, without needing complex rules for every possible screen size.

Step 1: Understanding Modern CSS for Responsive Grids

Before we can tell an AI what to build, we need to understand the modern approach to this problem. In the past, creating responsive layouts involved writing many media queriesโ€”explicit rules for different screen widths (e.g., one style for screens under 768px, another for screens above 1024px). This can be brittle and hard to maintain.

Today, CSS Grid offers a much more elegant solution that allows layouts to be intrinsically responsive. We can define a flexible grid that lets the browser figure out how many columns to show based on the available space.

The following video from Kevin Powell, a well-known CSS educator, is one of the best explanations of this technique. It will equip you with the key concepts and terminology needed to write a powerful prompt.

Breakpoint-Free CSS Grid Layouts

This video, 'Breakpoint-Free CSS Grid Layouts', will show you the modern way to create responsive grids using a single line of CSS, avoiding the need for multiple media queries.

Please watch from 00:52 to 03:12. Pay close attention to these three key concepts: The repeat() function with the autofit keyword. The minmax() function. How they combine to create a flexible grid: grid-template-columns: repeat(autofit, minmax(275px, 1fr));

The core idea from the video is this single line of CSS:
grid-template-columns: repeat(autofit, minmax(300px, 1fr));

Let's break it down:

  • repeat(autofit, ...): Tells the browser to create as many columns as can fit in the available space.
  • minmax(300px, 1fr): Tells each column that it must be at least 300px wide, but it can grow to fill any extra space (1fr means "one fractional unit").

When the screen is wide enough for three 300px columns, the browser shows three. When it shrinks, it might show two. When it's too narrow for even one 300px column, that column will simply shrink to fit the screen, preventing overflow. This is the power of modern CSS we want to leverage.


Step 2: Crafting a High-Quality AI Prompt

Now that we have the technical vocabulary, we can write a prompt that is far more effective than just "make a grid of cards." The key to "vibe coding" is precision in your request.

Let's consult an excellent guide on writing effective prompts for UI generation.

14 Prompts Every AI Coder Should Know

The article '14 Prompts Every AI Coder Should Know' provides a fantastic cheatsheet for guiding AI. We'll focus on the general principles and a specific example for our card grid.

First, read the 'Prompting Tips That Actually Work' section to understand the principles of a good prompt. Then, review the 'Responsive Card Grid' example under 'Core Prompt Patterns'.

The article highlights several key tips:

  • Be spatially specific: Use terms like grid, centered, etc.
  • Mention device behavior: "stacks on mobile, grid on desktop."
  • Use visual vocabulary: card, grid, box-shadow.

Armed with this knowledge, let's build our prompt. We will combine the CSS concepts from Step 1 with the prompt engineering strategies from Step 2.

Our Vibe Coding Prompt:

Role: You are an expert front-end developer specializing in modern, responsive CSS and semantic HTML.

Task: Create the HTML and CSS for a responsive, card-based grid layout to display a list of blog posts.

HTML Structure:

  • Create a <div> with a class of post-grid to act as the grid container.
  • Inside, create several example <div> elements with a class of card for the individual posts.
  • Each card should contain:
    • An <img> tag for a featured image.
    • An <h3> for the post title.
    • A <p> for a short summary.

CSS Styling & Layout:

  1. Grid Layout: Style the .post-grid container using CSS Grid. It must be responsive without using media queries. Use the repeat(autofit, minmax()) pattern.
  2. Card Sizing: Each card should have a minimum width of 300px and be allowed to grow to fill available space.
  3. Spacing: Add a gap to the grid for spacing between cards.
  4. Card Appearance: Style the .card elements with a light background color, rounded corners, and a subtle box-shadow to give them a modern, "floating" appearance. Add some padding inside the cards.
  5. Image: Ensure the image inside the card fills the width of its container without distortion.

This prompt is effective because it's not just asking for a result; it's specifying the method (CSS Grid, autofit, minmax) and the desired behavior (responsive without media queries).


Step 3: Integrating the AI's Output into Hugo

Run the prompt through your AI tool of choice. It should provide you with two blocks of code: HTML and CSS.

The CSS

The AI will likely generate CSS similar to this:

.post-grid {
  display: grid;
  grid-template-columns: repeat(autofit, minmax(300px, 1fr));
  gap: 1.5rem;
  padding: 1.5rem;
}

.card {
  background: #ffffff;
  border-radius: 8px;
  box-shadow: 0 4px 8px rgba(0,0,0,0.1);
  overflow: hidden; /* Ensures the image respects the border-radius */
  display: flex;
  flex-direction: column;
}

.card img {
  width: 100%;
  height: 200px;
  object-fit: cover;
}

.card-content {
    padding: 1rem;
}

.card h3 {
  margin-top: 0;
}

For now, the simplest way to add this to your project is to create a new file at static/css/style.css and paste the code there. Then, link to it from your main layout file (e.g., layouts/_default/baseof.html) inside the <head> tag:

<link rel="stylesheet" href="{{ "/css/style.css" | relURL }}">

The HTML

Now, we need to apply this structure to our taxonomy list template from the previous lesson: layouts/taxonomy/tag.html.

Take the AI-generated HTML structure and merge it with Hugo's range loop.

{{ define "main" }}
  <main>
    <h1>Posts tagged with '{{ .Title }}'</h1>

    <!-- This is our new grid container -->
    <div class="post-grid">
      {{/* Loop through all pages with this tag */}}
      {{ range .Data.Pages }}
        <!-- Each page becomes a card -->
        <article class="card">
          {{/* We can add a placeholder image or use a front matter param */}}
          {{ with .Params.image }}
            <img src="{{ . | relURL }}" alt="">
          {{ else }}
            <img src="https://placehold.co/400x200" alt="Placeholder image">
          {{ end }}
          
          <div class="card-content">
            <h3><a href="{{ .RelPermalink }}">{{ .Title }}</a></h3>
            <p>{{ .Summary }}</p>
          </div>
        </article>
      {{ end }}
    </div>

  </main>
{{ end }}

By wrapping our range loop inside the .post-grid div and wrapping each post's content inside the .card article, we are applying the AI-generated styles to our dynamic Hugo content. Restart your Hugo server, navigate to a tag page (e.g., localhost:1313/tags/seo/), and you should see your plain list transformed into a beautiful, responsive grid!

Test your understanding!

Looking at the prompt we created, what part is most critical for ensuring the layout adapts to different screen sizes without needing specific instructions for mobile, tablet, and desktop?

Show answer

The most critical part is the instruction: "It must be responsive without using media queries. Use the repeat(autofit, minmax()) pattern." This tells the AI to use the modern, intrinsic CSS Grid approach rather than the older, more verbose method of writing media queries for fixed breakpoints.


Conclusion

Fantastic work! You have successfully bridged the gap between a functional backend and a polished frontend. You learned the principles of modern, responsive CSS and, more importantly, how to articulate those principles in a precise AI prompt to get the exact result you need.

Key Takeaways:

  • Modern CSS Grid allows for intrinsically responsive layouts using repeat(autofit, minmax(...)), which is more robust than traditional media queries.
  • Effective AI prompting for UI requires being specific about the desired method (CSS Grid), behavior (responsiveness), and visuals (cards, shadows, etc.).
  • You can integrate AI-generated HTML structures and CSS styles into your Hugo templates to quickly build sophisticated, dynamic layouts.

Preview of the next lesson:
We added our CSS in the static folder, which works but isn't the most optimized approach for a production website. In our next lesson, we will explore integrating AI-generated CSS and JavaScript into the project using Hugo Pipes for bundling and optimization. This will allow us to process, minify, and manage our assets like a professional developer, improving site performanceโ€”a topic you know is crucial for SEO.