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

HTML

Can't call an image inside a div

Hey all,

I've had this problem in attempts in a row in two different projects.

I am placing an image inside it's own div. When I call the div it has no effect on the image. The only time I can make the image do what I want it to do is when I give the img a class and call that in my css. Here is a quick example:

In this jsbin: http://jsbin.com/ucuwiy/1/edit

My image has a "logoimg" class and is in a div with the class "logo-img". If you look at the css in my jsbin, I am using the class selector .logoimg to center my image. If you edit that class and call it by the div class my image is in by adding a "-" between logo-img, I would think it would still impact the image the same way. Unfortunately whenever I put images inside a div class and give the div class rules to align the image it never impacts the image the way I anticipate it to.

Can you not edit an image position that is inside a div class?

2 Answers

Elliott Frazier
PLUS
Elliott Frazier
Courses Plus Student 9,647 Points

To select a image inside a div you need to first select the class of the div you want selected, like this .logo-img then add img to select the img element inside the .logo-img div, like this .logo-img img.

@Elliot Thanks! I understand now. What is the best practice semantically? Should I make a separate div class for a single image? OR add a class to the image without div tags?

Elliott Frazier
Elliott Frazier
Courses Plus Student 9,647 Points

it's more semantic/clean just to give the img tag a class and not wrap it in a div. I made a quick codepen that might help.

Elliott Frazier
Elliott Frazier
Courses Plus Student 9,647 Points

it's more semantic/clean just to give the img tag a class and not wrap it in a div. I made a quick codepen that might help.