CSS 如何为html网页上的所有元素指定字体属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3942254/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 22:58:20  来源:igfitidea点击:

How to specify font attributes for all elements on an html web page?

css

提问by Proud Member

When I set the font family, font size, color etc. it seems that some nested elements override these with ugly browser defaults.

当我设置字体系列、字体大小、颜色等时,似乎一些嵌套元素用丑陋的浏览器默认值覆盖了这些。

Must I really specify those a dozens of times for any kind of element on my page, or is there a way to set them globally once and forever?

我真的必须为页面上的任何类型的元素指定几十次,还是有办法一次性地全局设置它们?

How to do that?

怎么做?

回答by Bazzz

* {
 font-size: 100%;
 font-family: Arial;
}

The asterisk implies all elements.

星号表示所有元素。

回答by Paul

If you're using IE, chances are it will revert to the browser defaults for certain elements, like tables. You can counter that with something like the following CSS:

如果您使用的是 IE,它可能会恢复某些元素的浏览器默认设置,例如表格。你可以用类似下面的 CSS 来解决这个问题:

html, body, form, fieldset, table, tr, td, img {
    margin: 0;
    padding: 0;
    font: 100%/150% calibri,helvetica,sans-serif;
}

input, button, select, textarea, optgroup, option {
    font-family: inherit;
    font-size: inherit;
    font-style: inherit;
    font-weight: inherit;
}

/* rest of your styles; like: */
body {
    font-size: 0.875em;
}

Edit: you may want to read up on CSS resets; see threads like this one

编辑:您可能想阅读 CSS 重置;看到线程像这样一个

回答by Chris Cox

I can't stress this advice enough: use a reset stylesheet, then set everything explicitly. It'll cut your cross-browser CSS development time in half.

我不能充分强调这个建议:使用重置样式表,然后明确设置所有内容。它会将您的跨浏览器 CSS 开发时间缩短一半。

Try Eric Meyer's reset.css.

试试 Eric Meyer 的reset.css。

回答by jimplode

you can set them in the body tag

你可以在 body 标签中设置它们

body
{
    font-size:xxx;
    font-family:yyyy;
}

回答by Tyler Treat

If you specify CSS attributes for your bodyelement it should apply to anything within <body></body>so long as you don't override them later in the stylesheet.

如果您为body元素指定 CSS 属性,<body></body>只要您稍后不在样式表中覆盖它们,它就应该应用于其中的任何内容。

回答by Stepan Poperechnyi

If you want to set styles of all elements in body you should use next code^

如果你想设置 body 中所有元素的样式,你应该使用下一个代码^

  body{
    color: green;
    }