gpt4 book ai didi

html - CSS继承其他?

转载 作者:行者123 更新时间:2023-11-27 23:00:23 24 4
gpt4 key购买 nike

我想知道我是否可以设置一个子元素

color: inherit;

但如果父元素中没有指定颜色,则使用不同的颜色。

所以我想做你可以用字体系列做的同样的事情。即。

font-family: "arial", "sans-serif";

所以如果 arial 不可用,它将使用 sans-serif。

我试过了

color: inherit, #646323;

但没有成功。

这可能吗?

编辑 1:解释更多:

我可以通过这样做来缩短代码,因为我的页面上有很多标签。其中大部分我想要自定义颜色(而不是默认颜色(例如悬停、事件、访问等)。但有些链接,我想要不同的样式。

以这种方式而不是使用类会更好。因为我正在编辑一个包含许多链接的页面。

最佳答案

一个想法是为此考虑 CSS 变量:

p {
color: var(--c); /*initial is the fallback if --c is not defined */
}

span {
color: var(--c, red); /*red is the fallback if --c is not defined*/
}
<p>
color <span>text</span>
</p>
<p style="--c:blue">
color <span>text</span>
</p>

关于html - CSS继承其他?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52801247/

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