继承是 CSS 中重要的概念之一,它是指子元素自动继承父元素的某些样式属性。理解继承对于减少代码重复、提高代码可维护性至关重要。
继承是指子元素自动继承父元素的某些样式属性,而无需显式设置这些属性。
/* color - 文本颜色 */
body {
color: #333;
}
/* font-size - 字体大小 */
body {
font-size: 16px;
}
/* font-family - 字体家族 */
body {
font-family: Arial, sans-serif;
}
/* font-weight - 字体粗细 */
body {
font-weight: normal;
}
/* font-style - 字体样式 */
body {
font-style: normal;
}
/* line-height - 行高 */
body {
line-height: 1.6;
}
/* text-align - 文本对齐 */
body {
text-align: left;
}
/* text-decoration - 文本装饰 */
a {
text-decoration: none;
}
/* text-transform - 文本转换 */
.title {
text-transform: uppercase;
}
/* letter-spacing - 字母间距 */
.text {
letter-spacing: 1px;
}
/* word-spacing - 单词间距 */
.text {
word-spacing: 2px;
}
/* text-indent - 文本缩进 */
p {
text-indent: 2em;
}
/* list-style-type - 列表标记类型 */
ul {
list-style-type: disc;
}
/* list-style-position - 列表标记位置 */
ul {
list-style-position: inside;
}
/* list-style-image - 列表标记图像 */
ul {
list-style-image: url('bullet.png');
}
/* border-collapse - 边框合并 */
table {
border-collapse: collapse;
}
/* border-spacing - 边框间距 */
table {
border-spacing: 5px;
}
/* caption-side - 标题位置 */
table {
caption-side: top;
}
/* empty-cells - 空单元格显示 */
table {
empty-cells: show;
}
/* visibility - 可见性 */
.container {
visibility: visible;
}
/* cursor - 光标样式 */
.button {
cursor: pointer;
}
/* quotes - 引号样式 */
q {
quotes: "“" "”" "‘" "’";
}
/* width - 宽度 */
.box {
width: 300px;
}
/* height - 高度 */
.box {
height: 200px;
}
/* padding - 内边距 */
.box {
padding: 20px;
}
/* margin - 外边距 */
.box {
margin: 20px;
}
/* border - 边框 */
.box {
border: 1px solid #333;
}
/* background-color - 背景颜色 */
.box {
background-color: #f5f5f5;
}
/* background-image - 背景图像 */
.box {
background-image: url('image.png');
}
/* background-position - 背景位置 */
.box {
background-position: center;
}
/* background-repeat - 背景重复 */
.box {
background-repeat: no-repeat;
}
/* background-size - 背景尺寸 */
.box {
background-size: cover;
}
/* position - 定位 */
.box {
position: relative;
}
/* top - 顶部位置 */
.box {
top: 10px;
}
/* right - 右侧位置 */
.box {
right: 10px;
}
/* bottom - 底部位置 */
.box {
bottom: 10px;
}
/* left - 左侧位置 */
.box {
left: 10px;
}
/* z-index - 堆叠顺序 */
.box {
z-index: 1;
}
/* float - 浮动 */
.box {
float: left;
}
/* display - 显示方式 */
.box {
display: block;
}
/* overflow - 溢出 */
.box {
overflow: hidden;
}
/* opacity - 透明度 */
.box {
opacity: 0.5;
}
/* box-shadow - 盒阴影 */
.box {
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
/* 全局样式设置 */
body {
color: #333;
font-size: 16px;
font-family: Arial, sans-serif;
line-height: 1.6;
text-align: left;
}
/* 子元素自动继承这些样式 */
.container p {
/* 继承 color、font-size、font-family、line-height、text-align */
}
.container h1 {
/* 继承 color、font-family、line-height、text-align */
/* 但 font-size 可能被覆盖 */
}
/* 组件样式继承 */
.card {
color: #333;
font-size: 16px;
font-family: Arial, sans-serif;
line-height: 1.6;
}
.card h2 {
/* 继承 color、font-family、line-height */
font-size: 24px;
}
.card p {
/* 继承 color、font-size、font-family、line-height */
}
/* 链接样式继承 */
body {
color: #333;
font-size: 16px;
font-family: Arial, sans-serif;
}
a {
color: #007bff;
text-decoration: none;
}
a:hover {
color: #0056b3;
text-decoration: underline;
}
/* 父元素样式 */
body {
color: #333;
font-size: 16px;
}
/* 子元素覆盖继承 */
.title {
color: #007bff;
font-size: 24px;
}
/* 使用 inherit 关键字显式继承 */
.button {
color: inherit;
font-size: inherit;
font-family: inherit;
}
/* 使用 inherit 关键字继承父元素的样式 */
.container {
color: #333;
}
.child {
color: inherit;
}
/* 使用 initial 关键字重置为初始值 */
.text {
color: initial;
font-size: initial;
}
/* 推荐 - 利用继承 */
body {
color: #333;
font-size: 16px;
font-family: Arial, sans-serif;
line-height: 1.6;
}
/* 不推荐 - 重复设置样式 */
p {
color: #333;
font-size: 16px;
font-family: Arial, sans-serif;
line-height: 1.6;
}
h1 {
color: #333;
font-size: 32px;
font-family: Arial, sans-serif;
line-height: 1.6;
}
/* 推荐 - 在 body 层级设置继承 */
body {
color: #333;
font-size: 16px;
}
/* 不推荐 - 在每个元素上设置样式 */
p {
color: #333;
font-size: 16px;
}
h1 {
color: #333;
font-size: 32px;
}
/* 注意 - 盒模型属性不可继承 */
.container {
width: 100%;
padding: 20px;
}
.child {
/* 不会继承 width 和 padding */
width: auto;
padding: 0;
}
/* 推荐 - 使用 inherit 关键字 */
.button {
color: inherit;
font-size: inherit;
}
/* 不推荐 - 重复设置相同的值 */
.button {
color: #333;
font-size: 16px;
}
继承是 CSS 中重要的概念之一:
记住以下几点: