Inherit CSS Properties

Which CSS properties are inherited?

Here is the list of all inheritable properies.
I'm working with W3C's information, so I'd guess it should be correct. But knowing web browsers (IE specifically), some of these might not be inheritable by all browsers:

  1. azimuth
  2. border-collapse
  3. border-spacing
  4. caption-side
  5. color
  6. cursor
  7. direction
  8. elevation
  9. empty-cells
  10. font-family
  11. font-size
  12. font-style
  13. font-variant
  14. font-weight
  15. font
  16. letter-spacing
  17. line-height
  18. list-style-image
  19. list-style-position
  20. list-style-type
  21. list-style
  22. orphans
  23. pitch-range
  24. pitch
  25. quotes
  26. richness
  27. speak-header
  28. speak-numeral
  29. speak-punctuation
  30. speak
  31. speech-rate
  32. stress
  33. text-align
  34. text-indent
  35. text-transform
  36. visibility
  37. voice-family
  38. volume
  39. white-space
  40. widows
  41. word-spacing

Inherit css property from another property of a different class

Five years later...

I know that css variables would be the way to go but since it is in experimental phase, it would not be a suitable for our project.

As it's 2021 (and no-one is using Internet Explorer 11 anymore, phew, and all the major browsers fully support CSS Variables CSS Custom Properties) so you can now use var().

If you simply want to only define 20px once to avoid repeating yourself in CSS, then set a custom-property on a common ancestor of both .class1 and .class2 elements: most people use html (or html:root or just :root) for this:

:root {
--my-height: 20px;
}

.class1 {
line-height: var(--my-height);
}

.class2 {
height: var(--my-height);
}

Now, if you want .class2 elements to "inherit" their height: from any ancestor class1 elements instead of <html>, then this should work:

:root {
--my-height: 50px; /* Default value for .class2 elements which are not descendants of .class1` */
}

.class1 {
--my-height: 20px; /* Redefining the value */
line-height: var(--my-height);
}

.class2 {
height: var(--my-height);
}

...or if you want only .class2 descendants of .class1 to use the value:

:root {
}

.class1 {
--my-height: 20px;
line-height: var(--my-height);
}

.class1 .class2 {
height: var(--my-height);
}

But you probably shouldn't be setting line-height anyway - doing-so is a sign that you're misusing display: inline; or vertical-align:;.

Specificity of inherited CSS properties

Any declaration that matches element directly will get priority over the property that's inherited from the element's parent. Specificity has nothing to do with that.

How do I set a value of `inherit` to a CSS custom property?

I did some thinking and this solution just hit me. I can use custom properties in conjunction with preprocessor mixins.

<style type="text/less">
// NOTE: not syntactically valid CSS!
.mx-border(@arg) {
border: @arg;
}
figure {
.mx-border(1px solid red);
--foobar: 1px solid green;
}
figure > figcaption {
.mx-border(var(--foobar));
}
figure > figcaption:hover {
.mx-border(inherit);
}
</style>
<figure>this figure has a red border
<figcaption>this figcaption has a green border
because it inherits --foobar</figcaption>
</figure>
<!-- on hover -->
<figure>this figure has a red border
<figcaption>This figcaption
has a red border because the mixin
sets the `border` property to `inherit`.</figcaption>
</figure>

This way, I can encapsulate all the dependent styles into the .mx-border() mixin. Doing this doesn’t take advantage of CSS custom properties, but it does alleviate the hassle of writing everything a second time for the :hover.

Essentially it is the same as writing border: inherit;, with the added ability of putting more styles into the mixin and not having to duplicate them.

Inherit css properties

You can define common styles for two elements at once like so:

p, a {
font-size: 1em;
}

And then extend each one with their individual properties as you want:

p {
color: red;
}

a {
font-weight: bold;
}

Keep in mind: Styles defined later in a style sheet generally override properties defined earlier.

Extra: If you haven't already, I recommend getting the Firebug Firefox extension so you can see what styles the elements on your page are receiving and where they are inherited from.

CSS properties are not being inherited?

Some properties are inherited, and some aren't.

  • width. Inherited: no
  • background. Inherited: no
  • margin. Inherited: no
  • padding. Inherited: no
  • font-family. Inherited: yes

If you want some element to inherit the value of a non-inheritable property from its parent, you can use inherit value.


I suggest reading Inheritance section of the spec:

Some values are inherited by the children of an element in the document tree, as described above. Each property defines whether it is inherited or not.

When inheritance occurs, elements inherit computed values. The computed value from the parent element becomes both the specified value and the computed value on the child.

Each property may also have a cascaded value of 'inherit', which means that, for a given element, the property takes the same specified value as the property for the element's parent. The 'inherit' value can be used to enforce inheritance of values, and it can also be used on properties that are not normally inherited.



Related Topics



Leave a reply



Submit