Zesty
Zesty

Reputation: 3001

How do I select an element within a class with CSS?

Sample:

<html>
  <body>
    <div class="myclass">
      <span>Some text</span>
    </div>
  </body>
</html>

How do I select the span inside myclass?

Is it:

span.myclass {
  color: red;
}

It doesn't seem to get applied. If I Insect Element and set the same style it gets applied.

Upvotes: 1

Views: 2998

Answers (4)

tomsullivan1989
tomsullivan1989

Reputation: 2780

The following will select span elements within myclass:

.myclass span 
{
    color: red;
}

The CSS you have selects span elements that have the class myclass, e.g.:

<span class="myclass">Some text</span>

CSS selectors get evaluated right to left.

Upvotes: 5

like this

.myclass span{ color:blue;}

you go in the child from the parent.

Upvotes: 1

phron
phron

Reputation: 1845

you have to do

.myclass span{ color:red;}

Which can be read "Select every span in any element with class named 'myclass'. CSS rules are evaluated right to left

Upvotes: 2

James Montagne
James Montagne

Reputation: 78750

If you are trying to select a span within .myclass then it is this:

.myclass span

What you have is looking for a span which has the class of myclass. No such span exists so there is no effect.

Upvotes: 2

Related Questions