Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialKireeti K
9,675 PointsWhat does this css code do. margin: 0 auto;
I have read in a forum about this that auto will automatically add margin to both the left and right side of the element. But how much will it add automatically? Also in the project we used padding to get some space on both sides and why we need margin again?
Thanks in advance
4 Answers
Ben Van Beek
31 Points-It adds the same amount of space on both sides, so it will be centered horizontally.
-Image your element as a box with a border. The amount of space you want between your content and that border, is the padding of an element. The amount of space you want between your element and another element, let's say the top of the page, is the margin.
Jennifer Nordell
Treehouse TeacherShort answer? This will horizontally align the item in the center of the parent element
Owa Aquino
19,277 PointsI call it "center things code" xD.
Seriously it centers the element, but it dependent on its parent element. :)
Cheers!
Jonathan Grieve
Treehouse Moderator 91,253 PointsIn my experience it works best when the parent element is set to a block level element and to relative position.
Then the target element willl position itself centrally in the available space left to right of the containing element. :-)
donald stouffer
7,630 Pointsdonald stouffer
7,630 PointsOK, margins are meant to space elements on a webpage away from each other. The code we saw on the video is short for this:
So we are adding no blank space at the top of our section and the bottom. Auto basically centers the element on the screen. If the <section> is 980px wide and your screen size is 1080 wide, then 1080 - 980 = 100px / 2 (left and right). So 50px on each side.
Padding is different because it is space WITHIN the element. Check out this picture. Through your programming journey you will understand when one is needed over the other. The best way to understand CSS is to change your code and see what happens.