Lably
Skip to content

Font family (HTML)

CSS property in HTML

  • 4 min read
  • 3 chapters

Related on Lably

Through HTML, CSS and Font

The CSS term font family is matched with the typographical term typeface, which is a grouping of fonts defined by shared design styles. A font is a particular set of glyphs (character shapes), differentiated from other fonts in the same family by additional properties such as stroke weight, slant, relative width, etc. The CSS term font face is matched with "font"; it is decided by a combination of the font family and the additional properties.

In both HTML and CSS, the list is separated by commas. To avoid unexpected results, the last font family on the font list should be one of the generic families which are by default always available. In the absence of a font being found, the web browser will use its default font, which may be a user-defined one. Depending on the web browser, a user can in fact override the font defined by the code writer. This may be for personal taste reasons, but may also be because of some physical limitation of the user, such as the need for a larger font size or the avoidance of certain colors.

In addition to local fonts, modern web browsers support linking custom font files directly by using the @font-face declaration. Once included, such fonts can be listed in the font-family property, alongside all local and fallback fonts.

Examples

The following example defines the same font face (Times or a default serif, 14 points, italics) in three ways:

  • With CSS in a separate stylesheet.
  • With inline CSS applied to an element via the style attribute.
  • With the deprecated presentational element <font>...</font> and its non-standard attribute face. (<i>...</i> is not deprecated in general, but its presentational use is.)
<span id="mwLg"></span><span class="p" id="mwLw">.</span><span class="nc" id="mwMA">text</span><span class="w" id="mwMQ"> </span><span class="p" id="mwMg">{</span><span class="w" id="mwMw"> </span><span class="k" id="mwNA">font-family</span><span class="p" id="mwNQ">:</span><span class="w" id="mwNg"> </span><span class="n" id="mwNw">times</span><span class="p" id="mwOA">,</span><span class="w" id="mwOQ"> </span><span class="kc" id="mwOg">serif</span><span class="p" id="mwOw">;</span><span class="w" id="mwPA"> </span><span class="k" id="mwPQ">font-size</span><span class="p" id="mwPg">:</span><span class="mi" id="mwPw">14</span><span class="kt" id="mwQA">pt</span><span class="p" id="mwQQ">;</span><span class="w" id="mwQg"> </span><span class="k" id="mwQw">font-style</span><span class="p" id="mwRA">:</span><span class="kc" id="mwRQ">italic</span><span class="p" id="mwRg">;</span><span class="w" id="mwRw"> </span><span class="p" id="mwSA">}</span>
<span id="mwSw"></span><span class="p" id="mwTA"><</span><span class="nt" id="mwTQ">p</span> <span class="na" id="mwTg">class</span><span class="o" id="mwTw">=</span><span class="s" id="mwUA">"text"</span><span class="p" id="mwUQ">></span> Sample text formatted with CSS in a separate stylesheet. <span class="p" id="mwUg"></</span><span class="nt" id="mwUw">p</span><span class="p" id="mwVA">></span> <span class="p" id="mwVQ"><</span><span class="nt" id="mwVg">p</span> <span class="na" id="mwVw">style</span><span class="o" id="mwWA">=</span><span class="s" id="mwWQ">"font-family: times, serif; font-size:14pt; font-style:italic"</span><span class="p" id="mwWg">></span> Sample text formatted with inline CSS. <span class="p" id="mwWw"></</span><span class="nt" id="mwXA">p</span><span class="p" id="mwXQ">></span> <span class="p" id="mwXg"><</span><span class="nt" id="mwXw">p</span><span class="p" id="mwYA">><</span><span class="nt" id="mwYQ">i</span><span class="p" id="mwYg">><</span><span class="nt" id="mwYw">font</span> <span class="na" id="mwZA">face</span><span class="o" id="mwZQ">=</span><span class="s" id="mwZg">"times, serif"</span> <span class="na" id="mwZw">size</span><span class="o" id="mwaA">=</span><span class="s" id="mwaQ">"3"</span><span class="p" id="mwag">></span> Sample text formatted with the deprecated FONT tag. <span class="p" id="mwaw"></</span><span class="nt" id="mwbA">font</span><span class="p" id="mwbQ">></</span><span class="nt" id="mwbg">i</span><span class="p" id="mwbw">></</span><span class="nt" id="mwcA">p</span><span class="p" id="mwcQ">></span>

It is a best practice to put a space after the comma, and to put any name containing spaces between quotation marks.

Sign in to keep reading

2 more chapters are free with a Lably account.

More in Computing and communication