继承(Inheritance)
继承是 CSS 中某些属性从父元素传递到子元素的机制。
什么是继承?
继承(Inheritance)是指某些 CSS 属性会从父元素自动传递给子元素。这使得我们可以设置父元素的样式,然后所有子元素都会继承这些样式。
可继承的属性
以下属性通常会被继承:
文本相关属性
color- 文本颜色font-family- 字体族font-size- 字体大小font-weight- 字体粗细font-style- 字体样式line-height- 行高text-align- 文本对齐text-decoration- 文本装饰text-indent- 首行缩进text-transform- 文本转换
列表相关属性
list-style- 列表样式list-style-type- 列表项标记类型list-style-image- 列表项标记图像list-style-position- 列表项标记位置
间距相关属性
letter-spacing- 字符间距word-spacing- 单词间距
其他属性
visibility- 可见性cursor- 光标样式white-space- 空白处理
不可继承的属性
以下属性通常不会被继承:
盒模型属性
margin- 外边距padding- 内边距border- 边框width- 宽度height- 高度
背景属性
background- 背景background-color- 背景颜色background-image- 背景图像
布局属性
display- 显示方式position- 定位float- 浮动clear- 清除浮动
继承示例
css
/* 父元素设置字体 */
body {
font-family: Arial, sans-serif;
color: #333;
}
/* 所有子元素都会继承这些样式 */
p {
/* 继承 font-family: Arial */
/* 继承 color: #333 */
}
div {
/* 继承 font-family: Arial */
/* 继承 color: #333 */
}覆盖继承
子元素可以覆盖继承的样式:
css
body {
color: #333;
}
.highlight {
color: red; /* 覆盖继承的颜色 */
}inherit 关键字
使用 inherit 关键字可以强制继承:
css
p {
margin: inherit; /* 强制继承 margin */
}