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 trialPitrov Secondary
5,121 PointsWhat 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;.
1 Answer
Steven Parker
231,236 PointsThese 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
5,121 PointsThank you for the comment
khay sing
6,806 Pointskhay sing
6,806 Pointstext-align=center used to align the content (text for example) to center, however margin: auto is used to align the element itself to center.