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

Getting only parent element:CSS?

Ok, so here's my problem. Let's say I have a paragraph element, and inside this paragraph element is a nested image. Is there a way to select only the paragraph element to apply formatting to, without selecting the image along with it?

3 Answers

Hi Tyler, Have you tried this p < img?
This means it will select any p element if there is an img element nested inside.

Best regards,
Henry

This doesn't actually exist yet, but it has been suggested - http://css-tricks.com/parent-selectors-in-css/.

Oh I see. Sorry for the misinformation.

Which properties are affecting the img that are set on the p?

A CSS only solution (if you want to call it that) would be something like below, but again I'm unsure which properties are affecting the p as well as the img so this is just an example of possibility.

p {
    font-size: 16px;
    border: 1px solid #b4d455;
}

/* This will select all images within a paragraph */
p ~ img {
    font-size: 0px;
    border: 1px solid transparent;
}

Thanks for all the replies, it turns out that I was able to figure out how to fix my issue without using a parent selector. It had to do with how I was trying to use all inline block attributes because floats never work the way I want them to, so I floated some things around(thank god for git), and I got it working. The whole parent selector thing is interesting though. Cheers