:active

Baseline 已廣泛支援

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

:active CSS 偽類表示正在被使用者啟用的元素(例如按鈕)。當使用滑鼠時,“啟用”通常在使用者按下主滑鼠按鈕時開始。

試一試

.joinBtn {
  width: 10em;
  height: 5ex;
  background-image: linear-gradient(135deg, #f34079 40%, #fc894d);
  border: none;
  border-radius: 5px;
  font-weight: bold;
  color: white;
  cursor: pointer;
}

.joinBtn:active {
  box-shadow: 2px 2px 5px #fc894d;
}
<p>Would you like to subscribe to our channel?</p>
<button class="joinBtn">Subscribe</button>

:active 偽類通常用於 <a><button> 元素。此偽類的其他常見目標包括包含在已啟用元素中的元素,以及透過其關聯的 <label> 啟用的表單元素。

:active 偽類定義的樣式將被任何後續的具有至少相等特異性的連結相關偽類(:link:hover:visited)覆蓋。為了適當地樣式連結,請將 :active 規則放在所有其他連結相關規則之後,如 LVHA-order 所定義::link:visited:hover:active

注意:在帶有多個按鈕的滑鼠系統上,CSS 規定 :active 偽類必須僅適用於主按鈕;在慣用右手滑鼠上,這通常是最左邊的按鈕。

語法

css
:active {
  /* ... */
}

示例

HTML

html

CSS

css

結果

啟用的表單元素

HTML

html
<form>
  <label for="my-button">My button: </label>
  <button id="my-button" type="button">Try Clicking Me or My Label!</button>
</form>

CSS

css
form :active {
  color: red;
}

form button {
  background: white;
}

結果

規範

規範
HTML
# 選擇器-active
選擇器 Level 4
# active-偽類

瀏覽器相容性

另見