关于CSS:字体家族在哪里定义?

Where are font families defined?

当我在CSS中指定font-family属性时,可以指定特定的字体,例如" Times New Roman",也可以指定字体系列名称,例如" Times"。

在哪里定义" Times New Roman"是" Times"字体家族中的一种字体,或者它只是基于名称匹配?


按照w3c的规定

There are two types of font family names:

  • family-name - The name of a font-family, like"times","courier","arial", etc.

  • generic-family - The name of a generic-family, like"serif","sans-serif","cursive","fantasy","monospace".

是的,如果您想使用" Times"系列,则可以仅指定" Times",但是如果您想在系列中使用特定字体,则必须输入字体的完整名称(如您所说,与字体文件中包含的名称匹配。

编辑:
一个实际的例子是Times Family,其中包含多个字体。如果您未指定任何字体(例如font-family:" Times"),则将选择一个"默认"字体(我猜这是正常粗细的字体),否则,如果您设置font-family:" Times Semibold"字体将被使用。

编辑2:来源

  • The User Agent makes (or accesses) a database of relevant CSS 2.1 properties of all the fonts of which the UA is aware. If there are two fonts with exactly the same properties, the user agent selects one of them.
  • At a given element and for each character in that element, the UA assembles the font properties applicable to that element. Using the complete set of properties, the UA uses the 'font-family' property to choose a tentative font family. The remaining properties are tested against the family according to the matching criteria described with each property. If there are matches for all the remaining properties, then that is the matching font face for the given element or character.
  • If there is no matching font face within the 'font-family' being processed by step 2, and if there is a next alternative 'font-family' in the font set, then repeat step 2 with the next alternative 'font-family'.
  • If there is a matching font face, but it does not contain a glyph for the current character, and if there is a next alternative 'font-family' in the font sets, then repeat step 2 with the next alternative 'font-family'.
  • If there is no font within the family selected in 2, then use a UA-dependent default 'font-family' and repeat step 2, using the best match that can be obtained within the default font. If a particular character cannot be displayed using this font, then the UA may use other means to determine a suitable font for that character. The UA should map each character for which it has no suitable font to a visible symbol chosen by the UA, preferably a"missing character" glyph from one of the font faces available to the UA.