gpt4 book ai didi

html - CSS —::选择与 var() 回退

转载 作者:太空宇宙 更新时间:2023-11-04 14:49:40 30 4
gpt4 key购买 nike

我正在尝试使用 css 变量更改整个网页的选择颜色:

html {
--my-var: red;
}

::selection {
background-color: var(--my-var);
}
<p>a b c</p>
<div>
<p>x y z</p>
</div>

我正确地看到应用了选择样式。但是,如果 var 引用了一个 undefined variable ,则根本没有选择颜色:

html {
--my-var: red;
}

::selection {
background-color: var(--not-my-var);
}

如何为使用 css 变量值(如果存在)或回退到默认浏览器选择颜色的整个页面定义选择颜色?我试过 var(--not-my-var, unset)var(--not-my-var, inherit)var(- -not-my-var, initial) 但它们似乎不起作用

最佳答案

获得所需内容的唯一方法是确保该值无效,以便浏览器将忽略它并回退到默认值:

::selection {
background-color: something_invalid;
}
<p>a b c</p>
<div>
<p>x y z</p>
</div>

不幸的是,在使用 var(--variable) 时这是不可能的,因为该值永远不会无效。

来自 the specificatition我们有以下步骤来查找值:

To substitute a var() in a property’s value:

  1. If the custom property named by the first argument to the var() function is animation-tainted, and the var() function is being used in the animation property or one of its longhands, treat the custom property as having its initial value for the rest of this algorithm.
  2. If the value of the custom property named by the first argument to the var() function is anything but the initial value, replace the var() function by the value of the corresponding custom property.
  3. Otherwise, if the var() function has a fallback value as its second argument, replace the var() function by the fallback value. If there are any var() references in the fallback, substitute them as well.
  4. Otherwise, the property containing the var() function is invalid at computed-value time.

(2) 和 (3) 是微不足道的,总会给我们一个值。 (1) 告诉我们将自定义属性的值视为 initial,我们将陷入 (3) 或 (4)。

对于(4)我们也可以从同样的规范中读到:

A declaration can be invalid at computed-value time if it contains a var() that references a custom property with its initial value, as explained above, or if it uses a valid custom property, but the property value, after substituting its var() functions, is invalid. When this happens, the computed value of the property is either the property’s inherited value or its initial value depending on whether the property is inherited or not, respectively, as if the property’s value had been specified as the unset keyword.

background-color 不是继承的,所以它将使用它的初始值,即 transparent 这就是你看不到颜色的原因。


现在,如果我们考虑回退情况 (3)

  • 使用initial表示背景颜色初始值如此透明
  • 使用inherit表示继承颜色但是又没有什么可以继承那么透明了
  • 使用 unset 我们将混合使用 inheritinitial

The unset CSS keyword resets a property to its inherited value if it inherits from its parent, and to its initial value if not. In other words, it behaves like the inherit keyword in the first case, and like the initial keyword in the second case ref


您唯一的机会可能是使用 revert但支持率很低,我不确定它是否有效。

The revert CSS keyword reverts the cascaded value of the property from its current value to the value the property would have had if no changes had been made by the current style origin to the current element. Thus, it resets the property to its inherited value if it inherits from its parent or to the default value established by the user agent's stylesheet (or by user styles, if any exist). ref

更新

根据下面的评论,revert 似乎也是不可能的。

关于html - CSS —::选择与 var() 回退,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56798646/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com