FAQ: The Box Model - Auto

This community-built FAQ covers the “Auto” exercise from the lesson “The Box Model”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Learn CSS

FAQs on the exercise Auto

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

1 Like

In the CSS course Learning CSS The Box Model, on Lesson 10: Auto, using it to center text, couldn’t you do the same thing with text-align: center; What is the difference and what situations would you use either one.


In the second example mentioned in this exercise the width has been set to 400px.
As a result the div will center the element greater than 400px in a content.

How is that possible? I thought the width is until 400px?

Good question!

I guess it is again a way of differentiating between HTML and CSS.

Does anyone know the correct answer?

The containing element can never be smaller than the 400px set width of this element. When the containing element is greater than 400px, the box will be centered horizontally no matter what the width of the outer one.

margin: 0 auto;


text-align: center;

Margins do not affect text alignment. We can center the element but not have an effect on text it contains. If we use text-align on the parent to align boxes it contains, that will also have an effect on the text in the boxes.

Why is it not possible to center an element that takes up the full width of the page? Wouldn’t that just make the element appear in the exact center of the webpage?

1 Like

If it spans the full width how do add margins on either side? All we can do in that case is to center the content within it.

<p>Some text</p>

The above will span the page with the text at the left edge. Giving a text-align: center; rule will center the text.

<p style="text-align: center">Some text</p>

although a style sheet would be preferred…

p {
    text-align: center;

We can also use the <center></center> element to wrap the text node:

<p><center>Some text</center></p>

However, WCAG 1.0 advises otherwise…

Use ‘text-align: center’ instead of the deprecated CENTER element.

If you’re using a border-box though, wouldn’t that solve the issue of factoring in the margins?

There would be no margins if the element itself spans the entire width. All block level elements default to 100% and border-box would have the same effect it has on narrower widths.

I understand using ‘margin: 0 auto’ removes top/bottom margins, and adds margins left/right to automatically center an element horizontally, but why doesn’t ‘margin: auto 0’ (reversing the command) center an element vertically instead?

An age old question, and one that you’ll get a multitude of answers to. It’s also the reason we have Flexbox and Grids, since in native form, CSS takes quite a beating (as in the layout author).

The document will flow where it can and stretch all the vertical limits it needs to. Horizontal is easy to control. Set the width, and Bob’s your uncle. Height is not such an easy thing to regulate, though we do have overflow fallback if a container has a set height.

We’ve brought down a document of indeterminant size and are expected to render it, by whatever style configurations we may have contrived.

It’s like a bucket of paint that we throw at the browser and let it sort out which pixels get it. Only we do help it along by structuring and directing presentation.

Sorry but i didn’t find out how to ask my own question so i just used reply.
Why do we need to give a width to the element for CSS to be able to center the element from the left and right borders of its container using the ‘‘auto’’ value? Because if we just use the ''margin-left" and “margin-right” properties we can do it without giving a width property to the element.

margin is fixed to whatever we set it. auto is not fixed but creates equal margins on both sides. The alternative is to use percentages for the margins but this also has an effect on container width when the browser is re-sized. auto handles this much more cleanly without affecting the container width if it is fixed.

There is a fine line that separates the two methods, as both are valid. Design preferences factor into our choice to use one over the other. There is no one better way.

Bottom line, be sure to test your document in all manner of device sizes to be sure the layout doesn’t break (responsive design), or at least breaks gracefully (adaptive design).

I was playing around a bit and tried to set the style to this:

#main {
padding: 40px;
text-align: center;
width: 400px;
margin: 1 auto;

I was quite surprised that the whole box was moved to the left no matter the number it was (1, 10, 100). Does that mean I cannot use different number than zero?

Not exactly, no. It means CSS does not recognize the number if it has no unit. All numbers must have a unit, save for zero. Since zero is undefined, it has no unit that can be applied (logically).

margin: 0 auto;

margin: 10px auto;