CSS :active-Pseudoklasse
Die :active-Pseudoklasse wird verwendet, um den aktiven Link oder ein beliebiges anderes Element auszuwählen und zu stylen. Sie wird vom Benutzer aktiviert.
Ein Element wird aktiv, wenn der Benutzer auf den Link oder das Element klickt und die Maustaste gedrückt hält.
Die :active-Pseudoklasse wird auf <a>- und <button>-Elemente angewendet. Diese Pseudoklasse zielt auch auf Elemente ab, die ein aktiviertes Element enthalten, sowie auf Formular-Elemente, die über das <label>-Element aktiviert werden.
Die :link, :hover oder :visited Pseudoklassen überschreiben die Definition, die von der :active-Pseudoklasse festgelegt wird. Um Links korrekt zu stylen, muss die :active-Regel nach allen anderen linkbezogenen Regeln platziert werden (:link, :visited, :hover, :active).
Hinweis
Verwechseln Sie
:activenicht mit:focus. Während:activegilt, solange ein Element aktiviert wird (z. B. während eines Mausklicks), gilt:focus, wenn ein Element den Tastatur- oder programmatischen Fokus erhält.
Info
Auf Systemen mit Mäusen mit mehreren Tasten gibt CSS3 an, dass der
:active-Zustand durch die primäre Eingabetaste (typischerweise die linke Maustaste) ausgelöst wird.
Version
Syntax
CSS :active-Pseudoklasse Beispiel
:active {
css declarations;
}Beispiel für die :active-Pseudoklasse:
CSS :active-Pseudoklasse Code-Beispiel
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
a:active {
background-color: #8ebf42;
color: #666;
}
</style>
</head>
<body>
<h2>:active selector example</h2>
<a href="https://www.w3docs.com/">w3docs.com</a>
</body>
</html>Beispiel für die :active-Pseudoklasse mit dem <a>-Tag:
CSS :active-Pseudoklasse, Code-Beispiel 2
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
a {
color: #1c87c9;
}
a:active {
background-color: #8ebf42;
color: #eee;
}
</style>
</head>
<body>
<h2>:active selector example</h2>
<p>Lorem Ipsum is simply dummy text of the printing and <a href="#">typesetting</a> industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into <a href="#">electronic</a> typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like <a href="#">Aldus PageMaker</a> including versions of Lorem Ipsum.</p>
</body>
</html>Klicken Sie im folgenden Beispiel auf den Text, um zu sehen, wie sich die Farbe ändert.
Beispiel für die :active-Pseudoklasse mit dem <div>-Tag:
CSS :active-Pseudoklasse, weiteres Code-Beispiel
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
div {
padding: 30px;
background-color: #8ebf42;
color: #eee;
}
div:active {
background-color: #666;
color: #fff;
}
</style>
</head>
<body>
<h2>:active selector example</h2>
<div>
Lorem ipsum is simply dummy text...
</div>
</body>
</html>Praxis
Was ist der Zweck des :active-Selektors in CSS und wo kann er verwendet werden?