-1

I would like to highlight 'The second paragraph' of p id="dd". Can I make it with nth-child? When I add "#dd" in styles, it stops to work.

<!DOCTYPE html>
<html>
<head>
<style> 
#dd p:nth-child(3) {
    background: red;
}
</style>
</head>
<body>
<p id="de">
<p>The first paragraph.</p>
<p>The second paragraph.</p>
<p>The third paragraph.</p>
<p>The fourth paragraph.</p>
</p>
<p id="dd">
<p>The first paragraph.</p>
<p>The second paragraph.</p>
<p>The third paragraph.</p>
<p>The fourth paragraph.</p>
</p>
</body>
</html>
jampjamp
  • 31
  • 1
  • 1
  • 4

3 Answers3

2

As Muhammad Usman kindely noted above,

You need to Change the "parent" <p> to a <div> and it will work.

#dd p:nth-child(3) {
  background: red;
}
<div id="de">
  <p>The first paragraph.</p>
  <p>The second paragraph.</p>
  <p>The third paragraph.</p>
  <p>The fourth paragraph.</p>
</div>
<div id="dd">
  <p>The first paragraph.</p>
  <p>The second paragraph.</p>
  <p>The third paragraph.</p>
  <p>The fourth paragraph.</p>
</div>
2

p inside a p is not allowed. Make a div and add the paragraphs in it.

https://jsfiddle.net/8fh108cL/

The relevant change in the snippet.

<div id="de">
<p>The first paragraph.</p>
<p>The second paragraph.</p>
<p>The third paragraph.</p>
<p>The fourth paragraph.</p>
</div>
<div id="dd">
<p>The first paragraph.</p>
<p>The second paragraph.</p>
<p>The third paragraph.</p>
<p>The fourth paragraph.</p>
</div>
pol
  • 2,641
  • 10
  • 16
0

You shouldn't append paragraphs inside a paragraph. Please refer to this really good post from Matt Ball.

Your approach is right, you can use #dd:nth-child(n) where n is the child you are targeting with your CSS rules.