inherit
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
inherit
关键字使得元素获取其父元素的计算值。它可以应用于任何 CSS 属性,包括 CSS 简写 all
。
对于继承属性,该关键字只是增强了属性的默认行为,通常只在覆盖原有的值的时候使用。
备注: 继承始终来自文档树中的父元素,即使父元素不是包含区块。
示例
从规则中排除所选元素
css
/* 设置二级标题的颜色为绿色 */
h2 {
color: green;
}
/* 保留 sidebar 内二级标题元素的样式,使其继承父元素的颜色 */
#sidebar h2 {
color: inherit;
}
在这个例子中,sidebar 中的 h2
元素可能有不同的颜色。如果某个 h2
元素的 div
匹配下面的规则:
css
div#current {
color: blue;
}
那它的颜色会变成蓝色。
规范
Specification |
---|
CSS Cascading and Inheritance Level 4 # inherit |