CSS可继承属性
在 CSS 中,可继承属性是指当元素未显式设置该属性时,会自动从其父元素继承值的属性。以下是主要可继承属性的分类列表:
📜 文本与字体相关
color:文本颜色font及其子属性:font-family:字体系列font-size:字体大小font-style:斜体/正常(如italic)font-weight:字体粗细(如bold)font-variant:小型大写字母(如small-caps)
line-height:行高text-align:文本对齐方式(如left,center)text-indent:首行缩进text-transform:文本转换(如uppercase,capitalize)letter-spacing:字母间距word-spacing:单词间距white-space:空白处理方式(如nowrap)direction:文本方向(如rtl从右到左)
📝 列表相关
list-style及其子属性:list-style-type:列表标记类型(如disc,decimal)list-style-position:标记位置(inside/outside)list-style-image:自定义列表标记图片
🖋️ 其他属性
visibility:元素可见性(如hidden)cursor:鼠标指针样式(如pointer)quotes:引号样式(如« " »)
⚠️ 重要注意事项
不可继承的常见属性:
width/height、margin/padding/border、background、position、display、float、overflow、z-index等布局和盒模型属性不会继承。强制继承:
通过inherit关键字可强制继承父元素的值(即使本身不可继承):cssdiv { background: inherit; /* 强制继承背景 */ }表单元素例外:
部分表单控件(如<input>、<textarea>)默认不继承文本属性,需手动设置:cssinput, button, textarea { font-family: inherit; /* 手动启用继承 */ color: inherit; }
🌰 继承示例
html
<div style="color: blue; font-size: 20px;">
父元素文本
<p>子元素自动继承蓝色和20px字体</p>
</div>💡 最佳实践:利用继承减少重复代码(如全局设置
font-family在<body>上)。通过浏览器开发者工具检查样式时,继承值通常显示为 灰色 或标明 "Inherited from...”。