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 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).