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 trialLim Yongtaek
3,638 PointsWhy 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;
}
Lim Yongtaek
3,638 PointsHi Raza,
You have explained to me like a professional. Now I can see clearly why we use it.
Marcus Parsons
15,719 PointsHey 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
8,595 Pointsmargin: 0 auto; will also work to center the contents.
Marcus Parsons
15,719 PointsIt will center the center contents horizontally but not vertically.
Muhammad Rizwan
8,595 PointsYou are right Marcus Parsons
Ehsan Raza
263 PointsEhsan Raza
263 PointsThat 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.