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
。
对于继承属性,inherit 关键字只是增强了属性的默认行为,通常只在覆盖原有的值的时候使用。
继承始终来自文档树中的父元素,即使父元素不是包含块。
示例
css
/* 设置二级标题的颜色为绿色 */
h2 {
color: green;
}
/* ...but leave those in the sidebar alone so they use their parent's color */
#sidebar h2 {
color: inherit;
}
在下面这个例子中,如果 sidebar 中h2
元素的 div 匹配下面的规则的话,颜色会变成蓝色。
css
div#current {
color: blue;
}
规范
Specification |
---|
CSS Cascading and Inheritance Level 4 # inherit |
浏览器兼容性
BCD tables only load in the browser
See also
- Inheritance
- Use
initial
to set a property to its initial value. - Use
unset
to set a property to its inherited value if it inherits, or to its initial value if not. - Use
revert
to reset a property to the value established by the user-agent stylesheet (or by user styles, if any exist). - The
all
property lets you reset all properties to their initial, inherited, reverted, or unset state at once.