【发布时间】:2014-06-17 07:46:28
【问题描述】:
例如,我有一个字体颜色设置为紫色的 div,div 中的一些文本继承了颜色,但有些不是,例如在一张桌子上。 (以下代码另存为test.html并在浏览器中打开进行测试)
<div style="color: purple;">
<p>Some text</p> <!-- text here is purple -->
<table>
<tr>
<td>Table cell 1</td> <!-- text here is NOT purple -->
<td>Table cell 2</td> <!-- text here is NOT purple -->
</tr>
</table>
Another text <!-- text here is purple -->
</div>
如果我将 div 替换为 body,它们会继承。
<body style="color: purple;">
<p>Some text</p> <!-- text here is purple -->
<table>
<tr>
<td>Table cell 1</td> <!-- text here is purple -->
<td>Table cell 2</td> <!-- text here is purple -->
</tr>
</table>
Another text <!-- text here is purple -->
</body>
我想知道:
- 他们为什么以及如何不从父容器继承样式?
- 是否有其他解决方法可以使内容元素继承字体颜色,就好像它们继承自正文一样?
【问题讨论】:
-
在哪个浏览器中?我在这两种情况下都看到紫色文字。
-
您可能在某处进行了重置。颜色通常应该继承。
-
@panther 我在 IE、Firefox、Chrome、Opera、Safari(在 Windows 7 SP1 上)上测试了它们,所有这些都给出了相同的结果......
-
在大多数浏览器中,您可以右键单击页面上的元素并单击“检查元素”,这将打开一个窗口,显示正在应用的样式以及覆盖的样式。
标签: html css inheritance