Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

Badges on Cards

Oscar has just arrived at the shelter, so we've given his card a "New" badge: a span above the picture. For now, it takes up a line of its own. We'd like it in the top left corner of the card, over the picture.

The property position has a class for each of its values, named after the value: static, relative, absolute, fixed and sticky. The offsets top, right, bottom and left have classes with the numbers of the spacing scale:

ClassCSS
top-3top: 0.75rem; (12 px)
left-3left: 0.75rem; (12 px)
right-0right: 0;
-top-2top: -0.5rem; (a minus before the class makes the value negative)
inset-0top: 0; right: 0; bottom: 0; left: 0;

absolute inset-0 stretches an element over the whole of its positioned ancestor. That's handy, for example, for a semi-transparent layer over a picture.

And remember the trick from the CSS course: the offsets of an absolute element are measured from its nearest ancestor with a position other than static. That's why the card will need the class relative.

Instructions

  1. 1

    Add the classes absolute top-3 left-3 to the badge. It jumps to the top left corner of the page, over the logo: none of its ancestors has a position, so its offsets are measured from the page.

  2. 2

    Add the class relative to Oscar's card (the article element). The badge moves to the corner of the card, 12 px from its top and left edges: the card is now its nearest ancestor with a position.

Back

9/11

  1. 1. Display
  2. 2. Flexbox
  3. 3. Justify and Align
  4. 4. Columns
  5. 5. Wrapping and Growing
  6. 6. Grid
  7. 7. Spanning Columns
  8. 8. Centering
  9. 9. Badges on Cards
  10. 10. A Sticky Header
  11. 11. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.