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.

CSS CSS Layout Techniques Positioning Schemes Absolute Centering

Lim Yongtaek
Lim Yongtaek
3,638 Points

Why do we need to write the properties "top, right, bottom, left to 0" for absolute centering?

.icon::after {
    content: "";
    display: block;
    width: 150px;
    height: 90px;
    background: url('../img/icon.png') no-repeat;
    background-size: 100%;
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    margin: auto;
}
Ehsan Raza
Ehsan Raza
263 Points

That is because the auto margin will do the work for you. You define "0" so that by default the element is not position either top, left, right and bottom. I'm no expert at css and i'm fairly new but this is what i think why we use it.

Lim Yongtaek
Lim Yongtaek
3,638 Points

Hi Raza,

You have explained to me like a professional. Now I can see clearly why we use it.

Hey Lim Yongtaek,

The top, left, bottom, and right positioning properties are set to 0, so that the image will go to the origin of the containing element which starts at the top left corner. And then automatic margins are applied via the margin: auto property declaration as Ehsan Raza said.

Does everything else about that positioning make sense, Lim?

2 Answers

Muhammad Rizwan
Muhammad Rizwan
8,595 Points

margin: 0 auto; will also work to center the contents.

It will center the center contents horizontally but not vertically.

Muhammad Rizwan
Muhammad Rizwan
8,595 Points

You are right Marcus Parsons