css - CSS中的inherit关键字有什么作用?

标签 css

谁能解释一下 inherit 关键字在 CSS 中的含义及其工作原理?

最佳答案

它将使用与其父级具有的相同属性相同的值。

body {
   margin: 234px;
}
h1 {
   margin: inherit; /* this equals 234px in this instance */
}
<body>
   <h1></h1>
</body>

如果 <h1> 有多个实例在文件中,它将占用其父元素的边距,因此 234px 并不总是它的值。例如:

<body>
    <h2></h2>
    <div>
        <h2></h2>
    </div>
</body>
body {
    margin: 20px;
}
div {
    margin: 30px;
}
h2 {
    margin: inherit; /* 20px if parent is <body>; 30px if parent is <div> */
}

关于css - CSS中的inherit关键字有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2002514/

相关文章:

javascript - 如何在手机上缩小页面或div

CSS 边框锯齿状,带有不同大小/颜色的边框

css - 我的 css refs 都不起作用 - 这包括外部和内部 css - 总是收到 404 错误

asp.net - 网络机器人问题

javascript - 第三方应用程序中显示多个滚动条

javascript - 将包装器的高度绑定(bind)到特定子元素的高度

html - 在 wordpress 的左侧和右侧添加带有文本的页脚

css - 下拉菜单隐藏在 IE 中的其他元素后面

html - 如何使CSS网格只出现在手机上而不出现在桌面上

html - 如何让媒体查询/css 在 html 电子邮件中工作