What is the purpose of using font: inherit? - css

I just wanted to know why font: inherit; is used in Cascading Style Sheets.

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:
p { color: blue; }
div.important { color: red; }
<div class="important">
<p>This is a text</p>
</div>
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:
div.important p { color: inherit; }

The declaration font:inherit is used in many “CSS Reset” stylesheets, which have often been copied into various libraries and frameworks. The original Reset CSS by Eric Meyer has font:inherit. No specific motivation is given. The overall rationale is said to be “to reduce browser inconsistencies in things like default line heights, margins and font sizes of headings, and so on”. But Meyer links to a previous post of his where he explains the idea, saying, among other things: “I want all this because I don’t want to take style effects for granted. This serves two purposes. First, it makes me think just that little bit harder about the semantics of my document. With the reset in place, I don’t pick strong because the design calls for boldfacing. Instead, I pick the right element—whether it’s strong or em or b or h3 or whatever—and then style it as needed.”
Several HTML elements have a default rendering in browsers as regards to font properties: headings, form fields, table header cells, some phrase elements, etc. 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. It has gained popularity and often applied routinely.

The font CSS property is either a shorthand property for setting font-style, font-variant, font-weight, font-size, line-height, and font-family; or a way to set the element's font to a system font, using specific keywords. -MDN
By using font: inherit;, it tells an element to inherit those relevant values from its parent container. See the examples below:
In the 1st group: you can see there are some special style set by default from the browser, h1 is bolder and larger it also inherits the relevant values from body automatically. However, for the input element, it doesn't inherit any of those values, since it's a replaced element and serves its unique purpose.
In the 2nd group: It forces those elements to inherit those values from body by using font: inherit;.
Now, you see what it does. It's entirely up to you when to use it, for instance you might want to use <h1> tag for the site logo in the home page, and you probably want to make it look no difference than it appears on other pages. And of course, it's commonly being used in CSS reset frameworks.
body {
font-family: "Comic Sans MS", "Comic Sans", cursive;
font-style: italic;
}
.inherit {
font: inherit;
}
<h1>Heading</h1>
<input type="button" value="Button">
<hr>
<h1 class="inherit">Heading</h1>
<input class="inherit" type="button" value="Button">

Not all browsers inherit font properties on all elements. Netscape 4.x was notoriously bad about about inheritance. Consider the following style:
body { background: black; color: white }
In Netscape 4.x, the color was not applied to table elements, so you would end up with the default black text inside the table on a black background.
Font properties have the same kind of deal for some elements, particularly form elements (and table elements for older browsers). It's not uncommon to see a definition like this:
table, form { font: inherit }

The inherit is used to get the properties from the parent element. In other words, inherit the properties of parent element.
The default property is inherit, it means, say you have div and a p.
<div>
<p>Hello World!</p>
</div>
Now you give a style:
div {font-famlily: Tahoma;}
p {font-family: inherit;}
That font-family is inherited to the p from its parent element div.

Using {font:inherit;} in CSS makes sense because various user agents (a.k.a. browsers) have user agent stylesheet (read: default stylesheet) with something like
body
{
font: -magic-font-from-user-preferences;
}
textarea, input
{
font: monospace;
}
The {font:inherit;} is used to workaround the special case where font or font-family is not inherited by default due to user agent stylesheet but the author of the content wishes the font family to be inherited.
The actual user agent behavior with the value inherit differs due to various bugs, unfortunately. Resulting behavior may be closer to author intent than the default value, though.

inherit in CSS simply means it inherits the values from parent element, so for example:
<div style="font-family: Arial;">
<p style="font-family: inherit; /* This will inherit the font-family of the parent p*/">
This text will be Arial..And inherit is default behavior of the browser
</p>
</div>
Here <p> inherits the font-family: Arial; from it's parent div
You need to use inherit for example in the case of anchor elements,
the color property is commonly set to blue in the user agent style
sheet. If you wanted to reinforce the importance of the inherited
value, you could use the value inherit in an author or user style
sheet, overwriting the user agent style sheet declaration.
More Reference

Related

Font family within a tag

When I have an a tag within a div whose font-family is specified, the font-family seems to affect only the text within the div, but not the text within the a tag. Why is that, and how can it be fixed?
<div style="font-family: 'Times New Roman', Times, serif"> Hello world! Click here! </div>
As #John mentions in comment; you might have some other CSS linked file which is overriding your font-family. You can use inherit property inline, as your div also has style defined inline.
Click here!
The problem described is not caused by the code posted, i.e. the cause is in the part of code that was not disclosed. Generally, setting e.g. font-family on an element affects as such only the text directly in the element, not text wrapped in an inner element. However, if there is no CSS rule (in author, user, or browser style sheets in use) for an inner element, then an inner element inherits font-family from its parent. But in this case there apparently is a CSS rule that sets font-family on the a element.
The fix depends on what other CSS code is used. This does not seem to be a real-life case but just as an exercise, so it’s better to use a different approach, e.g.
<div class="foobar"> Hello world! Click here! </div>
with the following CSS code in a linked external style sheet or within a style element on the page:
.foobar, .foobar a { font-family: 'Times New Roman', Times, serif }
Replace foobar by a name that reflects the meaning or role of the element, just to make the code more readable to humans.
However, depending on the other CSS code that sets font-family on the a element now, you may need to use a more specific selector, or the !important specifier, or both.

Using data attributes instead of classes in CSS

HTML
<div data-foo> ... </div>
CSS
div[data-foo]{ ... }
Is this a good idea? Are there any drawbacks?
I think the data- approach makes sense when I have hundreds of "foo" elements, because the HTML markup size decreases (-3 characters for each element).
div[data-foo] is not supported in old IE (IE6, see here: http://www.quirksmode.org/css/selectors/)
div[data-foo] makes less semantic sense
class="foo" and data-foo will take up about the same space when DEFLATE-d. If you haven't set up your server to deflate, you should.
class=foo is only one character longer than data-foo even uncompressed, and is perfectly valid HTML.
It totally depends on you, for example elements must be having different attributes, so you need to define styles and even repeat some, instead I'll use a class which I can use for both, instead of using attribute selector which will limit my declared properties upto an element with that attribute, where you can freely use classes regardless of element attribute combination
.class { /* You can use this anywhere you need these properties */
font-family: Arial;
font-size: 13px;
}
Where as this will limit to ELEMENT-ATTRIBUTE combination
div[data-menu] { /* This will LIMIT you to a combination of div
element having an attribute called data-menu */
font-family: Arial;
font-size: 13px;
}
Important : Specificity will make you a huge mess

Override font size in a DIV using CSS

In a DIV I place some text/html code which get loaded from a database. This text sometimes contains font size definitions (ex: font size="3"). is there a way to override this font size in this specific DIV using CSS.
I am grateful for any help.
Assuming mark-up similar to the following:
<div>
<font size="1">Some text at 'size="1"'</font> and natively-sized text, with more at <font size="26">'size="26".'</font>
</div>​
Then you can explicitly instruct CSS to inherit the font-size from the parent element:
div {
font-size: 1.5em;
}
div font {
font-size: inherit;
}
JS Fiddle demo.
Please note, of course, that font is deprecated and should, therefore, not be used (as support for the element can stop without notice, and/or implementations change without warning).
Incidentally, while !important will force a declaration to override the usual cascade of styles, it's taking a sledgehammer to crack a nut; and, if it can be avoided (and in this case, it seems, it can be avoided) it should be, since it complicates later debugging of styles, and associated inheritance problems.
Further, this is treating the symptom of your problem; the problem you're really facing is the presence of the font tags in your content/database. This should be corrected, by removing the elements, and replacing them with appropriately-styled elements, such as em, span and so forth...
References:
font element, at the W3.org.
font element at the MDN.
Using the CSS !important notation you are telling the browser to overwrite any font-size defined inside your div:
From the above link it reads:
However, for balance, an "!important" declaration (the delimiter token "!" and keyword "important" follow the declaration) takes precedence over a normal declaration.
Example
See this working Fiddle Example!
.htmlContents * {font-size:10px!important;}
<div class="htmlContents">my database html content</div>
One idea: give these text tags an id or class, then use JavaScript to find these elements and change the style on them.
How about stripping the "font" tags from the text before inserting into the div? Then just style the div with a font size.
Thanks had same problem couldn't override font-size of footer of a nested element a.
.footer ul li a {
font-size: 20px ;
height: 25px;
}

How to clear CSS ascendant font on all descendants within a div?

In my default style sheet, I set the default font like this:
body, div, span, ...
{
font:normal normal normal 13px Arial, helvetica, Sans-Serif;
}
which works great, except that when I add a 3rd-party control on the page, it's inheriting this font, which makes it not display properly.
If I wrap the 3rd-party control in a div, how can I remove/clear the globally set font, so that anything inside the div will act as if the font was never set?
The only way to prevent a rule-set from applying to an element where the selector matches or to prevent inheritance is to explicitly set a different value.
You cannot say "Inherit from an element that is not the parent"
You cannot say "Ignore the author stylesheet for this element"
So figure out what "display properly" means, express that in CSS and apply it to the third party content.
You'd have to give a class, or id, to the div containing the 3rd party control, and explicitly over-write the style rules for that container. Unless I'm missing something, if you set the default font for the body all children and descendants of the body should automatically inherit (you shouldn't need to specify div, span /* etc */).
But you can't specify a non-inherit rule, you have to over-write inheritance, sadly (though sensibly, I think).

How can I apply a font type to everything using css

Basically, I need to adjust the font type everywhere it isn't specified. How can I do that? table, div, span, p, input, you name it. Is there a way I can do them all with 1 CSS rule that I can add?
Yes there is, use it on the body element and it will cascade down to all children unless you specify otherwise.
body {
font-family: Arial, Verdana, sans-serif;
}
#tahoma {
font-family: Tahoma, sans-serif;
}
You can then override it
<body>
<div id="default">
I will inherit the body font-family, in this case Arial because no other rule has been set!
</div>
<div id="tahoma">
<p>Me, <span>Me</span> and <strong>Me</strong> are using Tahoma because our parent #tahoma says so.</p>
</div>
</body>
This is why it's called Cascading Style Sheets. Styles set at any level will "cascade" down. So if you set this styling on the body element, every other element in the body will take on the same styles.
body {
font: ...;
}
What's missing from the existing answers is the fact that the font family of some elements, like input, is unaffected by simply setting the font family for body, as suggested here.
The reason is that an element inside the body inherits the font family setting only if there is no rule that specifies this setting for this element. For elements like input (and also pre, code, etc.), there usually are rules that specify the font family: the basic styling rules defined by the browser.
To override the browser default, one needs to explicitly set the font family for these elements:
body, input, button, textarea {
font-family: ...;
}
Note that !important should not be necessary to achieve this. Using !important is a sign that the stylesheets were not designed carefully enough.
Use !important css property.
http://webdesign.about.com/od/css/f/blcssfaqimportn.htm
Like:
*{
YOUR_FONT_STYLE
}

Resources