1

This is my HTML:

<dl id="id">
    <dt>test</dt>
    <dd>whatever</dd>
    <dt>test1</dt>
    <dd>whatever2</dd>
    ....
</dl>

I want to select the third dd but I cant make it work. This is what I tried:

dl#id dd:nth-child(3) {  
     color: yellow;
}

The style is not applied, whats wrong?

Thanks!

1
  • 1
    Uhh, there is no DD that's a third child, you'll have to change it to nth-child(6) to match that DD. You'll have to use :nth-of-type to match the element and which number it is. Commented Apr 24, 2013 at 11:35

2 Answers 2

6

The 3rd child of #id is actually the <dt>test1</dt>, so the selector does not match anything.

It would be more appropriate to use the :nth-of-type selector here:

dl#id dd:nth-of-type(3)

It is also possible to take advantage of the rather rigid HTML structure (pairs of <dt>/<dd>) and simply write dl#id :nth-child(6), but in this case it's pointless (browser support is AFAIK the same for both selectors).

Sign up to request clarification or add additional context in comments.

2 Comments

Yeah, thats it, thanks! Is there a way to continue from that selected element? Like dl#id dd:nth-of-type(3) li
@user1856596: If the li is in a list within the dd, sure.
-1

You could also do it with jquery:

$("dl#id dd")

This would give you all the dd within the dl. With an index you can then access the nth element For the third this would be:

$("dl#id dd").eq(2)

You can then add a style property:

$("dl#id dd").eq(2).css("color", "yellow")

I hope this answered your question

Comments

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.