【问题标题】:How do I set a value to its default user agent value?如何将值设置为其默认用户代理值?
【发布时间】:2019-09-12 07:17:35
【问题描述】:

通过使用 initial 值,我可以将元素的 CSS 属性设置为其规范定义的初始值。

但在某些情况下,我想将 CSS 属性设置为其初始用户代理值。

例如,select 元素的appearance 属性因您使用的操作系统而异。它的默认值是none,但大多数时候它会被用户代理样式表设置为menulist,尤其是在桌面浏览器上。

所以,如果我想将 select 元素设置回其默认用户代理值,我该怎么做?理想情况下,我可以删除所有覆盖它的规则,但这并不总是一个实用的选择(例如,在 WordPress 主题中,或在使用第三方库时)。

这是一个视觉示例:

https://jsfiddle.net/xqy6zsfk/

默认选择元素

<select id="select1">
<option>appearance: initial;</option>
<select>

<select id="select2">
<option>appearance: unset;</option>
<select>

<select id="select3">
<option>appearance: revert;</option>
<select>

<select id="select4">
<option>apperance: inherit;</option>
<select>

 

select
{
  display: block;
  margin-bottom: 20px;
  padding: 10px;
}

#select1
{
    -webkit-appearance: initial;
    -moz-appearance: initial;
    appearance: initial;
}

#select2
{
    -webkit-appearance: unset;
    -moz-appearance: unset;
    appearance: unset;
}

#select3
{
    -webkit-appearance: revert;
    -moz-appearance: revert;
    appearance: revert;
}

#select4
{
    -webkit-appearance: inherit;
    -moz-appearance: inherit;
    appearance: inherit;
}

【问题讨论】:

    标签: css css-selectors user-agent


    【解决方案1】:

    将属性设置回用户代理样式表中定义的方法是使用revert 值。在MDN revert 上是这样描述的:

    如果它从其父级继承,它会将属性重置为其继承的值,或者重置为用户代理样式表建立的默认值。

    不幸的是,对revert 的支持尚未普及。根据caniuse,截至 2020 年 1 月,这些是唯一支持它的浏览器:

    【讨论】:

    • 看起来他们正计划将其添加到 Chrome 84。这是向前迈出的一大步!
    猜你喜欢
    • 1970-01-01
    • 2015-08-25
    • 1970-01-01
    • 2016-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多