【问题标题】:How to set CSS variable to the value unset, "--unset-it: unset"?如何将 CSS 变量设置为未设置的值“--unset-it: unset”?
【发布时间】:2020-05-14 12:33:33
【问题描述】:

我想为 CSS 变量赋予未设置的值,这样我就可以将它用于像 outline: unset 这样的东西。 (我知道--unset-it: unset 中的“未设置”指的是变量本身。)

可以吗?我在这里做了一些测试:

const theDump = document.getElementById("dump");
const root = document.documentElement;
let checked = false;

document.getElementsByName("unset-it").forEach((elt) => {
  function displayIt(elt) {
    root.style.setProperty("--unset-it", elt.value);
    const propVal = getComputedStyle(root).getPropertyValue("--unset-it");
    theDump.textContent = `--unset-it: ${propVal};`;
  }
  if (!checked) {
    checked = true;
    elt.checked = true;
    elt.focus();
    displayIt(elt);
  }
  elt.addEventListener("click", evt => {
    const elt = evt.target;
    displayIt(elt);
  });
});
* {
  box-sizing: border-box;
}

#ex {
  background: yellow;
  padding: 0.5rem;
  margin: 1rem 0;
}

input[type=radio] {
  position: relative;
  width: 15rem;
  margin: 0;
  margin-left: 1rem;
  margin-bottom: 0.5rem;
}

input[type=radio]::after {
  content: attr(value);
  position: absolute;
  left: 0;
  top: 0;
}

#div1 {
  outline: var(--unset-it, 2px dotted red);
}
<input type="radio" name="unset-it" value='"unset"'>
<input type="radio" name="unset-it" value="'unset'">
<input type="radio" name="unset-it" value='unset'>
<input type="radio" name="unset-it" value='whatever'>
<input type="radio" name="unset-it" value='"whatever"'>
<input type="radio" name="unset-it" value='5px solid green'>
<input type="radio" name="unset-it" value='"5px solid green"'>
<input type="radio" name="unset-it" value="initial">

<div id="ex">
  <p id="div1">
    outline: var(--unset-it, 2px dotted red);
  </p>
  <p id="dump">Dump</p>
</div>

【问题讨论】:

    标签: css css-variables


    【解决方案1】:

    就像我在this previous answer with inherit 中所做的那样,您需要将unset 设置为备用值并考虑initial 来激活它:

    * {
      box-sizing: border-box;
    }
    
    #ex {
      background: yellow;
      padding: 0.5rem;
      margin: 1rem 0;
    }
    
    
    #div1 {
      --unset-it: 2px dotted red;
      outline: var(--unset-it, unset);
    }
    <div id="ex">
      <p id="div1">
        outline: 2px dotted red;
      </p>
      <p id="div1" style="--unset-it:initial">
        outline: unset;
      </p>
    </div>

    该技巧适用于任何全局值,例如 unsetinheritinitialrevert


    另一个想法是考虑计算时间的无效值。从the specification我们可以读到:

    如果声明包含引用具有初始值的自定义属性的var(),则声明在计算值时可能无效,如上所述,或者如果它使用有效的自定义属性,但是属性值,在替换了它的var() 函数后,无效。发生这种情况时,属性的计算值要么是属性的继承值,要么是其初始值,具体取决于属性是否被继承,就好像属性的值已被指定为未设置的关键字.

    * {
      box-sizing: border-box;
    }
    
    #ex {
      background: yellow;
      padding: 0.5rem;
      margin: 1rem 0;
    }
    
    
    #div1 {
      outline: var(--unset-it, 2px dotted red);
    }
    <div id="ex">
      <p id="div1">
        outline: 2px dotted red;
      </p>
      <p id="div1" style="--unset-it:'random-value-here'">
        outline: unset;
      </p>
    </div>

    你在使用时应该注意,因为它与你将使用 CSS 变量的属性密切相关,因为值的有效性取决于属性。

    对某些属性有效而对其他属性无效的值示例:

    * {
      box-sizing: border-box;
    }
    
    #ex {
      background: yellow;
      padding: 0.5rem;
      margin: 1rem 0;
    }
    
    
    #div1 {
      outline: var(--unset-it, 2px dotted red); /*invalid here*/
    }
    #div1::before {
      content:var(--unset-it,"content"); /* valid here */
    }
    <div id="ex">
      <p id="div1" style="--unset-it:'random-value-here';">
        outline: unset;
      </p>
    </div>

    【讨论】:

    • @Leo 本地值是多少?
    • @Leo 你说的本地是什么意思?什么是您的本地价值
    • @Leo 你不能这样做,你像 C 和 C++ 一样思考。你需要像我一样重做你的逻辑。需要取消设置回退值(这是默认值),然后将变量设置为调试时要使用的大纲。你正在做的事情是行不通的
    • @Leo unset 不能存储在 CSS 变量中,因为它是 CSS 变量的值(阅读我分享的链接以了解这一点)所以如果您尝试这样做,那么您成功的运气为 0 (即使是 JS)。像我描述的唯一解决方案是将 unset 视为后备值并使用所需的值切换 CSS 变量。如果定义了 CSS 变量,那么你有大纲,如果没有,那么你有你的 unset。
    • @Leo 这不是规范错误。 unset 被定义为 initial 或根据属性继承,CSS 变量是继承的属性,因此 unset 将回退到继承,我们将从父级继承值,如果未定义,则它将什么都没有(初始),这是规范中也明确定义
    猜你喜欢
    • 2012-12-22
    • 2019-05-11
    • 2018-11-12
    • 2020-03-19
    • 1970-01-01
    • 1970-01-01
    • 2020-06-26
    相关资源
    最近更新 更多