How to Apply global font to whole HTML document

HtmlCssFont Family

Html Problem Overview


I have a HTML page which includes some text and formatting. I want to make it have the same font-family and the same text-size ignoring all inner formatting of text.

I want to set a global font format for the HTML page.

How can I achieve this?

Html Solutions


Solution 1 - Html

You should be able to utilize the asterisk and !important elements within CSS.

html *
{
   font-size: 1em !important;
   color: #000 !important;
   font-family: Arial !important;
}

The asterisk matches everything (you could probably get away without the html too).

The !important ensures that nothing can override what you've set in this style (unless it is also important). (this is to help with your requirement that it should "ignore inner formatting of text" - which I took to mean that other styles could not overwrite these)

The rest of the style within the braces is just like any other styling and you can do whatever you'd like to in there. I chose to change the font size, color and family as an example.

Solution 2 - Html

Best practice I think is to set the font to the body:

body {
    font: normal 10px Verdana, Arial, sans-serif;
}

and if you decide to change it for some element it could be easily overwrited:

h2, h3 {
    font-size: 14px;
}

Solution 3 - Html

Set it in the body selector of your css. E.g.

body {
    font: 16px Arial, sans-serif;
}

Solution 4 - Html

Use the following css:

* {
    font: Verdana, Arial, 'sans-serif' !important;/* <-- fonts */
}

The *-selector means any/all elements, but will obviously be on the bottom of the food chain when it comes to overriding more specific selectors.

Note that the !important-flag will render the font-style for * to be absolute, even if other selectors have been used to set the text (for example, the body or maybe a p).

Solution 5 - Html

You should never use * + !important. What if you want to change font in some parts your HTML document? You should always use body without important. Use !important only if there is no other option.

Solution 6 - Html

Try this:

body
{
    font-family:your font;
    font-size:your value;
    font-weight:your value;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSweetyView Question on Stackoverflow
Solution 1 - HtmlAmadiereView Answer on Stackoverflow
Solution 2 - HtmlTeneffView Answer on Stackoverflow
Solution 3 - HtmlPetecoopView Answer on Stackoverflow
Solution 4 - HtmlkarllindmarkView Answer on Stackoverflow
Solution 5 - HtmlRakowuView Answer on Stackoverflow
Solution 6 - HtmlanglimasSView Answer on Stackoverflow