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 Foundations Advanced Selectors Additional Pseudo-Classes

An attribute selector problem

[id*="#"] { background-color: tomato; }

I was supposed to writ css code to target an elemeent ONLY IF its ID matches the HASH in a URL, and set its background-color to tomato.

So I used attribute selector like above, but apparently it didn't work.

Cheers, Phuc

4 Answers

Nick Nish
Nick Nish
7,304 Points

Hey @Phuc

There are many roads to success, in this case a lot of CSS selector solutions would work, including @Dustin's. If you want to use an attribute selector, you'll have to specify the type of element that you want to select: (or if you want to select all elements with an ID you can try the second line, but it's generally poor practice to overuse the (*))

div[id="id-name"] { background-color: tomato; }
*[id="id-name"] { background-color: tomato; }

How about:

div:not([id="s2"]) {
    background-color: tomato;
}

It worked as it selected the right one, however I don't think that was the purpose of the exercise. ( :not attribute selector was mentioned in the section).

It worked as it selected the right one, however I don't think that was the purpose of the exercise. ( :not attribute selector was mentioned in the section).