我被介绍到以下代码:
<!DOCTYPE html>
<html>
<head>
<style>
span {
color: blue;
border: 1px solid black;
}
.extra span {
color: inherit;
}
</style>
</head>
<body>
<div>
Here is <span>a span element</span> which is blue, as span elements are set to be.
</div>
<div class="extra" style="color:green">
Here is <span>a span element</span> which is green, because it inherits from its parent.
</div>
<div style="color:red">
Here is <span>a span element</span> which is blue, as span elements are set to be.
</div>
</body>
</html>
Link: https://www.w3schools.com/cssref/tryit.asp?filename=trycss_inherit https://www.w3schools.com/cssref/tryit.asp?filename=trycss_inherit
我不知道为什么额外跨度的颜色是绿色的。当他们使用“继承”值时,他们会采用与第一个引入的颜色相似的颜色吗?是这样吗?
这里的“父母”和“孩子”指的是什么?他们的定义是什么?
如果我们有一个<p>
里面一个<div>
元素,即<div>
是 的父级<p>
和<p>
是的孩子<div>
<div>
<p></p>
</div>
你可以阅读这个网页:https://www.w3schools.com/js/js_htmldom_navigation.asp https://www.w3schools.com/js/js_htmldom_navigation.asp它完美地解释了。
节点树中的节点相互之间具有层次关系。
术语“父母”、“孩子”和“兄弟姐妹”用于描述这种关系。
- 在节点树中,顶部节点称为根(或根节点)
- 每个节点只有一个父节点,除了根节点(根节点没有
家长)
- 一个节点可以有多个子节点
- 兄弟姐妹(兄弟或姐妹)是具有相同父节点的节点
CSS 使用这种结构来制作特定的选择器,例如first-child
, :nth-child(n)
, :last-child
...
您可以看到所有 CSS 选择器here https://www.w3schools.com/cssref/css_selectors.asp
价值inherit
应用于 HTML 节点的 CSS 属性,只需采用该属性的父值。
所以如果我有一个像这样的html:
<div>
<p></p>
<p class="red"></p>
</div>
和CSS一样:
div {
background-color: red;
}
div > p {
background-color: blue;
}
.red {
background-color: inherit;
}
具有红色类的 div,使用inherit
将采用父级的红色值。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)