CSS的font和font-size

CSS的font和font-size。

引用

原帖收藏于IT老兵驿站

前言

今天遇到一个问题,没有搞明白font和font-size的关系,研究了https://www.w3schools.com/css/css_font.asp这个帖子,终于搞明白,不过搞不懂为什么,这个网站为什么也需要翻墙,所以就诞生了中文的http://www.w3school.com.cn/这样的网站。但是“取法乎上,才能得其中”,想得到最好的学习效果,还是应该去看原版的东西。

这篇文章原本以富文本编辑发表,但是富文本编辑器的引用功能失效,导致格式很乱,只好再编辑成Markdown的格式。

正文

先来破题,一言以蔽之,font相当于多个font相关属性的组合属性。

这篇笔记会把部分重要的原文摘录在下面,底下配上一定的翻译和笔记,这篇文章感觉英文相对都比较简单,没有太多需要翻译的地方。原文中有去往在线操练场的链接,也都是不翻墙而不能用。

The CSS font properties define the font family, boldness, size, and the style of a text.

CSS的font属性定义了font family、boldness,、size和一个文本的style。这是CSS的四个属性。

CSS Font Families

In CSS, there are two types of font family names:


generic family - a group of font families with a similar look (like “Serif” or “Monospace”)
font family - a specific font family (like “Times New Roman” or “Arial”)

Font Family

The font family of a text is set with the font-family property.


The font-family property should hold several font names as a “fallback” system. If the browser does not support the first font, it tries the next font, and so on.

font-family属性准备了一些字体作为“后备”系统。如果浏览器不支持前面的字体,它就尝试下一个,以此类推。

Start with the font you want, and end with a generic family, to let the browser pick a similar font in the generic family, if no other fonts are available.

用你想要的字体开始,用一个generic family(普通的字体家族)结束,如果没有其它字体可用,让浏览器从generic family选择一个相似的字体。

Note: If the name of a font family is more than one word, it must be in quotation marks, like: “Times New Roman”.


More than one font family is specified in a comma-separated list:

Example

1
2
3
p {
font-family: "Times New Roman", Times, serif;
}

For commonly used font combinations, look at our Web Safe Font Combinations.

Font Style

字体样式

The font-style property is mostly used to specify italic text.

字体样式属性更多用来指定italic(斜体)文本。

This property has three values:
normal - The text is shown normally
italic - The text is shown in italics
oblique - The text is “leaning” (oblique is very similar to italic, but less supported) 这个属性和italic有些像,不过缺乏一些浏览器的支持

1
2
3
4
5
6
7
8
9
10
11
12
Example
p.normal {
font-style: normal;
}

p.italic {
font-style: italic;
}

p.oblique {
font-style: oblique;
}

Font Size

字体大小

The font-size property sets the size of the text.

font-size属性设置文本的大小。

Being able to manage the text size is important in web design. However, you should not use font size adjustments to make paragraphs look like headings, or headings look like paragraphs.

在web设计中能够管理文本的大小是很重要的。然而,你不应该使用这个属性去修改标题,让它像段落,反之亦然。

Always use the proper HTML tags, like<h1> - <h6>for headings and <p> for paragraphs.

能使用HTML标签的地方,尽量去使用,例如<h1> - <h6>,去用作标题,和<p>用作段落。

The font-size value can be an absolute, or relative size.

字体大小可以是一个绝对大小,或者是一个相对大小。

Absolute size:
Sets the text to a specified size
Does not allow a user to change the text size in all browsers (bad for accessibility reasons)
Absolute size is useful when the physical size of the output is known


Relative size:
Sets the size relative to surrounding elements
Allows a user to change the text size in browsers
Note: If you do not specify a font size, the default size for normal text, like paragraphs, is 16px (16px=1em).

Set Font Size With Pixels

用pixels来设置字体大小

Setting the text size with pixels gives you full control over the text size:

1
2
3
4
5
6
7
8
9
10
11
12
Example
h1 {
font-size: 40px;
}

h2 {
font-size: 30px;
}

p {
font-size: 14px;
}

Tip: If you use pixels, you can still use the zoom tool to resize the entire page.

Set Font Size With Em

用Em设置字体大小

To allow users to resize the text (in the browser menu), many developers use em instead of pixels.


The em size unit is recommended by the W3C.

em字体单位被W3C所推荐。

1em is equal to the current font size. The default text size in browsers is 16px. So, the default size of 1em is 16px.

1em相当于当前的字体大小,例如当前浏览器的字体大小是16px,那么1em的默认大小就是16px。

The size can be calculated from pixels to em using this formula: pixels/16=em

pixel和em的换算公式:pixels/16=em

1
2
3
4
5
6
7
8
9
10
11
12
Example
h1 {
font-size: 2.5em; /* 40px/16=2.5em */
}

h2 {
font-size: 1.875em; /* 30px/16=1.875em */
}

p {
font-size: 0.875em; /* 14px/16=0.875em */
}

In the example above, the text size in em is the same as the previous example in pixels. However, with the em size, it is possible to adjust the text size in all browsers.
Unfortunately, there is still a problem with older versions of IE. The text becomes larger than it should when made larger, and smaller than it should when made smaller.

Use a Combination of Percent and Em

组合使用百分比和Em

The solution that works in all browsers, is to set a default font-size in percent for the element:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Example
body {
font-size: 100%;
}

h1 {
font-size: 2.5em;
}

h2 {
font-size: 1.875em;
}

p {
font-size: 0.875em;
}

Our code now works great! It shows the same text size in all browsers, and allows all browsers to zoom or resize the text!

Font Weight

字体权重(姑且把weight翻译成权重)

The font-weight property specifies the weight of a font:

1
2
3
4
5
6
7
8
Example
p.normal {
font-weight: normal;
}

p.thick {
font-weight: bold;
}

Responsive Font Size

响应式字体大小

The text size can be set with a vw unit, which means the “viewport width”.

使用vm单位当做字体大小的单位,vm意味着“viewport width”,视点宽度。

That way the text size will follow the size of the browser window:
Hello World
Resize the browser window to see how the font size scales.

1
2
Example
<h1 style="font-size:10vw">Hello World</h1>

Viewport is the browser window size. 1vw = 1% of viewport width. If the viewport is 50cm wide, 1vw is 0.5cm.

Viewport是浏览器窗体大小,1vw=1%的viewport的宽度,如果viewport是50cm宽,那么1vw是0.5cm宽。

Font Variant

字体变种

The font-variant property specifies whether or not a text should be displayed in a small-caps font.

font-variant这个属性指定了一个文本是否会用small-caps字体所显示。

In a small-caps font, all lowercase letters are converted to uppercase letters. However, the converted uppercase letters appears in a smaller font size than the original uppercase letters in the text.

在small-caps字体中,所有小写字母会被转换成大写字母。然后,转换后的大写字母用一种比正常的大写字母小的字体来显示。这个最好要实践一下才能理解。

1
2
3
4
5
6
7
8
Example
p.normal {
font-variant: normal;
}

p.small {
font-variant: small-caps;
}

总结

研究了font,搞明白了一直没有搞明白的这些关系。现在感觉学的都有些碎片化,欠缺的是如何去设计CSS呢?要找一篇对于这个的介绍来学习学习。

参考

https://www.w3schools.com/css/css_font.asp