0

transparent -- Specifies that the color should be transparent. This is default
inherit -- Inherits this property from its parent element.

But practically speaking, I don't see any difference in them. In the following example, both child elements will have coral background color.

<div style="background-color: coral">
  <div style="background-color: transparent">
    transparent
  </div>
  <div style="background-color: inherit">
    inherit
  </div>
</div>

In which conditions would I get a different result?

DarkBee
  • 16,592
  • 6
  • 46
  • 58
viam0Zah
  • 25,949
  • 8
  • 77
  • 100
  • If the parent is coral, and you inherit, it will be coral. Also, if it's transparent, it will be transparent on it's parent, so it will be coral aswell – arlan schouwstra Jul 14 '23 at 07:26

3 Answers3

5

You can clearly see a difference when the color has transparency (related: Color of stacked semi-transparent boxes depends on order?)

div {
  padding: 20px;
}
<div style="background-color: rgb(255 127 80 / 50%)">
  <div style="background-color: transparent">
    transparent
  </div>
  <div style="background-color: inherit">
    inherit
  </div>
</div>
Temani Afif
  • 245,468
  • 26
  • 309
  • 415
3

See this example:

div {
  border: 1px solid black;
  padding: 1em;
  margin-right: 50%;
  margin-bottom: 1em;
}

div div {
  margin-right: -100%;
}
<div style="background-color: coral">
  <div style="background-color: transparent">
    transparent
  </div>
  <div style="background-color: inherit">
    inherit
  </div>
</div>

Transparent elements have no background color and you see through them, inheriting elements have a background color (that of their parent). This does make a difference in some situations, like here.

deceze
  • 510,633
  • 85
  • 743
  • 889
1

The difference.

When you set transparent then anything behind can be seen not just the parent background. That's the main difference.

<div style="background-color: coral">
  <div style="background-color: transparent; position: relative; z-index: 2">
    transparent
  </div>
  <div style="background-color: inherit; position: relative; z-index: 2">
    inherit
  </div>
  <div style="background: red; height: 100px; float: right; margin-top: -35px; position: relative; z-index: 1">
    difference
  </div>
</div>
Md. Rakibul Islam
  • 2,140
  • 1
  • 7
  • 14