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 trial

CSS How to Make a Website CSS: Cascading Style Sheets Style the Basic Elements

Olivia Van Buskirk
Olivia Van Buskirk
353 Points

When editing the margin using CSS, what does "auto" do, and what does "0" do?

I'm jnot sure exactly what these do on the page!

css/main.css
a {
    text-decoration: none;
}

#wrapper {
  max-width: 940px;
  margin: 0 auto;
}
Jonathan Fernandes
Jonathan Fernandes
Courses Plus Student 22,784 Points

Hi Olivia,

Basically, you are telling it to center the wrapper in it's parent container.

Normally when setting a margin, it goes clockwise. First you set the top, then the right, then the bottom, then the left. So if you changed it to "margin: 10px 15px 20px 25px;" then you are telling it to move the element 10 pixels from the top, 15 pixels from the right, 20 pixels from the bottom, and 25 pixels from the left.

Now when you set a max width, adding auto will automatically center that element so that the left and right have an equal amount of pixels separating the element from the parent container's borders.

Does that answer your question?

1 Answer

The value of said property is adjusted automatically according to the content or the context of the element.

For example, a block-level element with height: auto will grow taller as it contains more text. For another example, a block element with margin: 0 auto will have the left and right margins increased until it becomes centered along the y-axis of the viewport.

It really depends on the property you give the value to, different properties behave differently depending on the content and context.

Source