tags:

views:

434

answers:

8

My markup looks like:

<div class="c1">
      <li class="c2"><a href="">blah</a></li>
</div>

I want the text blah to be red.

c1 is used other places, so I want to drill down as much as possible without affecting other markup.

+3  A: 
<style>
  div.c1 li.c2 a { color: red; }
</style>
Pointy
+8  A: 

Use this style definition in your css-file:

div.c1 li.c2 a {
  color: red;
}

PS: Having your <li> tag inside your <div>-tag without an <ul>-tag is not recommended.

Espo
A: 
.c1 .c2 a {
  color: red;
}
vikingosegundo
A: 
.c2 a
{
     color: red;
}
nasufara
A: 

The following code will do (very specific).

.c1 .c2 a { color: red; }
NawaMan
+1  A: 
<div class="c1">
      <li class="c2"><a href="">blah</a></li>
</div>
<style>
  div.c1 li.c2 a { color: red; }
</style>
rahul
+1  A: 

Use the following rule:

div.c1 li.c2 a {
    color: red;
}

This matches a tags inside of li tags with class c2 inside of div tags with class c1.

For added uniqueness, you might want to give the a tag its own class name.

Also, li tags should only appear inside of list tags. (ul or ol).
Did you mean <li class="c1">?

SLaks
A: 

The most specific CSS selectors would probably be

div.c1 > li.c2 > a:link,
div.c1 > li.c2 > a:active,
div.c1 > li.c2 > a:hover,
div.c1 > li.c2 > a:visited {
    color: red;
}

The more specific the CSS selectors, the less work for the browser's rendering engine.

However, something is wrong with your markup if this is supposed to be HTML and the <li> element's parent is a <div> instead of <ol> or <ul>.

ndim
CSS selectors are evaluated from right to left, so the "less work for the browser" part is wrong.
RegDwight
With > between the elements in the CSS selector (i.e. immediate parent-child relationship of the elements), the browser only needs to check up to three levels of nested elements, maximum for each <a> element.If you just specify "any ancestor" (i.e. just a space between the elements in the CSS selector), the browser needs to check all ancestors back to the root <html> for ever <a> element.Checking three levels max is less work than checking all levels.
ndim