CKTIS / CSS
stroke-dashoffset — spostare un motivo tratteggiato
La proprietà stroke-dashoffset determina in quale punto di un motivo ripetuto di tratti e spazi inizia un contorno SVG. Modifica la fase del motivo, non la posizione o la lunghezza del tracciato.
Versione: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate; usage example verification passed
Sintassi
stroke-dashoffset: <length-percentage>;
/* SVG 2 also permits <number>. */
stroke-dashoffset: 0;
stroke-dashoffset: 10px;
stroke-dashoffset: -10px;
stroke-dashoffset: 5%;stroke-dashoffset specifica a quale distanza all’interno del motivo ripetuto definito da stroke-dasharray inizia la tratteggiatura del percorso. Il valore iniziale è 0; la proprietà è ereditata e si applica ai contorni delle forme SVG e al testo. Senza un motivo tratteggiato, modificare l’offset non produce effetti visibili. Non equivale a transform né alle proprietà border.
Sono ammessi valori positivi e negativi. Un offset positivo salta l’inizio del motivo; uno negativo ne sposta la fase nella direzione opposta. CSS Fill and Stroke specifica la sintassi <length-percentage>, mentre SVG 2 ammette anche <number> in unità utente. Le percentuali si riferiscono alla diagonale normalizzata dell’area di visualizzazione SVG, non alla lunghezza del percorso: sqrt((width² + height²) / 2). In SVG 2, l’attributo pathLength influisce sull’interpretazione delle distanze dei tratti.
Nell’esempio, stroke-dasharray: 20px 10px crea tratti lunghi 20 unità e spazi lunghi 10. Il contorno inferiore ha stroke-dashoffset: 10px, quindi il suo primo tratto visibile è lungo 10 unità. La geometria resta invariata.
Esempi
<!doctype html>
<meta charset="utf-8">
<title>stroke-dashoffset</title>
<style>
path {
fill: none;
stroke: black;
stroke-width: 4px;
stroke-linecap: butt;
stroke-dasharray: 20px 10px;
}
.shifted { stroke-dashoffset: 10px; }
</style>
<svg xmlns="http://www.w3.org/2000/svg" width="160" height="70" viewBox="0 0 160 70">
<path d="M 20 20 H 140" />
<path class="shifted" d="M 20 50 H 140" />
</svg>Risultato
Two horizontal black dashed strokes. The upper stroke begins with a 20-unit dash; the lower stroke begins with a 10-unit dash, followed by a 10-unit gap. Neither path moves.
Il codice mostrato e il risultato dichiarato sono stati verificati in un ambiente isolato con il metodo registrato nella prova di verifica di questa versione esatta.