CSS Selectors help to select HTML elements (ex: DIV, P, H1) to apply styles.
CSS selector does the following works.
Why we need CSS selectors?
Every HTML page is a collection of HTML elements. Internally all HTML elements are linked using DOM. Different CSS selectors are used to find/select HTML elements from the DOM tree to style.
“*” denotes universal selector. Universal selector is used to select every HTML element to apply styles.
div *{
color:red;
}
In the above example, every child element under the DIV element is styled.
Universal selector vs Body selector:
Universal selector:
Body selector:
Class selector is used to select CSS class applied HTML elements to apply styles. Class selector starts with ‘.” followed by name(ex: .section). This is one of the mostly used selector.
.section{
color:red;
position:relative;
left:50px;
}
.section-heading {
font-weight:bold;
}
In the above example,
ID selector is used to select specified ID applied HTML element. ID selector starts with ‘#” followed by name(ex: #section1). ID to be unique to an HTML page.
#section-heading1 {
color: red;
}
#section-heading2 {
color: green;
}
In the above example, P elements are styled based on ID.
Note: CSS still works when it finds duplicate ID(more than one element use same ID) in a page. But JavaScript wouldn’t work properly when it access duplicate ID directly in a page.
Element selector is used to select HTML elements based on element type(ex: p, div).
p {
color:red;
}
In the above example, text color set to all P elements.
Note: If more than one CSS style declarations apply to the same element then browser decides which specific CSS style declaration to be applied to the element based on some predefined rules. This is called Specificity.
Descendant selector is used to select specified descendant elements under the parent element.
In Descendant selector syntax has two part. First part specifies parent level element and second part specifies descendant level elements.
#section1 p {
color:red;
}
In the above example, all P(descendant) elements under the DIV(parent) element are styled.
Note: Descendant elements includes all level(child, grandchild etc.,) child elements.
Descendant vs Child selectors:
Child selector is used to select specified direct child elements under the parent element.
Child selectors syntax has two parts. First part specifies parent element and second part specifies child elements.
#section1 > p {
color:red;
}
In the above example, all immediate P child elements under the parent element(div id=’#section1’) are styled. Grandchild P element is not styled.
Adjacent sibling selector is used to select a specified element which is immediate next to another specified element. Both elements should be in the same level.
Adjacent sibling selector syntax has two parts(S1 + S2). Second part element(E2) is a target element to select which is immediate next to first part element(S1). Both specified elements are in the same level.
div + p {
color:red;
}
In the above example, first P element after DIV element is only styled.
General sibling selector is used to select all specified sibling elements of another specified element.
General sibling selector syntax has two parts(S1 ~ S2). Second part elements(S2) are target elements to select which are available after first part element(S1). Both specified elements should be in the same level.
div ~ p {
color:red;
}
In the above example, all P elements after DIV element are styled.
Adjacent Sibling(S1 + S2) vs General Sibling(S1 ~ S2) selectors:
This attribute selector([attribute]) is used to select elements with specified attribute.
div[data-section] {
border:1px solid green;
}
In the above example, DIV elements with attribute “data-section” are styled.
[data-section] {
border:1px solid green;
}
In the above example, all elements with attribute([data-section]) are styled.
This attribute selector([attribute = value]) is used to select elements with specified attribute and value.
div[data-section="section2"] {
border:1px solid green;
}
In the above example, DIV element is styled based on attribute(“data-section”) and attribute value(="section2").
This attribute selector([attribute ~= value]) is used to select elements with specified attribute and attribute value contains specified word. Value can have one word or whitespace separated list of words.
[data-value ~="result"] {
border:1px solid green;
}
In the above example, elements with attribute(“data-value”) and attribute value contain word “result” are styled.
This attribute selector(attribute |= value) is used to select elements with specified attribute and attribute value exactly equal to a specified value or value starts with specified value immediately followed by a “-“ (hyphen).
[data-value |="result"] {
border:1px solid green;
}
[lang |= "fr"] {
color:red;
}
In the above example,
This attribute selector([attribute ^= value]) is used to select elements with specified attribute and attribute value starts with specified value.
[href ^="https"] {
font-size:1.5em;
}
In the above example, elements with attribute(“href”) and attribute value starts with “https” are styled.
This attribute selector([attribute $= value]) is used to select elements with specified attribute and attribute value ends with specified value.
[href $=".com"] {
font-size:1.5em;
}
In the above example, element with attribute “href” and attribute value ends with “.com” is selected.
This attribute selector([attribute *= value]) is used to select elements with specified attribute and attribute value should contain specified value.
[href *=".com"] {
font-size:1.5em;
}
In the above example, elements with attribute “href” and attribute value contain “.com” are styled.
Note:
:first-child pseudo-class selector is used to select a specified first child element under the parent element.
Note: Pseudo-class selectors have the following syntax.
selector:pseudo-class{ property:value; }
p:first-child {
border:1px solid red;
}
In the above example,
:last-child pseudo-class selector is used to select a specified last child element under the parent element.
p:last-child {
border:1px solid red;
}
In the above example, last child P elements under the parent element are styled.
:only-child pseudo-class selector is used to select a specified element when it is the only child element(child element count should be one and that child element should be a specified element) under the parent element.
p:only-child {
border:1px solid red;
}
In the above example, P element is the only child element of the one DIV element and it is styled.
:only-of-type pseudo-class selector is used to select a specified element when it is the only child of its type(child element count should be one based on specified element type) under the parent element.
p:only-of-type{
border:1px solid red;
}
In the above example, three P elements are styled. Each styled P element is the only child element of its type under each parent.
:first-of-type pseudo-class selector is used to select a specified first element of its type under the parent element.
p:first-of-type{
border:1px solid red;
}
In the above example, three P elements are styled. Each styled P element is the first element of specified type under the parent element.
Note: :first-child,:last-child,:only-child,:only-of-type,:first-of-type pseudo class selectors select only zero/one element under each parent. But pseudo class selectors name start with nth-xxx select zero/one/more elements under each parent.
:nth-child(n) pseudo class selector is used to select specified every nth child element under the parent element.
p:nth-child(2n+1){
border:1px solid red;
}
In the above example, 2n+1 formula values are 2*0+1=1, 2*1+1 = 3, 2*2+1 = 5, 2*3+1=7. But 3rd element is not a P element. So 1st, 5th and 7th P elements are styled.
:nth-last-child(n) pseudo class selector is used to select specified every nth child element(selection order starts from bottom to top) under the parent element.
p:nth-last-child(even){
border:1px solid red;
}
In the above example, applicable even numbers are 2,4 and 6. But 2nd and 4th elements (selection order starts from bottom to top) are not P elements and those are not styled. 6th element is a P element and it is styled.
:nth-of-type(n) pseudo class selector is used to select specified every nth child element of its type under the parent element.
p:nth-of-type(2n+1){
border:1px solid red;
}
In the above example, 2n+1 formula values are 2*0+1=1, 2*1+1 = 3, 2*2+1 = 5. Here above order only applicable to P elements. So 1st, 3rd and 5th P elements are styled based on its type.
:nth-last-of-type(n) pseudo class selector is used to select specified every nth child element of its type(selection order starts from bottom to top) under the parent element.
p:nth-last-of-type(2n+1){
border:1px solid red;
}
In the above example, 2n+1 formula values are 2*0+1=1, 2*1+1 = 3, 2*2+1 = 5. Here above order only applicable to P elements from bottom to top. So 1sth and 3rd P elements(selection order starts from bottom to top) are styled.
:not(selector) negation pseudo-class selector is used to select specified elements except parameter argument.
p:not(#key-val) {
color:#ff0000;
}
In the above example, all P elements except element with ID = “key-val” are styled.
:link pseudo-class is used to select unvisited links.
a:link {
color:green;
}
In the above example, unvisited <a> elements are styled (when page first loads).
:visited pseudo-class is used to select visited links.
a:visited {
color:red;
}
In the above example, visited two <a> elements are styled.
:active pseudo-class is used to select currently active element.
p:active, a:active{
font-size:22px;
color:#4cff00;
}
:focus pseudo-class is used to select currently focused element.
:focus {
color:#ff0000;
font-size:22px;
}
Note: :focus pseudo-class applies to focusable elements only.
:hover pseudo-class is used to select an element when point device over on it.
p:hover {
color:#ff0000;
text-decoration:underline;
}
Note: :hover pseudo-class applies to focusable and non-focusable elements.
:active vs :focus
When mouse click happens
:focus vs :hover
:root pseudo-class selector is used to select root element. Root element refers HTML element.
:root {
background-color:lightgreen;
margin:25px;
}
In the above example,
Note: Child elements inherit parent element’s many(not all) properties.
::before pseudo-element is used to insert content at the beginning of the specified element content. Inserted content will not be available in the DOM.
div:before{
content:"Section ";
color:#ff0000;
}
In the above example, content(“section”) is added before every DIV element.
Pseudo-elements vs Pseudo-class:
::after pseudo-element is used to insert content at the end of the specified element content. Inserted content will not be available in the DOM.
p:after{
content:url("tasks-ok.png");
}
In the above example, content(an image) is added after every P element.
::first-line pseudo-element is used to select first line of the block level(ex: P, DIV) element. This pseudo-element is not applicable on inline(ex: SPAN, EM) elements.
p:first-line{
color:#ff6a00;
font-size:15px;
}
In the above example, first line of the P element is styled. P element is a block level element.
::first-letter pseudo-element is used to select first letter of the block level(ex: P, DIV) element. This pseudo-element is not applicable on inline(ex: SPAN, EM) elements.
p:first-letter {
font-size:50px;
}
In the above example, first letter of the P(block level) element is styled. SPAN(inline) element is not styled.
Related selectors are explained using the following links.