3

I have the HTML below. In order to get green text in the text input I have to inherit the color from the parent <span>; otherwise, the text input would have black text. Why is this necessary? Why doesn't the input element just inherit the color property from its parent?

Are there other other elements that don't inherit properties from their ancestors?

<p>Without color: inherit;</p>
<div style="color: darkgreen;">
    <input type="text" value="Displays black" />
</div>

<p>With color: inherit;</p>
<div style="color: darkgreen;">
    <input type="text" style="color: inherit;" value="Displays green" />
</div>
Joundill
  • 6,828
  • 12
  • 36
  • 50
Tom Baxter
  • 2,110
  • 2
  • 19
  • 38

2 Answers2

4

It's your user agent stylesheet which is overriding the color setting on the input.

Here are the input rules from Chrome's user agent stylesheet:

enter image description here

Note the rule:

input {
    color: -internal-light-dark(black, white);
}

This is more specific than your color rule, so overrides it.

Browsers are free to implement whatever they want in their own stylesheets, so this depends from browser to browser.

You can read more here

Joundill
  • 6,828
  • 12
  • 36
  • 50
1

CSS applies styles based on the cascade, specificity, and inheritance.

In short, browsers have a style sheet (check out @Joundill's answer) that they apply before applying any site styles and it probably includes something like:

input {
  color: black;
}

This style takes preference over your inline style on the parent span.

Look into applying a CSS reset to reset the browser style sheet.

cam
  • 3,179
  • 1
  • 12
  • 15