Loading Please Wait...
CSS Selectors are used to select the HTML elements for which the CSS style rules are applied.
General selectors are used to select elements based on tag name, class, id, and attributes.
Universal selectors selects all the elements in the HTML page. It is defined by the * symbol.
Syntax: *
*{
color: red;
}
Element selectors (Tag selectors, Type selectors) selects all the elements based on element name.
Syntax: elementname
body{
background-color: #ff0000;
}
h1{
color: #ff0000;
}
p{
font-size: 14px;
}
Class selectors selects al the elements based on name of the class attribute given to element. It is defined by the dot(.) followed by class name.
Syntax: .classname
.text-red{
color: #ff0000;
}
ID selectors selects element based on name of the id attribute given to element. It is defined by the # followed by id name.
Syntax: #idname
#navigation{
background-color: #ff4d2e;
}
Attribute selectors selects all the elements based on name of the attribute given to element. It is defined by the attribute name enclosed by [].
Syntax: [attribute], [attribute=value], [attribute~=value], [attribute|=value], [attribute^=value], [attribute$=value], [attribute*=value]
[href]{
color: #ff0000;
}
Combinator selectors selects element based on the relationship between elements.
Descendant combinator selectors selects all the elements that are descendant of first selector. It is defined by the space (" ").
Syntax: selector1 selector2
Here selector1 and selector2 can be any selectors such as element, class, id, attribute, etc.
header h1{
color: red;
}
Child combinator selectors selects all the elements that are direct children of the first selector. It is defined by the > symbol.
Syntax: selector1 > selector2
ul > li{
color: red;
}
General sibling combinator selectors selects all the elements that sibling of the first selector. Second element comes after the first element (Not necessarily immediately) and both shares the same parent. It is defined by the ~ symbol.
Syntax: selector1 ~ selector2
h1 ~ p{
color: red;
}
Adjacent sibling combinator selectors selects all the elements that direct sibling of the first selector. Second element comes just after the first element and both shares the same parent. It is defined by the + symbol.
Syntax: selector1 + selector2
h1 + p{
color: red;
}
Column combinator selectors selects all the elements which belongs to a column. It is defined by the || symbol.
Syntax: selector1 || selector2
col || td{
color: red;
}
Pseudo selectors are used to select elements based on the state of the elements.
Pseudo class selectors selects elements based on the state information of the element which is not contained in the document tree. It is defined by the : symbol.
Syntax: selector:state
a:visited{
color: green;
}
button:hover{
color: pink;
}
Pseudo element selectors selects elements based on the entity information of the element which is not contained in the document tree. It is defined by the :: symbol.
Syntax: selector::entity
p::first-letter{
font-size: 36px;
}
p::first-line{
color: green;
}
Group selectors are used to select a group of elements. It is useful when two or more elements share the same style. It is defined by the , symbol.
Syntax: selectors1, selectors2
Here selectors1 and selectors2 can be any selectors such as class, id, element, pseudo, combinators, etc.
h1, p{
color: green;
}
.text-red, .text-green{
font-size: 14px;
}
#navigation h1, .section h1, div > h1{
font-size: 24px;
}
How you feel about this blog:
Share this blog on:
If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com
Contact UsWe are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..
Copyright ©
, Lynxsia IT Solutions, All rights reserved