diff --git a/files/en-us/web/css/@font-face/font-weight/index.md b/files/en-us/web/css/@font-face/font-weight/index.md index b9f0c43481ecd7b..407ede97fab6d40 100644 --- a/files/en-us/web/css/@font-face/font-weight/index.md +++ b/files/en-us/web/css/@font-face/font-weight/index.md @@ -7,11 +7,11 @@ browser-compat: css.at-rules.font-face.font-weight {{CSSRef}} -The **`font-weight`** CSS descriptor allows authors to specify font weights for the fonts specified in the {{cssxref("@font-face")}} at-rule. The {{cssxref("font-weight")}} property can separately be used to set how thick or thin characters in text should be displayed. +The **`font-weight`** CSS descriptor enables authors to specify a single font weight, or a range of font weights, for the font specified in a {{cssxref("@font-face")}} at-rule. This is then used by the browser to select the appropriate font when a CSS rule sets the desired weight of a font. -For a particular font family, authors can download various font faces which correspond to the different styles of the same font family, and then use the `font-weight` descriptor to explicitly specify the font face's weights. The values for the CSS descriptor is same as that of its corresponding font property. +Unless it contains a [variable font](/en-US/docs/Web/CSS/CSS_fonts/Variable_fonts_guide), a single font file contains characters from a font family in a specific weight and style: for example, "Helvetica bold italic". Typically, a developer will want to use fonts from a single font family in a range of different weights. To accomplish this, you can define multiple {{cssxref("@font-face")}} at-rules for the same family, one for each weight, and set the `font-weight` descriptor to define the weight range for which that particular font file will be used. When CSS rules set a font weight by setting the {{cssxref("font-weight")}} property or the {{cssxref("font")}} shorthand property, the appropriate font will then be used. -There are generally limited weights available for a particular font family. When a specified weight doesn't exist, a nearby weight is used. Fonts lacking bold typeface are often synthesized by the user agent. To prevent this, use the {{cssxref('font-synthesis')}} shorthand property. +If a font file contains a variable font, it may support a range of font weights. To reflect this, the `font-weight` descriptor can specify the range of font weights for which the font should be used as a space-separated pair of font-weight values. ## Syntax @@ -21,15 +21,21 @@ font-weight: normal; font-weight: bold; font-weight: 400; -/* Multiple Values */ +/* Defining a range */ font-weight: normal bold; font-weight: 300 500; ``` -The `font-weight` property is described using any one of the values listed below. - ### Values +The syntax of the `font-weight` descriptor takes one of the following forms: + +- The keyword `auto`. +- A single `` value. +- A pair of `` values, separated by a space. + +Each `` may be any one of the following: + - `normal` - : Normal font weight. Same as `400`. - `bold` @@ -37,10 +43,6 @@ The `font-weight` property is described using any one of the values listed below - `` - : A {{cssxref("<number>")}} value between 1 and 1000, inclusive. Higher numbers represent weights that are bolder than (or as bold as) lower numbers. Certain commonly used values correspond to common weight names, as described in the [Common weight name mapping](#common_weight_name_mapping) section below. -In earlier versions of the `font-weight` specification, the property accepts only keyword values and the numeric values 100, 200, 300, 400, 500, 600, 700, 800, and 900; non-variable fonts can only really make use of these set values, although fine-grained values (e.g. 451) will be translated to one of these values for non-variable fonts. - -CSS Fonts Level 4 extends the syntax to accept any number between 1 and 1000, inclusive, and introduces [Variable fonts](#variable_fonts), which can make use of this much finer-grained range of font weights. - ### Common weight name mapping The numerical values `100` to `900` roughly correspond to the following common weight names: @@ -80,20 +82,115 @@ People experiencing low vision conditions may have difficulty reading text set w ## Examples -### Setting normal font weight in a @font-face rule +### Selecting normal and bold fonts + +In this example we include two fonts, one normal weight, one bold weight, from the ["Fira Sans"](https://fonts.google.com/specimen/Fira+Sans) font family using two `@font-face` at rules. We set `font-weight` descriptors to match the weight of the fonts. -The following finds a local Open Sans font or imports it, and allows using the font for normal font weights. +After this, CSS rules can select the normal or the bold font for the "Fira Sans" family just by setting the {{cssxref("font-weight")}} property. Note that the {{htmlelement("strong")}} HTML element also selects the bold font, because by default `` elements use a bold font. + +#### HTML + +```html +

Fira Sans, normal weight paragraph

+

Fira Sans, bold weight paragraph

+

Fira Sans, default weight <strong> element

+``` + +#### CSS ```css @font-face { - font-family: "Open Sans"; - src: - local("Open Sans") format("woff2"), - url("/fonts/OpenSans-Regular-webfont.woff") format("woff"); + font-family: "Fira Sans"; + font-weight: normal; + src: url("https://mdn.github.io/shared-assets/fonts/FiraSans-Regular.woff2"); +} + +@font-face { + font-family: "Fira Sans"; + font-weight: bold; + src: url("https://mdn.github.io/shared-assets/fonts/FiraSans-Bold.woff2"); +} + +body { + font-family: "Fira Sans", serif; + font-size: 2rem; +} + +p.one { + font-weight: normal; +} + +p.two { + font-weight: bold; +} +``` + +#### Result + +{{embedlivesample("Selecting normal and bold fonts", "", 300)}} + +### Setting a range for a variable font + +In this example we include a variable font, ["League Mono"](https://www.theleagueofmoveabletype.com/league-mono), using a single `@font-face` at-rule. We've used a `font-weight: 300 700` value to effectively limit the range of weights that are available. If a CSS rule uses our "League Mono" font, then if it specifies a weight outside this range the weight it gets is clamped to the range. + +To show the effect of this we've included another font using "League Mono" that does not set the `font-weight` descriptor, and we've called this "League Mono-complete". + +#### HTML + +```html +

League Mono-complete, font-weight 200

+

League Mono, font-weight 400

+

League Mono, font-weight 600

+

League Mono-complete, font-weight 800

+``` + +#### CSS + +```css +@font-face { + font-family: "League Mono"; + src: url("https://mdn.github.io/shared-assets/fonts/LeagueMono-VF.ttf"); + font-weight: 300 700; +} + +@font-face { + font-family: "FireSans"; + src: url("https://mdn.github.io/shared-assets/fonts/FiraSans-Regular.woff2"); +} + +p { + font-family: "League Mono", "FireSans", serif; + font-size: 1.5rem; +} + +p.one { + font-weight: 200; +} + +p.two { font-weight: 400; } + +p.three { + font-weight: 600; +} + +p.four { + font-weight: 800; +} ``` +#### Result + +The result of this is: + +- Setting the `font-weight` property to `200` gets a `normal` version of FireSans. +- Setting the `font-weight` property to `400` gets "League Mono" at a weight of 400. +- Setting the `font-weight` property to `600` gets "League Mono" at a weight of 600. +- Setting the `font-weight` property to `800` gets a boldened version of "FireSans". + +{{embedlivesample("Setting a range for a variable font", "", "400")}} + ## Specifications {{Specifications}}