I just wanted to know why font: inherit;
is used in Cascading Style Sheets.
相关问题
- Adding a timeout to a render function in ReactJS
-
Why does the box-shadow property not apply to a
- Add animation to jQuery function Interval
- jQuery hover to slide?
- Issue with star rating css
Like the other answers have said, it’s to inherit a CSS property from the parent element.
What the other answers have failed to say is why you’d need this. Because, after all, CSS properties are inherited anyway, right?
Well, no. Most are, by default (but link colour isn’t inherited from the parent element, for instance). But consider this case:
Now the text will be blue, not red. If we want the
<p>
to have its parent’s styling rather than its default styling, we have to override its CSS. We could of course repeat the property value (red
) but that violates DRY (don’t repeat yourself). Instead, we inherit it: