2

the CSS rule which says element element according to this link http://www.w3schools.com/cssref/css_selectors.asp is not applied in below code . What's the issue in this ?

 <!DOCTYPE html>
    <html>
    <head>
    <style>
    p div{
        text-align: center;
        color: red;
    }
    </style>
    </head>
    <body>
    
    <h1 class="">This heading will not be affected</h1>
    <p><div class="center">This paragraph will be red and center-aligned.</div></p>
    
    </body>
    </html>
LF00
  • 27,015
  • 29
  • 156
  • 295
Vishnu Shekhawat
  • 1,245
  • 1
  • 10
  • 20

2 Answers2

4

It's not the rule, per se, it's the HTML, which is invalid. You can't wrap a <div> inside a <p>. Put the <p> inside the <div> and swap the rule to div p { and it will work.

junkfoodjunkie
  • 3,168
  • 1
  • 19
  • 33
2

Small syntax changes ;-)

<!DOCTYPE html>
<html>
<head>
    <style>
        p.red{
            text-align: center;
            color: red;
        }
    </style>
</head>
<body>

<h1 class="">This heading will not be affected</h1>
<p class="red">This paragraph will be red and center-aligned</p>

</body>
</html>
Anna Jeanine
  • 3,975
  • 10
  • 40
  • 74