CSS Pseudo-Klassen
Was sind Pseudoklassen?
Eine Pseudoklasse wird verwendet, um einen speziellen Zustand eines Elements zu definieren.
Zum Beispiel kann es verwendet werden, um:
- Gestalten Sie ein Element, wenn ein Benutzer mit der Maus darüber fährt
- Gestalten Sie besuchte und nicht besuchte Links unterschiedlich
- Gestalten Sie ein Element, wenn es den Fokus erhält
Maus über mich
Syntax
Die Syntax von Pseudoklassen:
selector:pseudo-class {
property: value;
}
Anker-Pseudo-Klassen
Links können auf verschiedene Arten angezeigt werden:
Beispiel
/* unvisited link */
a:link {
color: #FF0000;
}
/* visited
link */
a:visited {
color: #00FF00;
}
/* mouse over link */
a:hover {
color: #FF00FF;
}
/* selected link */
a:active {
color: #0000FF;
}
Hinweis: a:hover
MUSS nach a:link
und
a:visited
in der CSS-Definition stehen, um wirksam zu sein! a:active
MUSS in der CSS-Definition nachkommen
a:hover
, um wirksam zu sein! Bei Pseudo-Klassennamen wird die Groß-/Kleinschreibung nicht beachtet.
Pseudo-Klassen und HTML-Klassen
Pseudo-Klassen können mit HTML-Klassen kombiniert werden:
Wenn Sie den Mauszeiger über den Link im Beispiel bewegen, ändert er die Farbe:
Beispiel
a.highlight:hover {
color: #ff0000;
}
Bewegen Sie den Mauszeiger auf <div>
Ein Beispiel für die Verwendung der :hover
Pseudo-Klasse für ein <div>-Element:
Beispiel
div:hover {
background-color: blue;
}
Einfacher Tooltip-Hover
Bewegen Sie den Mauszeiger über ein <div>-Element, um ein <p>-Element anzuzeigen (wie ein Tooltip):
Tada! Here I am!
Beispiel
p {
display: none;
background-color: yellow;
padding: 20px;
}
div:hover p {
display: block;
}
CSS - Die :first-child Pseudo-Klasse
Die :first-child
Pseudoklasse stimmt mit einem angegebenen Element überein, das das erste untergeordnete Element eines anderen Elements ist.
Übereinstimmung mit dem ersten <p>-Element
Im folgenden Beispiel stimmt der Selektor mit jedem <p>-Element überein, das das erste untergeordnete Element eines beliebigen Elements ist:
Beispiel
p:first-child
{
color: blue;
}
Übereinstimmung mit dem ersten <i>-Element in allen <p>-Elementen
Im folgenden Beispiel stimmt der Selektor mit dem ersten <i>-Element in allen <p>-Elementen überein:
Beispiel
p i:first-child
{
color: blue;
}
Gleichen Sie alle <i>-Elemente in allen ersten untergeordneten <p>-Elementen ab
Im folgenden Beispiel stimmt der Selektor mit allen <i>-Elementen in <p>-Elementen überein, die das erste untergeordnete Element eines anderen Elements sind:
Beispiel
p:first-child i
{
color: blue;
}
CSS - Die :lang Pseudo-Klasse
Die :lang
Pseudo-Klasse ermöglicht es Ihnen, spezielle Regeln für verschiedene Sprachen zu definieren.
Definiert im Beispiel unten :lang
die Anführungszeichen für <q>-Elemente mit lang="no":
Beispiel
<html>
<head>
<style>
q:lang(no) {
quotes: "~" "~";
}
</style>
</head>
<body>
<p>Some text <q lang="no">A quote in a paragraph</q>
Some text.</p>
</body>
</html>
Mehr Beispiele
Dieses Beispiel zeigt, wie Hyperlinks andere Stile hinzugefügt werden.
Dieses Beispiel demonstriert die Verwendung der Pseudoklasse :focus.
Alle CSS-Pseudoklassen
Selector | Example | Example description |
---|---|---|
:active | a:active | Selects the active link |
:checked | input:checked | Selects every checked <input> element |
:disabled | input:disabled | Selects every disabled <input> element |
:empty | p:empty | Selects every <p> element that has no children |
:enabled | input:enabled | Selects every enabled <input> element |
:first-child | p:first-child | Selects every <p> elements that is the first child of its parent |
:first-of-type | p:first-of-type | Selects every <p> element that is the first <p> element of its parent |
:focus | input:focus | Selects the <input> element that has focus |
:hover | a:hover | Selects links on mouse over |
:in-range | input:in-range | Selects <input> elements with a value within a specified range |
:invalid | input:invalid | Selects all <input> elements with an invalid value |
:lang(language) | p:lang(it) | Selects every <p> element with a lang attribute value starting with "it" |
:last-child | p:last-child | Selects every <p> elements that is the last child of its parent |
:last-of-type | p:last-of-type | Selects every <p> element that is the last <p> element of its parent |
:link | a:link | Selects all unvisited links |
:not(selector) | :not(p) | Selects every element that is not a <p> element |
:nth-child(n) | p:nth-child(2) | Selects every <p> element that is the second child of its parent |
:nth-last-child(n) | p:nth-last-child(2) | Selects every <p> element that is the second child of its parent, counting from the last child |
:nth-last-of-type(n) | p:nth-last-of-type(2) | Selects every <p> element that is the second <p> element of its parent, counting from the last child |
:nth-of-type(n) | p:nth-of-type(2) | Selects every <p> element that is the second <p> element of its parent |
:only-of-type | p:only-of-type | Selects every <p> element that is the only <p> element of its parent |
:only-child | p:only-child | Selects every <p> element that is the only child of its parent |
:optional | input:optional | Selects <input> elements with no "required" attribute |
:out-of-range | input:out-of-range | Selects <input> elements with a value outside a specified range |
:read-only | input:read-only | Selects <input> elements with a "readonly" attribute specified |
:read-write | input:read-write | Selects <input> elements with no "readonly" attribute |
:required | input:required | Selects <input> elements with a "required" attribute specified |
:root | root | Selects the document's root element |
:target | #news:target | Selects the current active #news element (clicked on a URL containing that anchor name) |
:valid | input:valid | Selects all <input> elements with a valid value |
:visited | a:visited | Selects all visited links |
Alle CSS-Pseudo-Elemente
Selector | Example | Example description |
---|---|---|
::after | p::after | Insert content after every <p> element |
::before | p::before | Insert content before every <p> element |
::first-letter | p::first-letter | Selects the first letter of every <p> element |
::first-line | p::first-line | Selects the first line of every <p> element |
::selection | p::selection | Selects the portion of an element that is selected by a user |