CSS Card Component
What you'll build
An HTML page with 3 card components side by side. Each card has an image, a title, a description, and a button, styled with padding, border, and shadow.
Goals
- Create a reusable card layout with consistent spacing
- Apply the CSS box model (padding, border, margin, shadow)
- Use flexbox to arrange cards in a row
- Add hover effects for interactivity
Prerequisites
- HTML structure
- Basic CSS (selectors, properties, flexbox)
Steps
- 1
Write the HTML for 3 cards
Create cards.html with a div.cards container holding 3 div.card elements, each with an image, h3 title, p description, and a link button.
Code<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Cards</title> <link rel="stylesheet" href="cards.css"> </head> <body> <div class="cards"> <div class="card"> <img src="https://placehold.co/300x150" alt="Card 1"> <h3>Card Title</h3> <p>A short description of this card.</p> <a href="#" class="btn">Learn more</a> </div> <div class="card"> <img src="https://placehold.co/300x150" alt="Card 2"> <h3>Another Card</h3> <p>Another description here.</p> <a href="#" class="btn">Learn more</a> </div> <div class="card"> <img src="https://placehold.co/300x150" alt="Card 3"> <h3>Third Card</h3> <p>Third description text.</p> <a href="#" class="btn">Learn more</a> </div> </div> </body> </html>Expected result
Three cards stacked vertically with default styling.
- 2
Layout the cards in a row
Create cards.css. Use flexbox to arrange cards horizontally with a gap.
Code.cards { display: flex; gap: 1.5rem; padding: 2rem; flex-wrap: wrap; }Expected result
Cards appear side by side with spacing between them.
- 3
Style the card box
Apply the box model: border, border-radius, box-shadow, and overflow hidden to clip the image corners.
Code.card { background: #fff; border: 1px solid #e2e8f0; border-radius: 0.75rem; box-shadow: 0 4px 6px -1px rgba(0,0,0,0.1); overflow: hidden; width: 300px; transition: transform 0.2s, box-shadow 0.2s; } .card:hover { transform: translateY(-4px); box-shadow: 0 10px 15px -3px rgba(0,0,0,0.1); }Expected result
Cards have rounded corners, a shadow, and lift on hover.
- 4
Style the card content and button
Add padding inside the card for content, make the image full-width, and style the button.
Code.card img { width: 100%; display: block; } .card h3 { margin: 0.75rem 1rem 0.5rem; font-size: 1.25rem; } .card p { margin: 0 1rem 1rem; color: #64748b; font-size: 0.875rem; } .card .btn { display: inline-block; margin: 0 1rem 1rem; padding: 0.5rem 1rem; background: #2563eb; color: #fff; text-decoration: none; border-radius: 0.375rem; font-size: 0.875rem; }Expected result
Content is padded inside the card, image fills the width, button is styled.
Hint
overflow: hidden on the card clips the image to the rounded corners.
- 5
Add responsive styles
Use media queries to adjust the layout for mobile: switch multi-column layouts to single column on small screens, reduce font sizes, and adjust spacing. Mobile-first means small-screen experience matters equally.
Code@media (max-width: 640px) { .container { flex-direction: column; padding: 1rem; } h1 { font-size: 1.5rem; } .card { width: 100%; margin-bottom: 1rem; } }Expected result
Below 640px viewport, the layout becomes single-column with smaller font and spacing; content does not overflow.
Try it yourself
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Cards</title>
<link rel="stylesheet" href="cards.css">
</head>
<body>
<div class="cards">
<!-- TODO: 3 cards -->
</div>
</body>
</html><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Cards</title>
<link rel="stylesheet" href="cards.css">
</head>
<body>
<div class="cards">
<div class="card">
<img src="https://placehold.co/300x150" alt="Card 1">
<h3>Card Title</h3>
<p>A short description of this card.</p>
<a href="#" class="btn">Learn more</a>
</div>
<div class="card">
<img src="https://placehold.co/300x150" alt="Card 2">
<h3>Another Card</h3>
<p>Another description here.</p>
<a href="#" class="btn">Learn more</a>
</div>
<div class="card">
<img src="https://placehold.co/300x150" alt="Card 3">
<h3>Third Card</h3>
<p>Third description text.</p>
<a href="#" class="btn">Learn more</a>
</div>
</div>
</body>
</html>
/* cards.css */
.cards { display: flex; gap: 1.5rem; padding: 2rem; flex-wrap: wrap; }
.card { background: #fff; border: 1px solid #e2e8f0; border-radius: 0.75rem; box-shadow: 0 4px 6px -1px rgba(0,0,0,0.1); overflow: hidden; width: 300px; transition: transform 0.2s, box-shadow 0.2s; }
.card:hover { transform: translateY(-4px); box-shadow: 0 10px 15px -3px rgba(0,0,0,0.1); }
.card img { width: 100%; display: block; }
.card h3 { margin: 0.75rem 1rem 0.5rem; font-size: 1.25rem; }
.card p { margin: 0 1rem 1rem; color: #64748b; font-size: 0.875rem; }
.card .btn { display: inline-block; margin: 0 1rem 1rem; padding: 0.5rem 1rem; background: #2563eb; color: #fff; text-decoration: none; border-radius: 0.375rem; font-size: 0.875rem; }What you learned
You built a reusable card component with the CSS box model — borders, shadows, padding, and hover transitions. This card pattern is the building block of most modern web UIs.
Related Resources
Related Lessons
- CSS Box Model
Padding, border, and shadow used here.
- CSS Flexbox
Flexbox for card layout.
Related Practice
- Predict the Box Model Width
Practice box model calculations.
Related Cheatsheets
- CSS Cheatsheet
Box model and flexbox reference.
Related Glossary
- CSS
Cascading Style Sheets.
Related Glossary
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
- CSS
Cascading Style Sheets.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.