What is difference in defining class .first or p.first in css | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

What is difference in defining class .first or p.first in css

I am bit confused what is the difference in defining class like .first or p.first in css

11th Jun 2017, 7:03 AM
Rahul Roy
Rahul Roy - avatar
5 Answers
+ 5
First difference was already told by @_Retr0/-: > .first will target all (any) elements which have 'first' as class attribute > p.first will target all <p> elements which have 'first' as class attribute Second difference is that p.first has more priority than .first, so p.first rules will overwrite .first rules, even if they are previously declared... Few examples: <div class="first">a div with class 'first'</div> <div>a div without class 'first'</div> <p class="first">a p with class 'first'</div> <p>a p without class 'first'</div> <style> p.first { /* target the first <p> */ background:red; /* applied to the first <p> */ } .first { /* target the first <div> and the first <p> */ border:black solid 2px; /* applied to the first <div> and the first <p> */ background:cyan; /* applied only to the first <div> as p.first declaration has higger priority */ } </style>
12th Jun 2017, 1:43 AM
visph
visph - avatar
+ 5
can you show by example ? This would help me to Understand better.
11th Jun 2017, 9:06 AM
Rahul Roy
Rahul Roy - avatar
+ 3
A logical thinking must be there to understand this concept about which Visph has discussed
12th Jun 2017, 3:20 AM
Siddharth Saraf
+ 3
@Siddharth Saraf: If you're talking about the css selector prioriries (specificities), it's quite hard to master, but it's very interresting, helpful and powerful to be known to better handle Css ;) https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity
12th Jun 2017, 3:27 AM
visph
visph - avatar
+ 2
p.first selects all the classes which r in p tag whereas .first selects all the classes
11th Jun 2017, 8:40 AM
_Retr0/-
_Retr0/- - avatar