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 Selectors Selectors - Beyond the Basics Attribute Selectors

Li Mbo
Li Mbo
5,096 Points

what is the difference between div[id="name1"] and div #name1 ?

Are they the same?

1 Answer

Kevin Korte
Kevin Korte
28,149 Points

No, they are not. div[id="name1"] would select this html

 <div id="name1"></div>

where div #name1 would select this html

<div>
  <div id="name1"></div>
  <!-- this element with id of name1 would be anything; an anchor, h or p tag, form input, or any other element that supports id -->
</div>

As you can see, two very different HTML.

Li Mbo
Li Mbo
5,096 Points

Extremely helpful ;D and to the point, thanks a lot Kevin!

Kevin Korte
Kevin Korte
28,149 Points

Sure thing, you're welcome. If you want to read about all of the attribute selectors, this is my favorite article that explains them.

https://css-tricks.com/attribute-selectors/

Gennadiy Anikeev
Gennadiy Anikeev
2,559 Points

But what about div#name1? Is it the same as div[id="name1"]?

Kevin Korte
Kevin Korte
28,149 Points

Hey Gennadiy, Yes, they would target the same selector.