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