1

With this HTML code.

<div class="noote">
    <p class="first admonition-title">Noote</p>
    <p class="last">Let's noote.</p>
</div>

How can I set the color of Noote to be red with css? I mean, how can I set something for (div class="noote") and (p class="first") under it with css?

1
  • do you want to set all DIVs with the class "noote" which have a P with the class "first" in it red? Commented Jan 17, 2011 at 19:58

3 Answers 3

4

Try this:

/*this will apply for the element with class first 
inside the element with class noot */

.noote .first{    
    color:red;
}

/* If you wanted to apply a rule for both individually
you can do: */

.noote, .first{    
    border:1px solid red;
}
Sign up to request clarification or add additional context in comments.

Comments

1
div.note{
   ...
}

Refers to the div element that has the class note.

p.first{
    ...
}

Refers to the p element that has the class first.

div.note p.first{
    ...
}

Refers to the p element inside note that has the class first.

In addition, if you want to set an element child without setting a class to it,

div.note p:first-child{
    /* it refers to the first p that contains noote */
}

3 Comments

OP is asking how to refer to .first inside .noote, not just separately.
And no, that is not the correct way to use :first-child. The correct way is div.note p:first-child. See stackoverflow.com/questions/4195161/… for details.
I dint't understand it by reading the first time :) thanks for the correction :) ..I'm going to update my answer
1

@amosrivera's got it.

It's worth nooting that descendant selectors require more CPU. I always use more specific rules where possible. So instead of

.noote .first{
    backgorund:red;
}

You could say

.noote > .first{
    backgorund:red;
}

A nominal difference in most cases, but still a good habit.

Really?

Descendant selectors are inefficient... The less specific the key, the greater the number of nodes that need to be evaluated.

Google "Let's make the web faster" docs

And

Descendent selectors are a major slow down to the Safari browser

John Sykes, May 2008

Some performance tests show little impact, and most writers agree that it only makes a difference on very large documents.

But mainly, I'm just going with my programmer instinct — say what you mean, and no more.

1 Comment

this looks interesting indeed, do you have a link that proves this performance difference?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.