如何将全局字体应用于整个 HTML 文档
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7025756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Apply global font to whole HTML document
提问by Sweety
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.
我有一个 HTML 页面,其中包含一些文本和格式。我想让它具有相同的字体系列和相同的文本大小,而忽略文本的所有内部格式。
I want to set a global font format for the HTML page.
我想为 HTML 页面设置全局字体格式。
How can I achieve this?
我怎样才能做到这一点?
回答by Amadiere
You should be able to utilize the asterisk and !important
elements within CSS.
您应该能够!important
在 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).
星号匹配所有内容(如果没有,您可能也可以逃脱html
)。
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)
在!important
确保没有任何东西可以替代你在这个样式设置什么(除非它也很重要)。(这是为了满足您的要求,即它应该“忽略文本的内部格式” - 我认为这意味着其他样式无法覆盖这些)
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.
大括号内的其余样式就像任何其他样式一样,您可以在其中做任何您想做的事情。我选择更改字体大小、颜色和系列作为示例。
回答by Teneff
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;
}
回答by Petecoop
Set it in the body selector of your css. E.g.
将它设置在你的 css 的 body 选择器中。例如
body {
font: 16px Arial, sans-serif;
}
回答by karllindmark
Use the following css:
使用以下 css:
* {
font: Verdana, Arial, 'sans-serif' !important;/* <-- fonts */
}
The *
-selector means any/allelements, but will obviously be on the bottom of the food chain when it comes to overriding more specificselectors.
该*
-selector指任何/所有的元素,但显然是在食物链的底层,当谈到覆盖更具体的选择。
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
).
请注意,!important
-flag 会将font
-style for呈现为*
绝对的,即使已使用其他选择器来设置文本(例如, thebody
或 a p
)。
回答by Rakowu
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.
你永远不应该使用* + !important
. 如果您想更改 HTML 文档某些部分的字体怎么办?你应该总是使用 body 而不重要。!important
仅在没有其他选择时才使用。
回答by anglimasS
Try this:
尝试这个:
body
{
font-family:your font;
font-size:your value;
font-weight:your value;
}