CKTIS / CSS
Ein Optionsfeld mit dem veralteten `clip: rect(0 0 0 0)` visuell ausblenden
Diese ältere CSS-Technik blendet das native Optionsfeld optisch aus, ohne seine Semantik oder Tastaturbedienung zu ändern.
Version: Current CSS specifications; browser support varies by feature
Syntax
clip: rect(0 0 0 0); /* historical, space-separated form; support is optional */
clip: rect(0, 0, 0, 0); /* specified, comma-separated form */
/* For HTML, clip applies with position: absolute or position: fixed. */
clip-path: inset(50%); /* modern way to create an empty clipping region */In älterem CSS blendet clip: rect(0 0 0 0) das native Optionsfeld aus, indem es den sichtbaren Zeichenbereich beschneidet. Bei HTML-Elementen wirkt clip nur bei absoluter Positionierung (position: absolute oder position: fixed). Die Argumente des alten rect() geben die obere, rechte, untere und linke Kante an. Vertikale Koordinaten werden von der oberen, horizontale von der linken Boxkante aus gemessen. Vier Nullen ergeben ein Rechteck ohne Breite und Höhe; die Box selbst ändert ihre Größe nicht.
Die Spezifikation verlangt Unterstützung für rect(0, 0, 0, 0). Die historische Schreibweise nur mit Leerzeichen ist optional; mische die Trennzeichen nicht. clip ist veraltet. Verwende in neuem Code clip-path: inset(50%): Jede Beschneidungskante rückt bis zur Mitte, sodass der Bereich leer ist. Das alte rect() ist nicht die gleichnamige Grundformfunktion für clip-path.
Das Beschneiden deaktiviert das Feld nicht. Behalte native input[type="radio"]-Elemente, einen gemeinsamen name in der Gruppe und sichtbare label-Elemente bei, die über for und id zugeordnet sind. Das Label ist die sichtbare Klickfläche. Zeige dort die Auswahl über :checked und den Tastaturfokus über :focus-visible an. Verwende dafür weder display: none noch aria-hidden="true" am Feld. Prüfe Tastaturbedienung und assistive Technologien in den Zielbrowsern.
Beispiele
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Radio clipping demo</title>
<style>
.choice {
position: relative;
margin-block: 0.5rem;
}
.choice > input[type="radio"] {
position: absolute;
top: 50%;
left: 0;
margin: 0;
transform: translateY(-50%);
clip: rect(0, 0, 0, 0);
clip: rect(0 0 0 0);
}
.choice > label {
display: inline-block;
padding: 0.25rem 0.5rem 0.25rem 1.75rem;
cursor: pointer;
}
.choice > input:checked + label {
font-weight: 700;
text-decoration: underline;
}
.choice > input:focus-visible + label {
outline: 2px solid currentColor;
outline-offset: 2px;
}
</style>
</head>
<body>
<fieldset>
<legend>Contact method</legend>
<div class="choice">
<input type="radio" id="email" name="contact" value="email" checked>
<label for="email">Email</label>
</div>
<div class="choice">
<input type="radio" id="phone" name="contact" value="phone">
<label for="phone">Phone</label>
</div>
</fieldset>
</body>
</html>Ergebnis
Expected on a fresh load in a browser supporting the comma-separated form of `clip`: both labels are visible, neither native radio circle is visible, and Email is bold and underlined. In a browser supporting `:focus-visible`, with browser and OS keyboard-navigation settings that let Tab reach native radio buttons, pressing Tab to enter the group focuses Email and outlines its label. With Email focused, pressing Down Arrow moves selection and keyboard focus to Phone; Phone becomes bold and underlined and receives the outline. Clicking Email selects it again.
Verwirft der Browser die Schreibweise mit Leerzeichen, greift die Deklaration mit Kommas. Anfangs ist Email ausgewählt. Wenn die Einstellungen von Browser und Betriebssystem native Optionsfelder per Tab erreichbar machen, setzt Tab beim Eintritt in die Gruppe den Fokus auf Email. Unterstützt der Browser `:focus-visible`, erscheint eine Kontur um das Label. Sobald Email den Fokus hat, verschiebt die Pfeiltaste nach unten Fokus und Auswahl auf Phone.
Zugehörige Lektionen
- CSS · Lektion 18 · Fortgeschritten · Version: 2