Structural pseudo-classes are CSS selectors used to target elements based on their position, order, or structure inside the DOM.
These selectors help developers style elements dynamically without adding extra classes manually.
They are commonly used in:
Without structural selectors, developers often need:
Structural pseudo-classes make CSS cleaner and more maintainable.
Selects elements based on their exact child position inside the parent.
Styles the second list item.
Commonly used for zebra-striped tables.
Selects elements based on their type position.
Unlike:
[code theme="dark"] :nth-child() [/code]this selector ignores different element types.
Styles the second paragraph only.
Paragraph 1
Paragraph 2
| Selector | Based On |
|---|---|
| nth-child() | Exact child position |
| nth-of-type() | Position among same element type |
Selects the first child element inside a parent.
[code theme="dark"] li:first-child{ font-weight:bold; } [/code]Only the first list item becomes bold.
Selects the last child element.
[code theme="dark"] li:last-child{ color:red; } [/code]Styles only the final child element.
Selects an element only if it is the single child inside the parent.
[code theme="dark"] p:only-child{ color:green; } [/code]Only Paragraph
Since the paragraph is the only child, styles apply.
Selects the first element of a specific type.
[code theme="dark"] p:first-of-type{ font-size:20px; } [/code]Targets the first:
[code theme="dark"] p [/code]element among siblings.
Paragraph 1
Paragraph 2
Selects the last element of a specific type.
[code theme="dark"] p:last-of-type{ color:purple; } [/code]Targets the final paragraph element.
Paragraph 1
Paragraph 2
TextSelects elements that contain no content.
[code theme="dark"] div:empty{ display:none; } [/code]Useful for hiding empty containers dynamically.
Since the div has no content:
[code theme="dark"] :empty [/code]applies.
| Selector | Purpose |
|---|---|
| :nth-child() | Select by child position |
| :nth-of-type() | Select by element type position |
| :first-child | First child element |
| :last-child | Last child element |
| :only-child | Single child element |
| :first-of-type | First element of type |
| :last-of-type | Last element of type |
| :empty | Empty elements |
Below are live examples of commonly used CSS structural pseudo-class selectors using modern layouts and Tailwind CSS utility classes.
| Selector | CSS | Live Demo |
|---|---|---|
| :nth-child(2) | [code theme="dark"] li:nth-child(2){ background:#dbeafe; } [/code] |
|
| :nth-of-type(2) | [code theme="dark"] p:nth-of-type(2){ color:blue; } [/code] |
HeadingParagraph 1 Paragraph 2 |
| :first-child | [code theme="dark"] li:first-child{ font-weight:bold; } [/code] |
|
| :last-child | [code theme="dark"] li:last-child{ color:red; } [/code] |
|
| :only-child | [code theme="dark"] p:only-child{ color:green; } [/code] |
Only Paragraph
|
| :first-of-type | [code theme="dark"] p:first-of-type{ font-size:20px; } [/code] |
HeadingFirst Paragraph Second Paragraph |
| :last-of-type | [code theme="dark"] p:last-of-type{ color:purple; } [/code] |
Paragraph 1 Last Paragraph Span Element |
| :empty | [code theme="dark"] div:empty{ display:none; } [/code] |
Empty div hidden by CSS
|
| Name | Role |
|---|---|
| Alex | Designer |
| John | Developer |
| Sarah | Manager |
Structural pseudo-classes are essential modern CSS selectors for creating dynamic and maintainable UI systems.