stop-color

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since April 2017.

Thuộc tính stop-color của CSS xác định màu sắc để sử dụng cho phần tử SVG <stop> bên trong một gradient. Nếu có, nó sẽ ghi đè thuộc tính stop-color của phần tử.

Note: Thuộc tính stop-color chỉ áp dụng cho các phần tử <stop> được lồng trong một <svg>. Nó không áp dụng cho các SVG, HTML hay phần tử giả khác.

Cú pháp

css
/* Giá trị <color> */
stop-color: red;
stop-color: hsl(120deg 75% 25% / 60%);
stop-color: currentColor;

/* Giá trị toàn cục */
stop-color: inherit;
stop-color: initial;
stop-color: revert;
stop-color: revert-layer;
stop-color: unset;

Giá trị

<color>

Màu của phần tô. Có thể là bất kỳ giá trị CSS <color> hợp lệ nào.

Định nghĩa chính thức

Initial valueblack
Applies to<stop> elements in <svg>
Inheritedno
Computed valueas specified
Animation typediscrete

Cú pháp chính thức

stop-color = 
<color>

Ví dụ

Xác định điểm dừng màu của gradient SVG

Ví dụ này trình bày trường hợp sử dụng cơ bản của stop-color, và cách thuộc tính CSS stop-color được ưu tiên hơn thuộc tính stop-color.

HTML

Chúng ta có một SVG với ba hình vuông <rect> và ba phần tử <linearGradient>. Mỗi gradient có bốn phần tử <stop> tạo ra các gradient từ đen sang trắng rồi từ trắng sang xám; sự khác biệt duy nhất giữa chúng là giá trị id.

html
<svg viewBox="0 0 264 100" xmlns="http://www.w3.org/2000/svg">
  <defs>
    <linearGradient id="myGradient1">
      <stop offset="25%" stop-color="black" />
      <stop offset="40%" stop-color="white" />
      <stop offset="60%" stop-color="white" />
      <stop offset="75%" stop-color="#333333" />
    </linearGradient>
    <linearGradient id="myGradient2">
      <stop offset="25%" stop-color="black" />
      <stop offset="40%" stop-color="white" />
      <stop offset="60%" stop-color="white" />
      <stop offset="75%" stop-color="#333333" />
    </linearGradient>
    <linearGradient id="myGradient3">
      <stop offset="25%" stop-color="black" />
      <stop offset="40%" stop-color="white" />
      <stop offset="60%" stop-color="white" />
      <stop offset="75%" stop-color="#333333" />
    </linearGradient>
  </defs>
  <rect x="2" y="10" width="80" height="80" fill="url('#myGradient1')" />
  <rect x="92" y="10" width="80" height="80" fill="url('#myGradient2')" />
  <rect x="182" y="10" width="80" height="80" fill="url('#myGradient3')" />
</svg>

CSS

Chúng ta bao gồm strokestroke-width để tạo đường viền cho hình chữ nhật. Chúng ta xác định màu của điểm dừng đầu tiên và cuối cùng trong mỗi gradient, ghi đè giá trị thuộc tính stop-color của chúng, bằng cách sử dụng thuộc tính stop-color. Các cú pháp CSS <color> khác nhau được hiển thị.

css
rect {
  stroke: #333333;
  stroke-width: 1px;
}

#myGradient1 {
  stop:first-of-type {
    stop-color: #66ccff;
  }
  stop:last-of-type {
    stop-color: #f4aab9;
  }
}
#myGradient2 {
  stop:first-of-type {
    stop-color: yellow;
  }
  stop:last-of-type {
    stop-color: purple;
  }
}
#myGradient3 {
  stop:first-of-type {
    stop-color: hsl(0deg 90% 50%);
  }
  stop:last-of-type {
    stop-color: hsl(20deg 60% 50%);
  }
}

Kết quả

Thông số kỹ thuật

Specification
Scalable Vector Graphics (SVG) 2
# StopColorProperty

Khả năng tương thích trình duyệt

Xem thêm