Advanced CSS Selectors
Lesson 12 โข Expert Track
What You'll Learn
๐ก Think of It Like This
Basic selectors are like addressing a letter to "John". Advanced selectors are like saying "the third John who lives on Main Street, but only when he's at home." They give you surgical precision โ you can target any element based on its state, position, attributes, or relationship to other elements.
1. Pseudo-classes โ Targeting Element States
Pseudo-classes select elements based on their current state โ whether they're hovered, focused, the first child, or match a pattern.
| Category | Selector | What It Targets |
|---|---|---|
| Interactive | :hover | Mouse is over element |
:focus | Element has keyboard focus | |
:active | Element is being clicked | |
:visited | Link has been visited | |
| Position | :first-child | First child of parent |
:last-child | Last child of parent | |
:nth-child(n) | Nth child (odd, even, 3n, 2n+1) | |
:not(selector) | Everything except the selector | |
| Form | :checked | Checked checkbox/radio |
:valid / :invalid | Form validation state | |
:disabled | Disabled form element |
Pseudo-classes in Action
See :hover, :nth-child, :first-child, and :not() at work
<!DOCTYPE html>
<html>
<head>
<title>Pseudo-classes</title>
<style>
body {
background: #0f172a;
font-family: system-ui, sans-serif;
padding: 30px;
color: #e5e7eb;
}
h3 { color: #22c55e; margin: 25px 0 10px; }
/* Styled list with nth-child */
ul { list-style: none; padding: 0; max-width: 400px; }
li {
padding: 12px 16px;
border-radius: 6px;
margin: 4px 0;
transition: all 0.2s;
}
/* Alternate row colours */
...2. Pseudo-elements โ Virtual Content
Pseudo-elements create virtual elements that don't exist in the HTML. They're perfect for decorative content, icons, and styling parts of text.
| Pseudo-element | What It Does |
|---|---|
::before | Inserts content before element (requires content) |
::after | Inserts content after element (requires content) |
::first-letter | Styles only the first letter of text |
::first-line | Styles only the first line of a paragraph |
::selection | Styles highlighted/selected text |
Pseudo-elements
Add decorative content and style text parts without extra HTML
<!DOCTYPE html>
<html>
<head>
<title>Pseudo-elements</title>
<style>
body {
background: #0f172a;
font-family: system-ui, sans-serif;
padding: 30px;
color: #e5e7eb;
}
h3 { color: #22c55e; margin: 25px 0 10px; }
/* ::before and ::after */
.quote {
background: #1e293b;
padding: 20px 25px;
border-radius: 8px;
font-style: italic;
font-size: 1.1rem;
position: relative;
margin: 15px 0;
}
.quote::before
...3. Combinators โ Relationship Selectors
Combinators target elements based on their relationship to other elements in the DOM tree.
| Combinator | Syntax | Targets |
|---|---|---|
| Descendant | div p | All p elements inside div (any depth) |
| Child | div > p | Only direct child p elements of div |
| Adjacent sibling | h2 + p | First p immediately after h2 |
| General sibling | h2 ~ p | All p elements after h2 (same parent) |
4. Attribute Selectors
Target elements based on their HTML attributes โ incredibly useful for styling links, inputs, and data attributes.
| Selector | Matches | Example Use |
|---|---|---|
[href] | Has the attribute | Any element with href |
[type="email"] | Exact match | Email inputs only |
[href*="youtube"] | Contains substring | Links containing "youtube" |
[href^="https"] | Starts with | Secure links |
[href$=".pdf"] | Ends with | PDF download links |
Attribute Selectors
Style links, inputs, and elements based on their attributes
<!DOCTYPE html>
<html>
<head>
<title>Attribute Selectors</title>
<style>
body {
background: #0f172a;
font-family: system-ui, sans-serif;
padding: 30px;
color: #e5e7eb;
}
h3 { color: #22c55e; margin: 25px 0 10px; }
.links { display: flex; flex-direction: column; gap: 8px; max-width: 400px; }
a {
display: block;
padding: 12px 16px;
background: #1e293b;
color: #94a3b8;
text-decoration: none;
border-radius: 6px;
...โ ๏ธ Common Mistakes
- Confusing : and :: โ Single colon
:hoveris a pseudo-class (state). Double colon::beforeis a pseudo-element (virtual content). - Forgetting content for ::before/::after โ These pseudo-elements require the
contentproperty, even if it's empty:content: ""; - Over-specific selectors โ
div.container ul li a:hoveris overly specific and fragile. Keep selectors as short as possible. - Using :nth-child when you mean :nth-of-type โ
:nth-child(2)targets the 2nd child regardless of type.:nth-of-type(2)targets the 2nd element of that specific type.
๐ Lesson Complete
You now have surgical precision when targeting CSS elements:
- โ Pseudo-classes target states: :hover, :focus, :nth-child, :not()
- โ Pseudo-elements create virtual content: ::before, ::after, ::first-letter
- โ Combinators target relationships: child (>), sibling (+, ~)
- โ Attribute selectors target by attribute: [href$=".pdf"], [type="email"]
Sign up for free to track which lessons you've completed and get learning reminders.