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

Centering

Our section with the cards is 80% wide, but it sits at the left edge of the page. To center it, we'll use one of the most common declarations in CSS:

margin: 0 auto;

The first value, 0, is the margin at the top and bottom. The second one, auto, is the margin on the left and right: the browser splits the free space next to the element equally between them, so the element ends up in the middle.

This only works when the element has a width set, like our width: 80%. Without it, the element is as wide as its parent, and there's no free space to split.

Don't confuse it with text-align: center: that centers the text inside its box, while margin: 0 auto centers the box itself.

Instructions

  1. 1

    In the rule for section, add margin: 0 auto. The space above the cards stays: it's the cards' own margin, not the section's.

Back

10/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.