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 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,109 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,109 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,109 Points

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