CKTIS / CSS
stop-color — kolor punktu gradientu SVG
Właściwość CSS `stop-color` określa kolor elementu `stop` w gradiencie SVG. Nie ustala jego położenia ani bezpośrednio koloru całej figury.
Wersja: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required
Składnia
stop-color: <color>;stop-color określa kolor punktu gradientu reprezentowanego przez element stop wewnątrz linearGradient lub radialGradient. Położenie punktu ustala osobny atrybut offset; figura korzysta z gradientu przez fill lub stroke.
Wartością jest <color>, na przykład red, #ff0000 lub currentColor. Wartość początkowa to black. Właściwość nie jest dziedziczona i może być animowana. Deklaracja CSS może nadpisać atrybut prezentacyjny stop-color na tym samym elemencie.
currentColor odwołuje się do wartości color elementu stop. Wartość alfa koloru (jego krycie) jest mnożona przez wartość stop-opacity. Uwaga: transparent oznacza przezroczystą czerń; aby wygasić kolor bez przejścia ku czerni, zachowaj ten kolor i ustaw końcowe stop-opacity: 0.
W przykładzie reguły CSS ustawiają czerwony początek i niebieski koniec gradientu. Oczekiwany rezultat to prostokąt 240 × 80 z poziomym przejściem od czerwieni po lewej do niebieskiego po prawej.
Przykłady
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Set SVG gradient colors with stop-color</title>
<style>
#start { stop-color: #ff0000; }
#end { stop-color: #0000ff; }
</style>
</head>
<body>
<figure>
<svg width="240" height="80" viewBox="0 0 240 80" role="img" aria-labelledby="gradient-title">
<title id="gradient-title">Rectangle with a horizontal gradient</title>
<defs>
<linearGradient id="gradient" x1="0%" y1="0%" x2="100%" y2="0%">
<stop id="start" offset="0%" />
<stop id="end" offset="100%" />
</linearGradient>
</defs>
<rect width="240" height="80" fill="url(#gradient)" />
</svg>
<figcaption id="result"></figcaption>
</figure>
<script>
const colorNames = { 'rgb(255, 0, 0)': 'red', 'rgb(0, 0, 255)': 'blue' };
function stopColor(id) {
const color = getComputedStyle(document.getElementById(id)).getPropertyValue('stop-color');
return colorNames[color] ?? color;
}
const result = `Rectangle gradient: ${stopColor('start')} on the left to ${stopColor('end')} on the right.`;
document.getElementById('result').textContent = result;
console.log(result);
</script>
</body>
</html>Wynik
Rectangle gradient: red on the left to blue on the right.
Wyświetlony kod i zadeklarowany wynik zweryfikowano w odizolowanym środowisku metodą zapisaną w dowodzie weryfikacji tej dokładnej wersji.