简体   繁体   English

如何在html / css中设置字体系列和大小

[英]How to set font family and size in html/css

I'd like to use css syntax in HTML document to set the font-family to "Arial" and font-size to 0.3cm for the whole document. 我想在HTML文档中使用css语法将整个文档的字体系列设置为“ Arial”,将字体大小设置为0.3cm。

I use this code: 我使用以下代码:

<style>
body { font-family: Arial; font-size: 0.3cm }
</style>

I am not sure if writing only Arial is enough, or should I write something like this? 我不确定只写Arial是否足够,还是应该写这样的东西?

<style>

body { font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif; font-size: 0.3cm }
  </style>

and I am also not sure if I cam use "cm" in the code, I mean it works in the browser but is it correct "code-wise" ? 而且我也不确定我是否在代码中使用“ cm”,我的意思是它可以在浏览器中使用,但是否正确“ code-wise”?

thanks 谢谢

font-family: Arial 字体家族:Arial

This means the browser will use Arial if you have it installed on your system. 这意味着如果您的系统上安装了Arial,则浏览器将使用Arial。 If not, it will use whatever the default font is for your browser. 如果没有,它将使用您浏览器的默认字体。

font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif 字体家族:Arial,'Helvetica Neue',Helvetica,sans-serif

This means the browser will use Arial if you have it installed on your system. 这意味着如果您的系统上安装了Arial,则浏览器将使用Arial。 If not, it will use Helvetica Neue if you have it installed on your computer. 否则,如果您的计算机上已安装Helvetica Neue,它将使用Helvetica Neue。 If not, it will use Helvetica if you have it installed on your computer. 否则,如果您的计算机上已安装Helvetica,它将使用Helvetica。 If not, it will use whatever the default sans-serif font is for your browser. 如果没有,它将使用您浏览器的默认sans-serif字体。

Both are perfectly valid. 两者都是完全有效的。 They just do slightly different things. 他们做的事情略有不同。

and I am also not sure if I cam use "cm" in the code 而且我也不知道我是否在代码中使用“ cm”

Yes, cm is a valid CSS unit of measurement. 是的,cm是有效的CSS度量单位。

I am not sure if writing only Arial is enough, or should I write something like this? 我不确定只写Arial是否足够,还是应该写这样的东西?

You can use Arial alone but It is advisable to use font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif; 您可以单独使用Arial,但建议使用font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif; Just is case Arial can not be used. 只是在不能使用Arial的情况下。

and I am also not sure if I cam use "cm" in the code, I mean it works in the browser but is it correct "code-wise" ? 而且我也不确定我是否在代码中使用“ cm”,我的意思是它可以在浏览器中使用,但是否正确“ code-wise”?

You can use cm but It seems it is recommended only for print by w3.org ,check this link http://www.w3.org/Style/Examples/007/units.en.html 您可以使用cm,但似乎仅建议w3.org进行打印,请检查此链接http://www.w3.org/Style/Examples/007/units.en.html

The recommended units for font size are em, px, %,rem 建议的字体大小单位为em,px,%,rem

The font-size property can accept values of type length . font-size属性可以接受length类型的值。 As of the time of writing, the exhaustive list of these types (excluding experimental units) is: 截至撰写本文时,这些类型的详尽列表(不包括实验单位)为:

em, ex, ch, rem, vh, vw, vmin, vmax, px, mm, cm, in, pt, pc em,ex,ch,rem,vh,vw,vmin,vmax,px,mm,cm,in,pt,pc

So, yes. 所以,是的。 You can use cm (centimeters) as a unit for that property. 您可以将cm (厘米)作为该属性的单位。 You should be aware, though, that 1cm rarely equals one true centimeter on screen, due to differing pixel densities on various displays. 但是,您应该知道,由于各种显示器上的像素密度不同, 1cm很少会在屏幕上等于一个真实厘米。 If that's really what you want, you could use the mozmm unit of measurement, although it is an experimental unit that is only supported by Firefox browsers. 如果这确实是您想要的,则可以使用mozmm单位,尽管这是仅由Firefox浏览器支持的实验单位。 The cm unit is used more often in stylesheets targeted at physical printed media. cm单位在针对物理印刷媒体的样式表中更常使用。

The font-family property accepts a stack (comma-separated list) of font family names. font-family属性接受字体家族名称的堆栈(以逗号分隔的列表)。 The browser will use the first one in the stack that it happens to recognize (installed on the computer). 浏览器将使用碰巧能够识别的堆栈中的第一个(安装在计算机上)。

Using font-family: Arial is a pretty safe bet, since almost all computers have the Arial font, but to be safe it is best to include a couple of fall-back fonts. 使用font-family: Arial是一个非常安全的选择,因为几乎所有计算机都使用Arial字体,但是为了安全起见,最好包括两个后备字体。 Quotation marks (or single-quotes) are traditionally used around multi-word font names or font names with numbers or symbols in them. 传统上,引号(或单引号)用于多字字体名称或其中带有数字或符号的字体名称。 It is also considered best-practice to include a <generic-name> at the end of the list. 在列表的末尾包含<generic-name>也被认为是最佳实践。 The exhaustive list of generic fonts is: 通用字体的详尽列表为:

serif, sans-serif, monospace, cursive, fantasy 衬线,无衬线,等宽,草书,幻想

So, the second option you listed for font-family is a little bit more "bulletproof". 因此,您为font-family列出的第二个选项更加“防弹”。 It lists some fall-back options and ends with a generic font in case the client has none of the hand-picked fonts installed. 它列出了一些后备选项,并以通用字体结尾,以防客户端未安装任何手写字体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM