0

.colored p{
   color: red;
}


article > .colored{
   color:powderblue;
}

.blue{
   color: white;
}
  
<!DOCTYPE html>
<html lang="ko">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
    <div class="colored">
      <p>hello</p>
      <p class="blue">hello</p>
      <div>
        <p>hello</p>
      </div>
    </div>
    <p>hello</p>
    <article>
      <div class="colored">hello</div>
    </article>
</body>
</html>

Why isn't the blue selector applied??? It's applied when I changed it(p.blue). I don`t know the difference between first case and second case ..

Wonjae
  • 1

1 Answers1

0

What is happening in second case is, p tag have parent class colored and that is why it is applying css of parent class to second p tag. Even though you have provided class blue but still parent class have high precedence.

You can use !important override precedence for color property in class blue. The !important instruction has the highest precedence of all precedence factors.

.colored p{
   color: red;
}


article > .colored{
   color:powderblue;
}

.blue{
   color: green!important;
}
  
<!DOCTYPE html>
<html lang="ko">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
    <div class="colored">
      <p>hello</p>
      <p class="blue">hello</p>
      <div>
        <p>hello</p>
      </div>
    </div>
    <p>hello</p>
    <article>
      <div class="colored">hello</div>
    </article>
</body>
</html>
Shweta
  • 31
  • 4