Skip to content

CSS Tutorial CSS Position

CSS Positioning: A Practical Example

Lesson 79 of 182 2 min read By Deepak Paswan Updated Oct 4, 2026

Now combine what you learned in one realistic piece: a product card with a corner badge and a caption sitting on top of the image.

index.html
HTML
<style>
  body { font-family: system-ui, sans-serif; background: #f4ece6; margin: 0; padding: 16px; }
  .card { position: relative; width: 260px; background: #fff; border-radius: 10px; overflow: hidden; box-shadow: 0 2px 8px rgba(0,0,0,.15); }
  .photo { height: 150px; background: linear-gradient(135deg, #1a5276, #d94a00); }
  .badge { position: absolute; top: 10px; left: 10px; background: #fff; color: #d94a00; font-weight: 700; font-size: 12px; padding: 3px 10px; border-radius: 999px; }
  .caption { position: absolute; left: 0; right: 0; top: 106px; padding: 8px 12px; color: #fff; background: rgba(0,0,0,.45); }
  .body { padding: 12px; }
</style>
<div class="card">
  <div class="photo"></div>
  <span class="badge">New</span>
  <div class="caption">The Slow Garden</div>
  <div class="body">A gentle novel about a neighbourhood allotment. <strong>$14</strong></div>
</div>

What is positioned, and why

  • .card is position: relative. It is the anchor for the badge and caption.
  • .badge is position: absolute with top and left, so it sits in the top-left corner of the card.
  • .caption is absolute and stretches across the image with left: 0; right: 0, placed over its lower part with top.
  • overflow: hidden on the card clips the image to the rounded corners.

Try changing things

  1. Move the badge to the top-right by using right: 10px instead of left: 10px.
  2. Change the caption’s top to 0. Where does it go?
  3. Add position: sticky; top: 0; to a header element in a taller page and scroll.

Exercise: add a close button

Add a small round × button to the top-right corner of the card, 8px from each edge.

Show a possible solution
style.css
CSS
.close { position: absolute; top: 8px; right: 8px; width: 24px; height: 24px; border-radius: 50%; border: 0; background: #fff; cursor: pointer; }

Add <button class="close" aria-label="Close">×</button> inside the card. The accessible label matters because the button has no text.