CSS Pseudoklasse
- Previous page CSS combinators
- Next page CSS Pseudoelement
Wat is een pseudo-klasse?
Pseudo-klassen worden gebruikt om speciale staten van elementen te definiëren.
Bijvoorbeeld, het kan worden gebruikt voor:
- Stel de stijl in wanneer de muis op het element rust
- Stel verschillende stijlen in voor bezochte en onbezochte links
- Stel de stijl van het element in wanneer het focus krijgt
Syntaxis
Syntaxis van de pseudo-klasse:
selector:pseudo-klasse { eigenschap: waarde; }
Anker-pseudo-klasse
Links kunnen op verschillende manieren worden weergegeven:
Example
/* Nog niet bezochte link */ a:link { color: #FF0000; } /* Bezoekte link */ a:visited { color: #00FF00; } /* Link met de muis over */ a:hover { color: #FF00FF; } /* Geselecteerde link */ a:active { color: #0000FF; }
Opmerking:a:hover
Moet naast de CSS-definitie a:link
en a:visited
zijn om effectief te zijn!a:active
Moet naast de CSS-definitie a:hover
Ze worden pas actief na de definitie! De naam van de pseudo-klasse is niet hoofdlettergevoelig.
Pseudo-klassen en CSS-klassen
Pseudo-klassen kunnen worden gecombineerd met CSS-klassen:
Wanneer je de muis over de link in het voorbeeld houdt, verandert de kleur:
Example
a.highlight:hover { color: #ff0000; }
Hover over de <div>
Gebruik op het <div>-element :hover
Pseudo-klasse voorbeeld:
Example
div:hover { background-color: blue; }
Simple tooltip hover
Hover over the <div> element to display the <p> element (similar to a tooltip effect):
嘿嘿,我在这里!
Example
p { display: none; background-color: yellow; padding: 20px; } div:hover p { display: block; }
CSS - :first-child pseudo-class
:first-child
Pseudo-classes match the specified element: it is the first child of another element.
Match the first <p> element
In the following example, the selector matches any <p> element that is the first child of any element:
Example
p:first-child { color: blue; }
Match the first <i> element within all <p> elements
In the following example, the selector matches the first <i> element within all <p> elements:
Example
p i:first-child { color: blue; }
Match all <i> elements within the first <p> element
In the following example, the selector matches all <i> elements within the <p> element that is the first child of another element:
Example
p:first-child i { color: blue; }
CSS - :lang pseudo-class
:lang
Pseudo-classes allow you to define special rules for different languages.
In the following example,:lang
Define quotes for <q> elements with the attribute lang="en":
Example
<html> <head> <style> q:lang(en) { quotes: "~" "~"; } </style> </head> <body> <p>Some text <q lang="no">A quote in a paragraph</q> Some text.</p> </body> </html>
More examples
- Add different styles to hyperlinks
- This example demonstrates how to add other styles to hyperlinks.
- Use :focus
- This example demonstrates how to use the :focus pseudo-class.
All CSS pseudo-classes
Selector | Example | Example description |
---|---|---|
:active | a:active | Select active links. |
:checked | input:checked | Select each checked <input> element. |
:disabled | input:disabled | Select each disabled <input> element. |
:empty | p:empty | Select each <p> element without child elements. |
:enabled | input:enabled | Kies elk ingeschakeld <input> element. |
:first-child | p:first-child | Kies elke <p> element die de eerste kind is van zijn ouder. |
:first-of-type | p:first-of-type | Kies elke <p> element die de eerste kind is van zijn ouder. |
:focus | input:focus | Kies het <input> element dat gefocust is. |
:hover | a:hover | Kies de link waarop de muis over gehouden wordt. |
:in-range | input:in-range | Kies <input> elementen met een waarde binnen een specifieke range. |
:invalid | input:invalid | Kies alle <input> elementen met een ongeldig waarde. |
:lang(language) | p:lang(it) | Kies elke <p> element waarvan de waarde van de lang-eigenschap begint met "it". |
:last-child | p:last-child | Kies elke <p> element die de laatste kind is van zijn ouder. |
:last-of-type | p:last-of-type | Kies elke <p> element die de laatste kind is van zijn ouder. |
:link | a:link | Kies alle niet-bezoekte links. |
:not(selector) | :not(p) | Kies elk element dat geen <p> element is. |
:nth-child(n) | p:nth-child(2) | Kies elke <p> element die de tweede kind is van zijn ouder. |
:nth-last-child(n) | p:nth-last-child(2) | Kies elke <p> element die de tweede kind is van zijn ouder, gerekend van de laatste kind. |
:nth-last-of-type(n) | p:nth-last-of-type(2) | Kies elke <p> element die de tweede kind is van zijn ouder, gerekend van de laatste kind. |
:nth-of-type(n) | p:nth-of-type(2) | Kies elke <p> element die de tweede kind is van zijn ouder. |
:only-of-type | p:only-of-type | Kies elke <p> element die de enige <p> element is van zijn ouder. |
:only-child | p:only-child | Kies de <p> element die de enige kind is van zijn ouder. |
:optional | input:optional | Select <input> elements without the "required" attribute. |
:out-of-range | input:out-of-range | Select <input> elements with values outside the specified range. |
:read-only | input:read-only | Select <input> elements that have specified the "readonly" attribute. |
:read-write | input:read-write | Select <input> elements without the "readonly" attribute. |
:required | input:required | Select <input> elements that have specified the "required" attribute. |
:root | root | Select the root element of the element. |
:target | #news:target | Select the currently active #news element (click on the URL containing the anchor name). |
:valid | input:valid | Select all <input> elements with valid values. |
:visited | a:visited | Select all visited links. |
All CSS pseudo-elements
Selector | Example | Example description |
---|---|---|
::after | p::after | Insert content after each <p> element. |
::before | p::before | Insert content before each <p> element. |
::first-letter | p::first-letter | Select the first letter of each <p> element. |
::first-line | p::first-line | Select the first line of each <p> element. |
::selection | p::selection | Select the part of the element chosen by the user. |
- Previous page CSS combinators
- Next page CSS Pseudoelement