Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Font color in a table doesn't inherit from parent div

For example I have a div with font color set to purple, some text within the div inherits the color, but some are not, e.g. in a table. (Save below code as test.html and open in the browser for testing)

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

If I replace the div with body, they DO inherit.

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

I'd like to know that:

  1. Why and how do they NOT inherit the style from the parent container?
  2. Is there an alternative workaround to make the content elements inherit the font color, as if they inherit from the body?
like image 773
Danny Lin Avatar asked Jun 17 '14 07:06

Danny Lin


People also ask

Is font inherited CSS?

Using CSS Reset, or specifically font: inherit means that on browsers supporting the inherit value, all such elements are rendered in copy text font, unless otherwise specified in a style sheet. So this is about a particular methodology (or, as some people might say, ideology or religion) of authoring and design.

What does inherit font-family mean?

The inherit option means it will receive the body's font family. To change to a different font family, you can do that from font manager.


1 Answers

This 'issue' is caused by the fact that you haven't declared a doctype, causing the browser to run in Quircks mode.

One result of this is what you see happening here: The table element is getting a color: -webkit-text; style which overrides the inheritance from the parent div.

Adding:

<!DOCTYPE html>

at the top of your document will cause the browser to render the page you'd want it to.

like image 90
Leon Avatar answered Oct 17 '22 05:10

Leon