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 Selectors Basic Attribute Selectors

Paul Trimor
Paul Trimor
5,388 Points

Whenever I use the [class] selector, it selects the entire body.

for example:

<html>
<h1 class="hello"> Heading </h1>
<h1> another heading </h1>

</html>

css

[class] {
 color:red;
}

The output of the code on codepen is that both headings are colored red. If I change the code to: [class] { background-color: red; }

The out put of this code is the entire body is red. I believe this is a fault in the website.

Can you link the pen?

You might also want to read up on attribute selectors.

4 Answers

Kevin Kenger
Kevin Kenger
32,834 Points

Hey Paul,

I agree with Dustin Matlock that using the class name is the best way to go about selecting the h1 element. But if you're still curious, I inspected the page with Chrome, and it looks like the <html> tag was given a class attribute with no value.

Here's a screenshot

I'm assuming that's why the entire document is being selected.

Here is how your code should look.

<html>
 <body>
   <h1 class="hello"> Hello </h1>  
   <h1> GOODBYE </h1>
 </body>

</html>
.hello {
  background-color: red;
}
Paul Trimor
Paul Trimor
5,388 Points

Thankss for all the replys. The issue wasn't trying to select the H1 element. I was trying to use the class attribute selector to see if it works.