Tutorials CSS Project: Card Component
Ch 56 / 60
Chapter 56 Project 12 min build CSS

Project: Card UI Component

This is the first hands-on project chapter — instead of learning one new CSS feature, you'll combine everything from earlier chapters (box model, flexbox, shadows, transitions, object-fit, grid) to build a real, production-quality product card from scratch: image with badge, category label, title, star rating, price with strikethrough discount, and a hover lift effect — then drop it into a fully responsive auto-fit grid.

-30% ♡
🎧
Audio
Wireless Studio Headphones
★★★★☆ 4.2 (318)

↑ This is the actual finished component, rendered live. Hover it.

What you'll build
Image area with aspect-ratio + badge overlay
Floating wishlist button (absolute positioning)
Star rating row with flexbox
Price with strikethrough discount
Hover lift with transform + shadow
Responsive auto-fit grid, zero media queries

Step 1 — HTML Structure

Start with semantic, minimal markup. Every visual element in the final card maps to one wrapper here — get this right and the CSS becomes straightforward:

HTML — card markup
<div class="card">
  <div class="card-img-wrap">
    <img src="headphones.jpg" alt="Wireless headphones">
    <span class="card-badge">-30%</span>
    <button class="card-wishlist" aria-label="Add to wishlist">♡</button>
  </div>

  <div class="card-body">
    <div class="card-category">Audio</div>
    <h3 class="card-title">Wireless Studio Headphones</h3>

    <div class="card-rating">
      <span class="card-stars">★★★★☆</span>
      <span class="card-rating-num">4.2 (318)</span>
    </div>

    <div class="card-footer">
      <div class="card-price-wrap">
        <span class="card-price">₹2,799</span>
        <span class="card-price-old">₹3,999</span>
      </div>
      <button class="card-btn" aria-label="Add to cart">+</button>
    </div>
  </div>
</div>

Step 2 — Card Shape & Image Area

The container needs rounded corners with overflow: hidden so the image respects them, plus a fixed aspect-ratio so the image area never distorts regardless of the actual photo's dimensions:

Shape + image only
🎧
Card title
No shadow yet
+ box-shadow elevation
🎧
Card title
Subtle resting shadow
CSS — card shape, image, and badge overlay
.card {
  width:        260px;
  background:   #fff;
  border-radius:16px;       /* rounded card */
  overflow:     hidden;     /* clips the image to the rounded corners */
  box-shadow:   0 2px 8px rgba(15,23,42,.08);  /* resting elevation */
}

.card-img-wrap {
  position:     relative;   /* anchor for badge + wishlist button */
  aspect-ratio: 4 / 3;     /* fixed shape, regardless of card width */
  overflow:     hidden;
}
.card-img-wrap img {
  width:     100%;
  height:    100%;
  object-fit:cover;   /* fills the box, crops instead of distorting */
}

/* ── Badge — positioned absolutely within the relative parent ── */
.card-badge {
  position:      absolute;
  top:           10px; left: 10px;
  background:    #ef4444;
  color:         #fff;
  font-size:     10px; font-weight: 700;
  padding:       4px 9px;
  border-radius: 999px;   /* pill shape */
}

/* ── Wishlist circle button ── */
.card-wishlist {
  position:      absolute;
  top:           10px; right: 10px;
  width:         30px; height: 30px;
  border-radius: 50%;
  background:    rgba(255,255,255,.9);
  border:        none;
  cursor:        pointer;
  transition:    background .2s;
}
.card-wishlist:hover { background: #fff; }

Step 3 — Title, Rating & Price Row

The body content uses flexbox twice: once for the rating row (stars + count, aligned on the same baseline), and again for the footer (price on the left, button on the right, pushed apart):

CSS — body content: category, title, rating, price
.card-body { padding: 14px 16px 16px; }

.card-category {
  font-size:      10.5px;
  color:          #94a3b8;
  font-weight:    600;
  text-transform:uppercase;
  letter-spacing:.4px;
  margin-bottom: 4px;
}

.card-title {
  font-size:     15px;
  font-weight:   700;
  color:         #0f172a;
  line-height:  1.3;
  margin-bottom:6px;
}

/* ── Rating row — stars and count side by side ── */
.card-rating {
  display:      flex;
  align-items:  center;
  gap:          5px;
  margin-bottom:10px;
}
.card-stars      { color: #f59e0b; font-size: 12px; letter-spacing: 1px; }
.card-rating-num { font-size: 11px; color: #64748b; font-weight: 600; }

/* ── Footer — price left, button right, pushed apart ── */
.card-footer {
  display:        flex;
  align-items:    center;
  justify-content:space-between;
}
.card-price-wrap { display: flex; align-items: baseline; gap: 6px; }
.card-price     { font-size: 17px; font-weight: 800; color: #264de4; }
.card-price-old { font-size: 11.5px; color: #94a3b8; text-decoration: line-through; }

Step 4 — Hover Lift Effect

The signature "lift" interaction: the card rises slightly and its shadow grows softer and larger, simulating it physically lifting off the page. Both properties need a transition declared on the resting state — not the hover state — so the change animates in both directions:

Resting state
🎧
translateY(0)
small shadow
Hover state (try it on the live preview above!)
🎧
translateY(-6px)
larger, softer shadow
CSS — hover lift transition
.card {
  /* ...shape rules from Step 2... */
  transition: transform .25s ease, box-shadow .25s ease;  /* on the BASE rule */
}

.card:hover {
  transform:  translateY(-6px);                  /* lift up 6px */
  box-shadow: 0 16px 32px rgba(15,23,42,.16);  /* larger, softer = "further from page" */
}

.card-btn {
  background:    #0f172a;
  color:         #fff;
  border:        none;
  border-radius: 9px;
  width:         36px; height: 36px;
  cursor:        pointer;
  transition:    background .2s;
}
.card-btn:hover { background: #264de4; }  /* button has its OWN independent hover */

Step 5 — Responsive Card Grid

A single line of CSS Grid makes the card collection fully responsive — adding or removing columns based on available width, with zero media queries:

Resize the browser — watch columns auto-adjust
card
card
card
card
card
CSS — auto-fit responsive grid
.card-grid {
  display:               grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap:                   1.5rem;
}

/* ── How auto-fit + minmax works ──
   - Each column is AT LEAST 240px wide
   - As many 240px+ columns as fit are created
   - Remaining space is divided EQUALLY among them (the 1fr)
   - No media query breakpoints needed — it's continuous,
     not stepped like traditional responsive grids */

.card {
  width: 100%;   /* let the grid cell control width, not a fixed px */
}

Component Anatomy Reference

Element Key CSS Why
.card border-radius, overflow:hidden, box-shadow Rounded shape, clips children, resting elevation
.card-img-wrap position:relative, aspect-ratio Anchors badge/button, fixed image shape
.card-img-wrap img object-fit: cover Fills box without distortion regardless of source size
.card-badge position:absolute, border-radius:999px Floats over image, pill shape
.card-rating display:flex, align-items:center Stars and count aligned on one row
.card-footer display:flex, justify-content:space-between Price and button pushed to opposite ends
.card:hover transform:translateY(), box-shadow Lift effect — requires transition on the base rule
.card-grid grid-template-columns:repeat(auto-fit,minmax()) Responsive columns with zero media queries
Free Resource
CSS3 Cheat Sheet — Component Building Patterns
Complete reference covering card components, hover effects, and responsive grid patterns.
Download PDF

Try It Yourself

The complete component, plus a 6-card responsive grid — resize the preview pane to see auto-fit columns respond:

Live Editor
✎ Editor
👁 Preview
Featured Course — Enroll Now
Modern Web Development With AI
Master HTML, CSS, JavaScript, React and Node.js with real projects and placement support.
120+
Chapters
40 hrs
Content
10 Real
Projects
4.9 ⭐
Rating
HTML5CSS3JavaScriptReact.jsNode.jsTailwind CSSAI Tools

Quick Check — Chapter 56

Questions & Answers
border-radius rounds the corners of the element's own border box, but it does NOT automatically clip child elements that visually extend past that rounded boundary. Without overflow: hidden, the image inside .card-img-wrap would render as a rectangle with sharp corners poking out past the card's rounded edges, breaking the rounded appearance entirely.

overflow: hidden tells the browser to clip anything inside the element that extends beyond its bounds — and because those bounds are rounded, the clipping follows the rounded shape too. This is why the pattern always pairs border-radius with overflow: hidden whenever a rounded container holds an image or other content that would otherwise overflow the corners.
They solve two separate problems and need each other:

aspect-ratio: 4/3 on the wrapper fixes the SHAPE of the image area — regardless of the card's actual rendered width (260px on desktop, maybe 200px in a narrower grid column), the box always maintains a 4:3 ratio. Without it, the box's height would depend entirely on the natural image dimensions, making cards in the same grid row have wildly different heights if photos have different aspect ratios.

object-fit: cover on the <img> itself controls how the actual photo fills that now-fixed box. Without it, a photo with a different native aspect ratio than 4:3 would either get squished/stretched to fit (distorted) or leave empty gaps. cover scales and crops the image so it always fully fills the box while preserving its proportions — the parts that don't fit are simply cropped off, not squeezed.

Use aspect-ratio alone and you get a fixed box but possibly distorted images. Use object-fit alone (without a fixed-ratio parent) and the box height still varies per image. Together, they guarantee both consistent card heights and undistorted images.
A CSS transition animates a property change FROM its current computed value TO a new value, and it needs to be active during BOTH directions of that change — when the hover starts (resting → hover) and when the hover ends (hover → resting, as the mouse leaves).

If transition is only declared inside .card:hover, the browser only "knows" to animate while that hover rule is actively matched. The moment the mouse leaves and :hover stops applying, the transition declaration disappears along with it — so the card snaps back to its resting position instantly, with no animation, while the hover-in direction still animated smoothly. This asymmetric "smooth in, instant out" bug is one of the most common transition mistakes.

Declaring transition: transform .25s ease, box-shadow .25s ease on the base .card rule (not the hover variant) ensures the transition is always active, regardless of which state is currently applied — so both the lift-up and the settle-back-down animate identically.
Breaking down each piece: minmax(240px, 1fr) defines a column track that's never narrower than 240px but can grow up to 1fr (an equal share of remaining space) if there's room. auto-fit tells the grid to automatically calculate how many of these flexible columns can fit in the available container width, repeating the pattern as many times as it takes.

As the container shrinks, fewer 240px+ columns fit, so the grid drops to fewer columns automatically — 4 columns become 3, then 2, then 1 — with each remaining column expanding to fill the freed-up space via the 1fr portion. As the container grows, more columns fit and each gets its fair share of any leftover space.

This produces a smooth, continuously adapting layout rather than the "jump" behavior of traditional media-query breakpoints (1 column below 600px, 2 columns from 600-900px, 3 above 900px). The grid recalculates column count at every pixel of container width, with no breakpoint values hardcoded anywhere.
The wishlist button (and the discount badge) need to visually "float" on top of the image in a fixed corner, regardless of how much other content surrounds them — they shouldn't take up space in the normal document flow the way a regular flex or block element would.

position: absolute removes the element from normal flow and positions it using top/right/bottom/left offsets — but relative to what? By default, an absolutely positioned element is relative to the entire page (or the nearest scrolling container), which would place it in the wrong corner entirely.

This is why .card-img-wrap has position: relative declared on it — that establishes it as the "positioning context" for any absolutely positioned descendants. Once that's set, .card-wishlist { position: absolute; top: 10px; right: 10px; } positions the button 10px from the top and right edges of .card-img-wrap specifically — not the page — giving the floating-corner-button effect.
Multiple Choice Questions
Question 1 of 5
What CSS pairing is required so a rounded card properly clips its image into the rounded shape?
Question 2 of 5
What does object-fit: cover do on the card image?
Question 3 of 5
Where must the transition property be declared for the hover lift to animate smoothly in BOTH directions?
Question 4 of 5
What does the wishlist button position relative to, given .card-img-wrap has position: relative?
Question 5 of 5
What does repeat(auto-fit, minmax(240px, 1fr)) guarantee about each grid column?
Chapter Summary
  • Card shape: border-radius + overflow: hidden together — the second clips children into the rounded shape the first creates.
  • Image area: aspect-ratio on a wrapper fixes the box shape regardless of card width; object-fit: cover on the image fills that box without distortion.
  • Floating overlays (badge, wishlist button): position: absolute on the child + position: relative on the image wrapper establishes the correct positioning context.
  • Rating and footer rows: flexbox with align-items: center for inline grouping, justify-content: space-between for pushing elements to opposite ends.
  • Hover lift: transform: translateY(-6px) + larger/softer box-shadow on :hover, with transition declared on the BASE rule so both directions animate.
  • Responsive grid: grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)) — continuous column adjustment with zero media queries.
  • Pattern reuse: this exact card skeleton (image wrapper + body + footer) applies to blog cards, team member cards, course cards, and dashboards with only content swapped.
~12 min build
Project chapter
Chapter 56 of 60

Rate Us on Google

4.8 / 5
Enjoying this tutorial?
Your 5-star review helps other learners discover our free tutorials and encourages us to keep creating quality content.
80+ reviews
Write a Google Review
Join WhatsApp Channel