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

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