inverted-colors

可用性有限

此特性不是基线特性,因为它在一些最广泛使用的浏览器中不起作用。

inverted-colors CSS 媒体特性 用于测试用户代理或底层操作系统是否已反转所有颜色。

颜色反转可能会产生不愉快副作用,例如阴影变成高光,这会降低内容的可读性。使用此媒体特性,你可以检测是否正在发生反转,并相应地设置内容样式,同时尊重用户偏好。

语法

css
/* Keyword value */
@media (inverted-colors: inverted) {
  /* styles to apply if inversion of colors is detected */
}

inverted-colors 特性以下列关键词值之一指定

none

表示颜色正常显示,未发生颜色反转。此关键词值评估为 false。

inverted

表示显示区域内的所有像素都已反转。此关键词值评估为 true。

示例

如果检测到颜色反转,则应用样式

此示例演示了 inverted-colors 媒体特性关键词值以及当 inverted-colors 媒体特性不受支持时的效果。

HTML

html
<p>
  If color inversion is detected, this text will appear blue on white (the
  inverse of yellow on black) along with a line over the text. If no color
  inversion is happening, the text will appear red on light gray without the
  line over the text.
</p>
<p>
  If the text is gray and no overline is present, it means your browser doesn't
  support the
  <code>inverted-colors</code> media feature.
</p>

CSS

css
p {
  color: gray;
}

@media (inverted-colors: inverted) {
  p {
    background: black;
    color: yellow;
    text-decoration: overline;
  }
}

@media (inverted-colors: none) {
  p {
    background: #eeeeee;
    color: red;
  }
}

结果

规范

规范
媒体查询 Level 5
# inverted

浏览器兼容性

另见