0

css selector specificity is not behaving the expected way.

this is the HTML Code

   <nav>
      <p>This is the navigation</p>
      <a href="blog.html">Blog</a>
      <a href="#">Challenges</a>
      <a href="#">Flexbox</a>
      <a href="#">CSS Grid</a>
   </nav> 

this is the CSS

this comes first

   p {
      font-size: 22px;
      line-height: 1.5;
    }

after that this comes

 nav{
    font-size:18px;
    }

as per my understanding in webpage p elememt inside nav element should have 18px but it is having 22px.

5
  • No, style for p element has more weight, so it is 22px Commented Mar 17, 2023 at 9:18
  • 1
    p is a child of nav, so its specificity will be higher. The solution depends on your code and what you want. Why did you add font-size: 22px; if you don't want it to be applied? Commented Mar 17, 2023 at 9:19
  • Also, keep in mind that rarely the order of rules matter in CSS. There are some cases but they are residual and unwanted Commented Mar 17, 2023 at 9:20
  • The nav{ font-size:18px } targets the <nav> tag, not the <p> tag. If p has its own specific font-size: 22px, it won't inherit from the parent (nav). Even if you give the parent a class and an id and target it like nav.specialClass#myId, the p will use its own style, unless you use e.g. p{ font-size: inherit}
    – qrsngky
    Commented Mar 17, 2023 at 9:22
  • Thanks for your timely response now i am totally clear.
    – ShivBalaji
    Commented Mar 18, 2023 at 17:31

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.