Introduction: CSS pseudo classes apply styles to the HTML elements based on some characteristics which cannot be specified using element attributes/classes/IDs. CSS structural pseudo classes are part of the CSS pseudo classes. Here different CSS structural pseudo classes are explained with examples and DOM tree.
Selects first child element under the parent element when first child element is a specified element.
Example:
<!DOCTYPE HTML>
<html>
<head>
<title> CSS Pseudo Class - :first-child</title>
<style type="text/css">
p:first-child {
background-color: lightgreen;
}
</style>
</head>
<body>
<div>
<p> DIV1 - 1st child P element</p>
</div>
<div>
<p>DIV2 - 1st child P element</p>
<p>DIV2 - 2nd child element</p>
<p>DIV2 - 3rd child element</p>
</div>
</body>
</html>
In the above example,
Result:
Selects last child element under the parent element when last child element is a specified element.
Example:
<!DOCTYPE HTML>
<html>
<head>
<title> CSS Pseudo Class - :last-child</title>
<style type="text/css">
p:last-child {
background-color: lightgreen;
}
</style>
</head>
<body>
<div>
<p> DIV1 - first/last child P element</p>
</div>
<div>
<p>DIV2 - 1st child P element</p>
<p>DIV2 - 2nd child element</p>
<p>DIV2 - 3rd child element</p>
</div>
</body>
</html>
In the above example,
Result:
Selects first child element of its type under the parent element.
Example:
<!DOCTYPE HTML>
<html>
<head>
<title> CSS Pseudo Class - :first-of-type</title>
<style type="text/css">
p:first-of-type {
background-color:lightgreen;
}
</style>
</head>
<body>
<div>
<p> DIV1 - 1st P type element</p>
</div>
<div>
<div>DIV2 - 1st DIV type element</div>
<p>DIV2 - 1st P type element</p>
<p>DIV2 - 2nd P type element</p>
<span>DIV2 - 1st SPAN type element</span>
</div>
</body>
</html>
In the above example,
Result:
Selects last child element of its type under the parent element.
Example:
<!DOCTYPE HTML>
<html>
<head>
<title> CSS Pseudo Class - :last-of-type</title>
<style type="text/css">
p:last-of-type {
background-color:lightgreen;
}
</style>
</head>
<body>
<div>
<p> DIV1 - first/last P type element</p>
</div>
<div>
<div>DIV2 - 1st DIV type element</div>
<p>DIV2 - 1st P type element</p>
<p>DIV2 - 2nd P type element</p>
<span>DIV2 - 1st SPAN type element</span>
</div>
</body>
</html>
In the above example,
Result:
Selects a specified element if it is the only child element under the parent element.
Example:
<!DOCTYPE HTML>
<html>
<head>
<title> CSS Pseudo Class - :only-child</title>
<style type="text/css">
p:only-child {
background-color: lightgreen;
}
</style>
</head>
<body>
<div>
<p>P element</p>
</div>
<div>
<p>P element1</p>
<p>P element2</p>
</div>
</body>
</html>
In the above example,
Result:
Selects a specified child element if it is the only child element of its type under the parent element.
Example:
<!DOCTYPE HTML>
<html>
<head>
<title> CSS Pseudo Class - :only-of-type</title>
<style type="text/css">
p:only-of-type {
background-color: lightgreen;
}
</style>
</head>
<body>
<div>
<span>DIV1 - SPAN element</span>
<p>DIV1 - P element</p>
</div>
<div>
<p>DIV2 - P element1</p>
<p>DIV2 - P element2</p>
</div>
</body>
</html>
In the above example,
Result:
Selects one or more child elements based on its type under the parent element.
Example:
<!DOCTYPE HTML>
<html>
<head>
<title> CSS Pseudo Class - :nth-of-type</title>
<style type="text/css">
p:nth-of-type(n+1) {
background-color: lightgreen;
}
</style>
</head>
<body>
<div>
<div>DIV type element1</div>
<p>P type element1</p>
<p>P type element2</p>
<div>DIV type element2</div>
<p>P type element3</p>
<div>DIV type element3</div>
</div>
</body>
</html>
In the above example,
Result:
Selects one or more child elements (count starts from bottom to top) based on its type under the parent element.
Example:
<!DOCTYPE HTML>
<html>
<head>
<title> CSS Pseudo Class - :nth-last-of-type</title>
<style type="text/css">
p:nth-last-of-type(2n+1) {
background-color: lightgreen;
}
</style>
</head>
<body>
<div>
<div>DIV type element1</div>
<p>P type element1</p>
<p>P type element2</p>
<div>DIV type element2</div>
<p>P type element3</p>
<p>P type element4</p>
<div>DIV type element3</div>
</div>
</body>
</html>
In the above example,
Result:
This is a negation pseudo-class selector. This takes simple selector as an argument and selects elements that are not represented by the argument.
Example:
<!DOCTYPE HTML>
<html>
<head>
<title> CSS Pseudo Class - :not</title>
<style type="text/css">
p:not(.elmnt-imp) {
background-color:lightgreen;
}
</style>
</head>
<body>
<div>
<p>DIV1 - P element1</p>
<p class="elmnt-imp">DIV1 - P element2</p>
</div>
<div>
<div>DIV2 - DIV element1</div>
<p>DIV2 - P element1</p>
<p>DIV2 - P element1</p>
<span>DIV2 - SPAN element</span>
</div>
</body>
</html>
In the above example,
Result:
Selects empty elements.
Example:
<!DOCTYPE HTML>
<html>
<head>
<title> CSS Pseudo Class - :not</title>
<style type="text/css">
p {
border: 1px solid lightgreen;
}
p:empty {
display: none;
}
</style>
</head>
<body>
<p>P element</p>
<p> </p>
<p></p>
</body>
</html>
In the above example,
Result:
Selects root element. Root element refers the HTML element.
Example:
<!DOCTYPE HTML>
<html>
<head>
<title> CSS Pseudo Class - :root</title>
<style type="text/css">
:root{
background-color:lightgreen;
color:red;
font-weight:bold;
border:1px solid red;
}
</style>
</head>
<body>
<p>P element1</p>
<p>P element2 </p>
<div>DIV element</div>
</body>
</html>
In the above example,
Result:
Note: 1) Selected elements are marked (node border color is changed) and styled in the DOM tree. 3) Use CSS Pseudo-Class link to get all the CSS pseudo classes details.
Related selectors are explained using following links.