0

When I try to target all anchor elements inside the p element using a descendant selector. Rules only apply to child elements and not the nested elements.

p a {
  display: inline;
  color: red;
}
<p>
  <a href="http://www.reddit.com">Go to Reddit website</a>
  <a href="http://www.quora.com">Go to QUORA</a>
  <div>
    <a href="http://www.channel9.com">channel9</a> 
  </div>
</p>
Aslam
  • 9,204
  • 4
  • 35
  • 51
rajini raja
  • 99
  • 3
  • 13

5 Answers5

1

A p tag cannot have a div tag inside it. Since this is an invalid html your browser corrects that for you. It closes the p tag as soon as it sees the div tag. And also adds a starting p tag for the closing p tag

Your actual HTML

<p>
   <a href="http://www.reddit.com">Go to Reddit website</a>
   <a href="http://www.quora.com">Go to QUORA</a>
   <div>
        <a href="http://www.channel9.com">channel9</a>  
   </div>
</p>

What it actually renders to

<p>
   <a href="http://www.reddit.com">Go to Reddit website</a>
   <a href="http://www.quora.com">Go to QUORA</a>
</p>
<div>
   <a href="http://www.channel9.com">channel9</a>  
</div>
<p></p>

And since the a tag with text channel9 is not inside a p tag, your css does not have any effect

Clyde Lobo
  • 9,126
  • 7
  • 34
  • 61
0

Seems your html is invalid, try to replace p with div. It should work. Something like below -

div a {
  display: inline;
  color: red;
}
<div>
  <a href="http://www.reddit.com">Go to Reddit website</a>
  <a href="http://www.quora.com">Go to QUORA</a>
  <div>
    <a href="http://www.channel9.com">channel9</a>  
  </div>
</div>

For details - Why <p> tag can't contain <div> tag inside it?

Community
  • 1
  • 1
pradeep1991singh
  • 8,185
  • 4
  • 21
  • 31
0

You cannot use div inside a p element

p a {
  display: inline;
  color: red;
}
<p>
  <a href="http://www.reddit.com">Go to Reddit website</a>
  <a href="http://www.quora.com">Go to QUORA</a>
  <span>
    <a href="http://www.channel9.com">channel9</a> 
  </span>
</p>
Aslam
  • 9,204
  • 4
  • 35
  • 51
0
<p>
   <a href="http://www.reddit.com">Go to Reddit website</a>
   <a href="http://www.quora.com">Go to QUORA</a>
   <div>
        <a href="http://www.channel9.com">channel9</a>  
   </div>
</p>

css

a {
   display: inline;
   color: red;
}
Alex
  • 409
  • 3
  • 18
0

Please use this code instead of above code.

.clr-rd a {
  color: red;
  display: inline;
}
<div class="clr-rd">
  <p>
    <a href="http://www.reddit.com">Go to Reddit website</a>
    <a href="http://www.quora.com">Go to QUORA</a>
    <div>
      <a href="http://www.channel9.com">channel9</a> 
    </div>
  </p>
</div>
Andrew Bone
  • 7,092
  • 2
  • 18
  • 33
M. Lak
  • 903
  • 9
  • 18