outline-color

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

outline-color CSS 屬性設定元素輪廓的顏色。

試一試

outline-color: red;
outline-color: #32a1ce;
outline-color: rgb(170 50 220 / 0.6);
outline-color: hsl(60 90% 50% / 0.8);
outline-color: currentColor;
<section class="default-example" id="default-example">
  <div class="transition-all" id="example-element">
    This is a box with an outline around it.
  </div>
</section>
#example-element {
  outline: 0.75em solid;
  padding: 0.75em;
  width: 80%;
  height: 100px;
}

語法

css
/* <color> values */
outline-color: #f92525;
outline-color: rgb(30 222 121);
outline-color: blue;

/* Global values */
outline-color: inherit;
outline-color: initial;
outline-color: revert;
outline-color: revert-layer;
outline-color: unset;

outline-color 屬性指定為以下值中的任意一個。

<color>

輪廓的顏色,指定為 <color>

規範還列出了一個附加值 auto,但目前沒有任何瀏覽器支援。當實現時,除非 outline-style 設定為 auto,否則 auto 將計算為 currentColor,在這種情況下,它將計算為 強調色

描述

輪廓是圍繞元素繪製的一條線,位於 border 之外。與元素的邊框不同,輪廓繪製在元素的框架之外,並且可能與其他內容重疊。另一方面,邊框實際上會改變頁面的佈局,以確保它適合而不會與任何其他內容重疊(除非你明確將其設定為重疊)。

在定義輪廓外觀時,通常使用簡寫屬性 outline 更方便。

無障礙

自定義 焦點樣式 通常涉及調整 outline 屬性。如果輪廓的顏色被調整,重要的是確保它與輪廓所放置的背景之間的對比度足夠高,以便有低視力障礙的人能夠感知它。

顏色對比度是透過比較文字和背景顏色值的亮度來確定的。為了滿足當前的 Web 內容可訪問性指南 (WCAG),文字內容需要 4.5:1 的比率,而大文字(如標題)需要 3:1 的比率。大文字定義為 18.66 畫素及粗體或更大,或 24 畫素或更大。

正式定義

初始值auto
應用於所有元素
繼承性
計算值對於關鍵字 auto,計算值為 currentcolor。對於顏色值,如果該值是半透明的,則計算值將是相應的 rgba() 值。如果不是,則將是相應的 rgb() 值。transparent 關鍵字對映到 rgba(0,0,0,0)
動畫型別一種顏色

正式語法

outline-color = 
auto |
<color> |
<image-1D>

<image-1D> =
<stripes()>

<stripes()> =
stripes( <color-stripe># )

<color-stripe> =
<color> &&
[ <length-percentage> | <flex> ]?

<length-percentage> =
<length> |
<percentage>

示例

設定實心藍色輪廓

HTML

html
<p>My outline is blue, as you can see.</p>

CSS

css
p {
  outline: 2px solid; /* Set the outline width and style */
  outline-color: blue; /* Set the outline color */
  margin: 5px;
}

結果

規範

規範
CSS Basic User Interface Module Level 4
# outline-color

瀏覽器相容性

另見