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

Margins

Our cards are stuck together. The space between elements is made with margin: the space outside the border, like the gap between two pictures on a wall.

margin: 20px;

Just like padding, the margin can be set on all sides at once, or on each side with margin-top, margin-right, margin-bottom and margin-left.

What's the difference between padding and margin? Padding is inside the border, and the element's background fills it. Margin is outside the border and is always transparent: you see the parent's background through it.

Browsers also give some elements a margin by default. Headings and paragraphs have a margin above and below them, which is why there are gaps between them although we haven't set any. The body element has a margin of 8 pixels on every side, and that's why the green area with the heading doesn't reach the edges of the preview.

When a value is zero, you can leave out the unit: margin: 0 is zero in any unit.

Instructions

  1. 1

    In the rule for .group, #breeds, add margin-top: 20px to make space above every card.

  2. 2

    In the rule for body, add margin: 0 to remove the default margin of the page. The green area then reaches the edges of the preview.

Back

8/12

  1. 1. Box Model
  2. 2. Width and Height
  3. 3. Units
  4. 4. Borders
  5. 5. Rounded Corners
  6. 6. Padding
  7. 7. Box Sizing
  8. 8. Margins
  9. 9. Several Values at Once
  10. 10. Centering
  11. 11. Overflow
  12. 12. Summary
Next

Try it yourself

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