表中的字体颜色不会从父div继承

Dan*_*Lin 6 html css inheritance

例如,我有一个字体颜色设置为紫色的div,div中的一些文本继承颜色,但有些不是,例如.在一张桌子里.

TEST1

<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>
Run Code Online (Sandbox Code Playgroud)

如果我用身体替换div,他们继承.TEST2

<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>
Run Code Online (Sandbox Code Playgroud)

我想知道:

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

Leo*_*eon 18

这个"问题"是由于您没有声明doctype,导致浏览器以Quircks模式运行.

这样做的一个结果就是你在这里看到的结果:table元素获得的color: -webkit-text;样式覆盖了父div的继承.

添加:

<!DOCTYPE html>
Run Code Online (Sandbox Code Playgroud)

在文档的顶部将导致浏览器呈现您想要的页面.

  • 就我而言,这是在 DOCTYPE 之前插入的 PHP 警告... (2认同)