html - 表格中的字体颜色不继承自父 div

标签 html css inheritance

例如,我有一个字体颜色设置为紫色的 div,div 中的一些文本继承了颜色,但有些则没有,例如在一张 table 上。 (将以下代码保存为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>

如果我用 body 替换 div,它们会继承。

<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>

我想知道:

  1. 为什么以及如何不从父容器继承样式?
  2. 是否有其他解决方法可以使内容元素继承字体颜色,就好像它们继承自正文一样?

最佳答案

这个“问题”是因为您没有声明文档类型,导致浏览器以 Quircks 模式运行。

这样做的一个结果就是您在此处看到的情况:表格元素正在获取 color: -webkit-text; 样式,该样式覆盖了从父 div 继承的样式。

添加:

<!DOCTYPE html>

在文档顶部将使浏览器呈现您想要的页面。

关于html - 表格中的字体颜色不继承自父 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24258380/

相关文章:

html - 如何忽略 wordpress 主题中的 <p> CSS 规则?

python - 在 python 中使用 Beautiful Soup 解析 html

javascript - Nodejs 聊天服务器跨页面持久化

css - 按钮 CSS 样式不适用于移动设备

scala - 在 Scala 中定义一个函数,该函数采用通配符数组作为参数

javascript - 我的 JavaScript 代码有什么问题?它不会得到 CharsLeft

html - 如何修复我的导航栏悬停的宽度?

javascript - 使用 cookie 保留整个网站的样式表偏好

java - 调用父类(super class)的getter会降低效率吗?

python - 在 Web.py/MIMERender 应用程序中重复类装饰器