CSS :defined 偽類

定義和用法

CSS :defined 偽類用于匹配任何已定義的元素。

此偽類可用于標準元素和已成功定義的自定義元素。

實例

使用 :defined 偽類:

custom-element:not(:defined) {
  border-color: grey;
  color: grey;
}
custom-element:defined {
  background-color: salmon;
  border-color: maroon;
  color: black;
}
/* 顯示加載消息 */
custom-element:not(:defined)::before {
  content: "Loading...";
  position: absolute;
  inset: 0 0 0 0;
  align-content: center;
  text-align: center;
  font-size: 25px;
  background-color: white;
}
/* 移除加載消息 */
custom-element:defined::before {
  content: "";
}

親自試一試

CSS 語法

:defined {
  css declarations;
}

技術細節

版本: CSS4

瀏覽器支持

表格中的數字指定了完全支持該偽類的首個瀏覽器版本。

Chrome Edge Firefox Safari Opera
54 79 63 10 41