Vishnu Shekhawat Vishnu Shekhawat - 5 months ago 32
CSS Question

css is not applied using this rule "element element". whats the issue in this code?

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>




Answer

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.