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