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

Design Web Typography Final Project: Create a Typographic Site Header and Summary

Pitrov Secondary
Pitrov Secondary
5,121 Points

What is the difference between text-align: center; and margin: auto;?

When he said to take text-align to center. I thought why not margin to auto, and what would be the consequences to that. when do I use margin: auto; instead of text-align: center;.

khay sing
khay sing
6,806 Points

text-align=center used to align the content (text for example) to center, however margin: auto is used to align the element itself to center.

1 Answer

Steven Parker
Steven Parker
231,248 Points

These are both centering techniques, but they work on different kinds of elements. For a full-width block element, such as a heading or div, the margin setting would have no effect but the text-align would work.

On the other hand, if you change the display mode to "table", then text-align would do nothing but the margin would be effective.

As khay sing pointed out, this relates to whether you are affecting the position of the element itself or the contents. Try some experiments after adding a border to the element to make the difference obvious.

Pitrov Secondary
Pitrov Secondary
5,121 Points

Thank you for the comment