CSS继承

CSS继承

CSS继承是一种机制,允许子元素自动继承父元素的某些样式属性,从而减少重复代码。

以下是一些常见的具有继承性的CSS属性:
color : 文字颜色
font-family : 字体族名称
font-size : 字体大小
font-weight : 字体粗细
line-height :行高
text-align : 文本对齐方式
text-indent : 文本块首行缩进
margin : 外边距
padding : 内边距

以下是一些不具有继承属性的CSS属性:
border:边框样式
width:宽度
height:高度
background:背景样式
position:定位方式

需要注意的是,并非所有属性都具有继承性,即使在上述示例中,继承性也可能受到其他因素影响。另外,有时可以使用inherit 关键字来显式地指定子元素继承父元素的样式。

以下是一个实例,演示CSS的继承效果:

html 复制代码
<!DOCTYPE html>
<html>
<head>
    <title>CSS继承示例</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            font-size: 16px;
            color: blue;
            text-align: center;
        }

        h1 {
            font-weight: bold;
            color: red; /* 覆盖继承的蓝色 */
        }

        p {
            line-height: 1.5; /* 继承body的font-family/font-size/color/text-align */
        }

        .container {
            margin: 20px;
            padding: 10px;
            background-color: lightgray;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Title</h1>
        <p>This is a paragraph of text.</p>
    </div>
</body>
</html>
相关推荐
求知若渴,虚心若愚。39 分钟前
Error reading config file (/home/ansible.cfg): ‘ACTION_WARNINGS(default) = True
linux·前端·ansible
LinDaiuuj2 小时前
最新的前端技术和趋势(2025)
前端
一只小风华~2 小时前
JavaScript 函数
开发语言·前端·javascript·ecmascript·web
程序猿阿伟3 小时前
《不只是接口:GraphQL与RESTful的本质差异》
前端·restful·graphql
若梦plus4 小时前
Nuxt.js基础与进阶
前端·vue.js
樱花开了几轉4 小时前
React中为甚么强调props的不可变性
前端·javascript·react.js
风清云淡_A4 小时前
【REACT18.x】CRA+TS+ANTD5.X实现useImperativeHandle让父组件修改子组件的数据
前端·react.js
小飞大王6664 小时前
React与Rudex的合奏
前端·react.js·前端框架
若梦plus5 小时前
React之react-dom中的dom-server与dom-client
前端·react.js
若梦plus5 小时前
react-router-dom中的几种路由详解
前端·react.js