Detailed explanation of css3 font variation font-variation

小云云
Release: 2018-02-10 15:37:16
Original
4436 people have browsed it

If you want to use one word to describe it, you can call it Font variants. The corresponding CSS attribute is the font-variation-* attribute, which includes the connection ( ligatures, caps, numerals, and alternate glyphs. These properties allow us to create more precise and beautiful typography (text) effects on the Web.

In fact, typography on the Web will always lag behind its effectiveness in print. But this is understandable because the fonts printed on the page have evolved over the centuries to a level of sophistication. It is still difficult to reach such a level in a browser.

However, due to the increase in OpenType functionality in Web fonts and the improvement of CSS features and capabilities, the gap between typesetting on the Web and typesetting in print is gradually narrowing.

So in today’s article, with the help of the previous example, let’s take a look at how to use CSS to control OpenType features, but please remember that any web fonts you use need to support these features.

font-variant-*Attributes

Most OpenType features can be controlled through the font-variant-* attribute in CSS. You can also use font-feature-settings to support lower version browsers. However, whenever possible, you should use the more modern font-variant-* properties. In actual use, you can use the @supports rule to downgrade font-variant-*. Use this attribute for supported browsers and # for unsupported browsers. ##font-feature-settings.

body {    font-feature-settings: "liga" 1;}@supports (font-variant-ligatures: common-ligatures) {    body {        font-feature-settings: normal;        font-variant-ligatures: common-ligatures;    }}
Copy after login

Maybe until now, you, like me, still don’t quite understand what the above code does. Don't worry, keep reading and you'll figure it out.

font-variant-*Mainly include:

  • font-variant-ligatures

  • font-variant-caps

  • font-variant-numeric

  • font-variant-alternates

  • font-variant-east-asian

Back Let’s briefly look at these properties.

font-variant-ligatures

A link is a single glyph composed of two or more glyphs. They often prevent unsightly or awkward letter collisions. The main function is

to help identify .

font-variant-ligaturesYou can use the following keywords as its attribute values:

/* Keyword values */
font-variant-ligatures: normal;
font-variant-ligatures: none;
font-variant-ligatures: common-ligatures;           /* <common-lig-values> */
font-variant-ligatures: no-common-ligatures;        /* <common-lig-values> */
font-variant-ligatures: discretionary-ligatures;    /* <discretionary-lig-values> */
font-variant-ligatures: no-discretionary-ligatures; /* <discretionary-lig-values> */
font-variant-ligatures: historical-ligatures;       /* <historical-lig-values> */
font-variant-ligatures: no-historical-ligatures;    /* <historical-lig-values> */
font-variant-ligatures: contextual;                 /* <contextual-alt-values> */
font-variant-ligatures: no-contextual;              /* <contextual-alt-values> */
font-variant-ligatures: contextual;                 /* <no-historical-ligatures> <common-ligatures> */

/* Global values */
font-variant-ligatures: inherit;
font-variant-ligatures: initial;
font-variant-ligatures: unset;
Copy after login

common-ligatures

These connections are the ones the designer decided to use under normal circumstances. In most cases, these should be used as most browsers enable them by default.

The most common connection values ​​are

fi, ffi, th, or similar. Their corresponding OpenType values ​​are liga and clig. Both values ​​are possible:

  • common-ligatures: Activate connection

  • no- common-ligatures: Disable connection

For example:

font-variant-ligatures: common-ligatures; /* 开启 */font-variant-ligatures: no-common-ligatures; /* 禁用 */font-feature-settings: &#39;liga&#39; 1; /* 低版本浏览器 开启 */font-feature-settings: &#39;liga&#39; 0; /* 低版本浏览器 禁用 */
Copy after login

The effect is as follows:

The previous line enables the connection and the next line disables it Connection effects

discretionary-ligatures

These can be used for printing connections to achieve special effects. These are disabled by default. It can also be understood that these values ​​can be used to control specific connections, specific fonts and defined by the OpenType designer. They correspond to the OpenType value

dig. It also has two possible values:

  • discretionary-ligatures: activate connection

  • no-discretionary -ligatures: Disable connection

If set in your code:

font-variant-ligatures: discretionary-ligatures; /* 激活 */font-variant-ligatures: no-discretionary-ligatures; /* 禁用 */font-feature-settings: &#39;dlig&#39; 1; /* 低版本浏览器 激活 */font-feature-settings: &#39;dlig&#39; 0; /* 低版本浏览器 禁用 */
Copy after login

The effect is as follows:

First line Enables the connection, the second line disables the connection

contextual

These values ​​control whether letters adapt to their context. That is, whether they fit in with the letters around them. These values ​​correspond to

calt in OpenType. Similarly contextual means enabled; no-contextual means disabled.

font-variant-ligatures: contextual; /* enable */font-variant-ligatures: no-contextual; /* disable */font-feature-settings: &#39;calt&#39; 1; /* low-level enable */font-feature-settings: &#39;calt&#39; 0; /* low-level disable */historical-ligatures
Copy after login

Using these values, it looks like

tz in German appears as ß. Its corresponding OpenType value is hlig. The historical-ligatures ring is enabled, while the no-historical-ligatures is disabled.

font-variant-ligatures: historical-ligatures; /* enable */font-variant-ligatures: no-historical-ligatures; /* disable */font-feature-settings: &#39;hlig&#39; 1; /* low-level enable */font-feature-settings: &#39;hlig&#39; 0; /* low-level disable */
Copy after login

font-variant-position

We often use the

sub and sup elements to change the characters in vertical position. By default, the browser will use a regular numeric character to make font-size smaller, and use vertical-align to raise or lower it. These are not true sub and sup, and often look ugly, not to mention they mess up the line-height.

Thankfully, there is now a way to actually achieve effects like

sub and sup using font-variant-position. However, this property is currently only supported in Firefox.

/* Keyword values */font-variant-position: normal;
font-variant-position: sub;
font-variant-position: super;/* Global values */font-variant-position: inherit;
font-variant-position: initial;
font-variant-position: unset;
Copy after login

sub

实现类似于<sub>标签的下标字符效果:

font-variant-position: sub; /* enable */font-variant-position: normal; /* disable both variants */font-feature-settings: &#39;subs&#39; 1; /* low-level enable */font-feature-settings: &#39;subs&#39; 0; /* low-level disable */
Copy after login

super

实现类似于<sup>标签的上标字符效果:

font-variant-position: super; /* enable */font-variant-position: normal; /* disable both variants */font-feature-settings: &#39;sups&#39; 1; /* low-level enable */font-feature-settings: &#39;sups&#39; 0; /* low-level disable */
Copy after login

font-variant-caps

font-variant-caps属性用来控制字母大写。只不过这里启用的是小型大写。

/* Keyword values */font-variant-caps: normal;
font-variant-caps: small-caps;
font-variant-caps: all-small-caps;
font-variant-caps: petite-caps;
font-variant-caps: all-petite-caps;
font-variant-caps: unicase;
font-variant-caps: titling-caps;/* Global values */font-variant-caps: inherit;
font-variant-caps: initial;
font-variant-caps: unset;
Copy after login

small-caps

小型大写字母的设计与小写字母相同,用于在运行文本中大写字母。它们使段落更具内聚力和可读性。

font-variant-caps: small-caps; /* enable */font-variant-caps: normal; /* disable all variants */font-feature-settings: &#39;smcp&#39; 1; /* low-level enable */font-feature-settings: &#39;smcp&#39; 0; /* low-level disable */
Copy after login

all-small-caps

small-caps只是替换了小写字母为大写字母。要是想将所有字母都变成小的大写字母,需要使用all-small-caps

font-variant-caps: all-small-caps; /* enable */font-variant-caps: normal; /* disable all variants */font-feature-settings: &#39;smcp&#39; 1, &#39;c2sc&#39; 1; /* low-level enable */font-feature-settings: &#39;smcp&#39; 1, &#39;c2sc&#39; 0; /* low-level disable */
Copy after login

petite-caps

标准的小型大写字母通常会比字体的x高度略大一些。一些字体有额外的小型大写,与x高度匹配。这些被称为petite-caps

font-variant-caps: petite-caps; /* enable */font-variant-caps: normal; /* disable all variants */font-feature-settings: &#39;pcap&#39; 1; /* low-level enable */font-feature-settings: &#39;pcap&#39; 0; /* low-level disable */
Copy after login

all-petite-caps

all-small-caps类似,它将所有字母包括小写字母和大写字母,都转换成小型大写字母,而且和字体的x字母高度相匹配。

font-variant-caps: all-petite-caps; /* enable */font-variant-caps: normal; /* disable all variants */font-feature-settings: &#39;pcap&#39; 1, &#39;c2pc&#39; 1; /* low-level enable */font-feature-settings: &#39;pcap&#39; 1, &#39;c2pc&#39; 0; /* low-level disable */
Copy after login

unicase

该功能将大写和小写字母映射到混合的小写字母和小写的大型字母,创建一个单一case的字母表。有时这些小写字母是实际的小型大写字母,有时它们是特别设计的unicase形式。这个特性的实现从字体到字体各不相同。

font-variant-caps: unicase; /* enable */font-variant-caps: normal; /* disable all variants */font-feature-settings: &#39;unic&#39; 1; /* low-level enable */font-feature-settings: &#39;unic&#39; 0; /* low-level disable */
Copy after login

titling-caps

标准的大写字母设计是用来与小写字母一起使用的,当它们被用在所有大写字母的字符串时,它们会显得过于大一点。有些字体还特别适合这种情况。

font-variant-caps: titling-caps; /* enable */font-variant-caps: normal; /* disable all variants */font-feature-settings: &#39;titl&#39; 1; /* low-level enable */font-feature-settings: &#39;titl&#39; 0; /* low-level disable */
Copy after login

font-variant-numeric

font-variant-numeric属性用来控制对数字、分数和序号标记的处理。数字的正确显示取决于上下文的不同,以下是一些一般性的规则:

  • 运行于文本正文中的数字,使用比例的是旧式数字

  • 运行在标题中的数字,使用的比例是内联数字

  • 在数字表格中的数字,使用的比例是表格内联数字

常用的属性:

font-variant-numeric: normal;font-variant-numeric: ordinal;font-variant-numeric: slashed-zero;font-variant-numeric: lining-nums;         /* <numeric-figure-values> */font-variant-numeric: oldstyle-nums;       /* <numeric-figure-values> */font-variant-numeric: proportional-nums;   /* <numeric-spacing-values> */font-variant-numeric: tabular-nums;        /* <numeric-spacing-values> */font-variant-numeric: diagonal-fractions;  /* <numeric-fraction-values> */font-variant-numeric: stacked-fractions;   /* <numeric-fraction-values> */font-variant-numeric: oldstyle-nums stacked-fractions;/* Global values */font-variant-numeric: inherit;
font-variant-numeric: initial;
font-variant-numeric: unset;
Copy after login

lining-nums

lining-nums里的数字近似大写字母,高度一致。它们应该用于标题或表格中的数字。通常情况下,数字是默认的。

font-variant-numeric: lining-nums; /* enable */font-variant-numeric: normal; /* disable all variants */font-feature-settings: &#39;lnum&#39; 1; /* low-level enable */font-feature-settings: &#39;lnum&#39; 0; /* low-level disable */
Copy after login

oldstyle-nums

旧式数字(Old-style)有不同的高度和对齐方式,因此更类似于小写字母。它们应该用于正文文本中。

font-variant-numeric: oldstyle-nums; /* enable */font-variant-numeric: normal; /* disable all variants */font-feature-settings: &#39;onum&#39; 1; /* low-level enable */font-feature-settings: &#39;onum&#39; 0; /* low-level disable */
Copy after login

proportional-nums

比例(Proportional)数字具有可变的间距,并与水平文本相融合。它们应该在大多数情况下使用,除了数据表格中。其中垂直对齐很重要。通常数字在默认情况下是比例数字。

font-variant-numeric: proportional-nums; /* enable */font-variant-numeric: normal; /* disable all variants */font-feature-settings: &#39;pnum&#39; 1; /* low-level enable */font-feature-settings: &#39;pnum&#39; 0; /* low-level disable */
Copy after login

tabular-nums

表格数字具有相同的宽度,应该在数据表格中使用,以允许数字垂直对齐。

font-variant-numeric: tabular-nums; /* enable */font-variant-numeric: normal; /* disable all variants */font-feature-settings: &#39;tnum&#39; 1; /* low-level enable */font-feature-settings: &#39;tnum&#39; 0; /* low-level disable */
Copy after login

diagonal-fractions

默认情况下,分数将以小写字母的形式显示。适当的分数将被格式化,以匹配一个衬里图形的高度,可以是对象线或堆叠。

font-variant-numeric: diagonal-fractions; /* enable */font-variant-numeric: normal; /* disable all variants */font-feature-settings: &#39;frac&#39; 1; /* low-level enable */font-feature-settings: &#39;frac&#39; 0; /* low-level disable */
Copy after login

stacked-fractions

在大多数Web字体中,堆叠分数并不像对角线分数那样普遍,但它应该被证明是有用的。这在大量的科学或数学中用得多。

font-variant-numeric: stacked-fractions; /* enable */font-variant-numeric: normal; /* disable all variants */font-feature-settings: &#39;afrc&#39; 1; /* low-level enable */font-feature-settings: &#39;afrc&#39; 0; /* low-level disable */
Copy after login

ordinal

Ordinal像stndrdth这样的序号默认显示为标准小写字母。然而,理想情况下,这些数字会随着数字的增加而增加。序数值支持这一点。

font-variant-numeric: ordinal; /* enable */font-variant-numeric: normal; /* disable all variants */font-feature-settings: &#39;ordn&#39; 1; /* low-level enable */font-feature-settings: &#39;ordn&#39; 0; /* low-level disable */
Copy after login

slashed-zero

可以使用斜线来替换零字符。

font-variant-numeric: slashed-zero; /* enable */font-variant-numeric: normal; /* disable all variants */font-feature-settings: &#39;zero&#39; 1; /* low-level enable */font-feature-settings: &#39;zero&#39; 0; /* low-level disable */
Copy after login

font-variant-alternates

字体可以为任何字符提供多种替换。font-variant-alternates属性提供了许多控制字符替换的方法。

/* Keyword values */font-variant-alternates: normal;
font-variant-alternates: historical-forms;/* Functional notation values */font-variant-alternates: stylistic(user-defined-ident);
font-variant-alternates: styleset(user-defined-ident);font-variant-alternates: character-variant(user-defined-ident);
font-variant-alternates: swash(user-defined-ident);
font-variant-alternates: ornaments(user-defined-ident);
font-variant-alternates: annotation(user-defined-ident);
font-variant-alternates: swash(ident1) annotation(ident2);/* Global values */font-variant-alternates: initial;
font-variant-alternates: inherit;
font-variant-alternates: unset;
Copy after login

historical-forms

被运用于周期表。请注意这与连接中的效果还是有不同之处。

font-variant-numeric: historical-forms; /* enable */font-variant-numeric: normal; /* disable all variants */font-feature-settings: &#39;hist&#39; 1; /* low-level enable */font-feature-settings: &#39;hist&#39; 0; /* low-level disable */
Copy after login

除此之外,根据不同的场景选择不同的属性值。

font-variation-settings

font-variation-settings属性主要用于指定需要更改的特性,其主要有由个字母和它们的变化值组成。该属性提供了对OpenType或TrueType字体变体的在低浏览器的控制。

/* Use the default settings */font-variation-settings: normal;/* Set values for OpenType axis names */font-variation-settings: "XHGT" 0.7;/* Global values */font-variation-settings: inherit;
font-variation-settings: initial;
font-variation-settings: unset;
Copy after login

这就是在我们文章的开头的示例中看到的font-variation-settings: 'INLN' 0;font-variation-settings: 'INLN' 1000, 'SWRM' 1000;等。每个值都有4个ASCII字符和一个表示axis值的数字组成。如果<string>有更多或更少的字符或包含U+20至U+7E的codepoint范围之外的字符,那么整个属性都是无效的。而<number>可以是分数,也可以是负数。

总结

撸了一圈font-variation-*相关的属性,总算是了解了这个Demo的实现原理了。但要完全掌握这些知识,还是需要一定的时间的。必竟有很多知识点都和字体以及排版相关的。这需要具备一些印刷相关的知识,或许能帮助我们更好的掌握这些属性的特性,以及使用的场景。

另外,目前这些属性,浏览器能支持的为数不多,不过不要紧,感兴趣的可以玩玩,体验一下。或者自己把文章开头的示例修改一下下,说不定能达到另外的效果。

相关推荐:

CSS3 font-feature-settings特性减除字体动画震颤效果实例分享

javascript字体颜色控件的开发

CSS的文本字体颜色如何设置

The above is the detailed content of Detailed explanation of css3 font variation font-variation. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template