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

Igor Kałużny
Igor Kałużny
4,337 Points

Centering a general class vs a specific class.

Hi, I'm confused why certain property values doesn't work when applied to more general classes, but apply when set to more specific classes. For example:

HTML
<div class="general">
   <p></p>
<div class="specific">
   <img>
</div>    specific
</div>    general

CSS
.general { 
margin: 0 auto; } - doesn't center img in .specific

.specific { 
margin: 0 auto; } - centers img

2 Answers

bhuwan shrestha
PLUS
bhuwan shrestha
Courses Plus Student 8,489 Points

Margin: 0 auto; CSS rule will centre the HTML element that it has been defined but it doesn't centre it's content. Either you can use text-align property or you can define the margin: 0 auto rule to the child element that you want be centred.

Its not classes, but rather HTML scope you are looking at. I think this simile may work: think of the parent div or the (div with the class of general) as a house and the child div(div class specific) as a room in that house. if you wanted to move the house you can, but the room inside the house stays the same, and if you wanted to change the size of the house the you can, and the room will also change in proportion of the house (if using % for widths). Now to change a room, you need to select the room and to change this, you need to select the child div inside the parent.

Igor Kałużny
Igor Kałużny
4,337 Points

Thank you very much so the problem wasnt in the code but in my understanding of html.