Sub topic 3
Arrange Project Cards with CSS Grid
Assume you know semantic HTML, CSS selectors, and the box model. This chapter uses those skills to arrange cards, adapt the page to different screens, and check it before publishing.
Build a responsive card grid
CSS Grid lays items into rows and columns. This rule creates as many columns as fit, while keeping each card at least 16rem wide:
#projects .project-list {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr));
gap: 1.5rem;
}auto-fit changes the column count as the available width changes. A common error is using fixed column widths, which can cause horizontal scrolling on small screens.
Make images and navigation responsive
Give the page a viewport setting so mobile browsers use the device width:
<meta name="viewport" content="width=device-width, initial-scale=1">Use srcset and sizes to let the browser choose an appropriate file. The dimensions reserve space before the image loads.
<img
src="project-800.jpg"
srcset="project-400.jpg 400w, project-800.jpg 800w"
sizes="(min-width: 48rem) 400px, 100vw"
width="800"
height="500"
loading="lazy"
alt="Screenshot of the weather dashboard project">Link navigation items to section IDs:
<nav aria-label="Main">
<a href="#projects">Projects</a>
<a href="#contact">Contact</a>
</nav>html { scroll-behavior: smooth; }
section[id] { scroll-margin-top: 1rem; }
a:focus-visible {
outline: 3px solid currentColor;
outline-offset: 2px;
}
@media (prefers-reduced-motion: reduce) {
html { scroll-behavior: auto; }
}Check, validate, and publish
Use header, nav, main, section, and footer landmarks. Give every meaningful image useful alt text. Keep text and background colors high-contrast. Test every link with a keyboard and confirm the focus indicator is visible.
Before publishing:
- Open the HTML in a browser and check narrow and wide widths.
- Use an HTML validator and an accessibility audit tool.
- Confirm images load, links work, and no horizontal scrolling appears.
- Publish the HTML, CSS, and image files with a static hosting service.
Exercise
Why is minmax(16rem, 1fr) safer than three fixed columns?
Show answer
It lets each card shrink only to 16rem and reduces the number of columns when the container becomes narrower.