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 Basics (2014) Basic Selectors Descendant Selectors

How do I create a descendant selector that targets span?

Im having trouble creating a descendant selector that targets the span.

Thank you

style.css

index.html
<!DOCTYPE html>
<html>
  <head>
    <title>Lake Tahoe</title>
    <link rel="stylesheet" href="page.css">
    <link rel="stylesheet" href="style.css">
  </head>
  <body> 
    <header id="top" class="main-header">
      <span class="title">Journey through the Sierra Nevada Mountains</span>
      <h1>Lake Tahoe, California</h1>
    </header>
    <p>
      Lake Tahoe is one of the most <span>breathtaking attractions</span> located in California. It's home to a number of ski resorts, summer outdoor recreation, and tourist attractions. Snow and skiing are a significant part of the area's reputation.
    </p>
    <a href="#">Find out more</a>
    <div class="main-content">
      <h2>Check out all the Wildlife</h2>
      <p>
        As spawning season approaches, the fish acquire a humpback and protuberant jaw. After spawning, they die and their carcasses provide a feast for gatherings of mink, bears, and Bald eagles.
      </p>
      <a href="#">See the Wildlife</a>
    </div>
    <footer>
      <p>All rights reserved to the state of <a href="#">California</a>.</p>
    </footer>
  </body>
</html>

4 Answers

You want to select first the header and then select span in your style.css file. Then place curly braces and between the curly braces you want to specify font-size followed by a colon a space and then 26px followed by a semicolon to close the statement.

It will look like this

header span { font-size: 26px; }

If this helped, please select best answer.

Happy Coding!

Thank you

Thank you so much .

Hi Yasir,

Using a space between selectors is an explicit descendant selector. For example,

index.html
<ul>
  <li>Home</li>
  <li>About</li>
  <li>Contact</li>
</ul>
style.css
ul li {
  /* all li tags nested within a ul tag will receive this style */
  font-size: 26px;
}

For more information about descendant selectors, check out this MDN article: Descendant selectors.

Hope this helps,

Cheers

Thank you.

Well since your span has a class of title you could easily just do

.title {
  //This selector is saying select all the elements with the class of title
}

But if you want a descendent then you will want to find spans parent element which happens to be the header tag with an id of top so you could do

#top > .title {
  //This selector is saying find the element with the id of "top" and then select all the "direct children" of #top that have a class of "title"
}

Alternatively you could switch out the classes and id with the tag names header and span, but since you have an id and class on them, it would probably be better to use them than to use the tag name. If you have any more questions feel free to ask me :)

Thank you

Matt F.
Matt F.
9,518 Points

Hello,

This will select all spans that are descendents of the header. Replace 'header' with whatever ancestor you want.

header span{
  background: red;
}

If you only want the direct children of the header, then:

header > span{
  background: red;
}

The first CSS code snippet will make the background color for both spans red. The second CSS snippet using the > "combinator" will only affect the first span.

<!DOCTYPE html>
<html>
  <head>
    <title>Lake Tahoe</title>
    <link rel="stylesheet" href="page.css">
    <link rel="stylesheet" href="style.css">
  </head>
  <body> 
    <header id="top" class="main-header">
      <span>My background is red with either CSS snippet.</span>
      <h1>Lake Tahoe, California</h1>
      <div>
             <span>
             My background is red only with the first CSS code snippet because I am not a direct child of the header.
           </span>
      </div>
    </header>
  </body>
</html>

Thank you so much .