Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
Atlas AKIN1,675 Points
I don't understand the coding to center an element with position:absolute.
In the video, the instructor makes the parent's position relative. I got this part.
Then he makes the children's(background image) position to be absolute and then offset it so that it would be in the middle. I got this part too.
So why did he need to set the margin to be auto?
Here is the code(partial) if anyone wants:
background-size:100%; top:0; bottom:0; right:0; left:0; margin:auto;
And by the way, why did we use background-size, even though we gave it specific width-length datas?
You must set the margin to auto in order to overwrite any previous margin rules you have. If you attempt to center the element and have parent margin attributes, the child element will inherit the parent margin styling and will not truly be centered, unless you specifically add margin styling to the child element.