CSS Selector¶
Types:¶
- universal --> * {} =
all tags
. - set to all element, overrides browser default, overrides inheritance.
- element --> h1 {} =
h1 tag only
- classes --> .s1{} =
<h1 class=s1>, <p class=s1>, etc
- reusable.
- id --> #s2{} =
<h1 id=s2>
- can be used only once as id is unique in html.
- Attributes --> [disabled] {} =
- Combinator --> multiple selector.
-
eg :
#id1 h1 {}
-
a.active
vsa .active
a.active
--> it will select anchor with active class.a .active
--> it will pick all desendant of achoe with class active.
Grouping rule:¶
selector1,selector1 {
//common declaration
}
Specifity¶
- scenario :
<h1 class=s1> text </h1> main.css: h1{ color : red } h1 { color : green} // h1{ color : green} h1 { color : red } // change order .s1 { color : blue } which color : ?
- cascading mean multiple style can be applied on element which leads to conflict and resolve it provides specifity
-
preference/priority of selector
-
Inheritance : parent has lowest priority.
- eg : body is parent of h1
body{ color : black} <body><h1 style="color:green"> </h1></body>
- more specific will win.
-
eg :
#id1 h1 {} //1 win h1 {} //2
-
!important
--> override specifity and will always be applied. - eg : div{ color: red !important} --> color would always be red wahtever the order, inheritance ,etc.
-
not preferred to use.
-
<p c1 c2 > text </p>
--> priority for c1 and c2..c1{ color : red} .c2{ color : blue} //win here order matter
COMBINATOR¶