WebD2: Applying Typography in CSS (2024)

UNIT 3 > MODULE 3

Overview

In this lesson you will learn some of the specific CSS properties that are used to define typography on web pages, and will apply these properties to your portfolio website. You will also learn some strategies for selecting a font family.

Learner Outcomes

At the completion of this exercise:

  • you will be able to identify whether a font is serif, sans-serif, or another font family.
  • you will be able to make informed decisions about which font families to use on a website, based in part on the likelihood that these fonts will be installed on users' computers.
  • you will be able to define the typography of your site by setting the font-family, font-size, font-weight, line-height, text-align, and letter-spacing properties in CSS.

Font Families: Serif, Sans-serif, and others

In CSS (and in typography in general) there are five basic types, or families, of fonts: serif, sans serif, cursive, fantasy, and monospace.

Serif fonts have small lines or strokes that extend from the ends of characters. They can look like small feet, caps, tails, flags or dots. Serif fonts have been used for centuries in printed books, magazines and newspapers.

Sans-serif fonts do not have serifs ("sans" is French for "without"). These fonts are simple and straightforward.

There has been extensive research on which of these font families, serif or sans-serif, is easier to read. Unfortunately, results are inconclusive. There are dozens of studies favoring both font families. The bottom line: There are many variables affecting readability of text, not just font family alone. For the body of a web page, it's important to choose a font that is not too cluttered and that flows gracefully from letter to letter without too much space between letters. Generally the best choice for attaining this uncluttered, flowing, easy-to-read look is sans-serif. However, it's possible to attain this look with certain serif fonts as well.

For page titles, headings and sub-headings, a serif font is sometimes a better choice, because they can be perceived as more stately and grand, which helps to contribute to how readers perceive the hierarchy of the page.

What about cursive, fantasy, and monospace?

These fonts are more difficult to read and should be used sparingly. Depending on the message or feeling you're trying to communicate, they might be suitable for short sections of text, such as headings or subheadings.

Common Typographic Properties in CSS

Typography on the Web involves the interplay between various properties in CSS. Here are a few of the most common:

font-family

This property specifies the font of an element. The value of font-family is a list of preferred fonts, separated by a comma, as in these example:

body {font-family: Verdana, Arial, sans-serif;}h1 {font-family: "Times New Roman", Times, serif;}

If the user has the first font in the list installed on their computer, their browser will display that font. If they don't, the browser will try to display the next font in the list. The last font in the list should always be one of the five generic font families, described above. Again, they are:

  • serif
  • sans-serif
  • cursive
  • fantasy
  • monospace

This is the fallback font, to be used if none of the preferred fonts are available.

Note that in the above example, "Times New Roman" appears in quotation marks, whereas the other fonts don't. Anytime a font's name is more than one word, it must be in quotation marks.

font-size

This property specifies the size of the font. This can be expressed in relative units like % or em, or in absolute units like px (see the earlier lesson Anatomy of a Style if you need a refresher on these units).

Font size can also be expressed using terms like small, medium, large, larger, x-large, and xx-large.

font-style

This property specifies the style of a font, either normal, italic, or oblique.

font-weight

This property specifies the weight, or thickness, of characters. It can be normal, lighter, bold, or bolder. It can also be expressed as a numeric value between 100 and 900 (numbers must be divisible by 100).

line-height

This property specifies the height of a line of text. This is typically expressed either as a % or em and must be greater than the height of the font or else the lines of text will be squished together.

For maximum readability, the W3C's Web Content Accessibility Guidelines 2.0 calls for the line-height of blocks of text to be at least 1.5em or 150%

text-align

This property specifies how text is aligned horizontally. This can be either left, right, or center. It could also be justify, which aligns text on both the left and right sides of the page like a printed publication. However, this tends to result in awkward spaces between words and should be avoided on the web.

letter-spacing

This property enables you to specify an amount of space between letters. It can be expressed using the same units as other properties, such as font-size. It is sometimes used to give a distinctive look to certain elements such as headings.

text-shadow

This property is new in CSS3. Consequently it isn't supported by all browsers, but if you have a relatively current browser, this property can attach a shadow to text. If done well and used sparingly, this can be have a nice effect for content such as headings. For more information, consult the CSS3 Text-shadow Property page at W3Schools.com.

Activities

  1. Study each of the following websites for some possible font-family combinations:
  2. Select two fonts that you think would look nice for the body of your portfolio website. Since this will affect most of the text on your website, be sure to select fonts that you think will be easy to read. Also, be sure the two fonts are in the same family (for example, two sans-serif fonts or two serif fonts). Choose fonts that are similar to each other. Keep in mind who your audience is (who might ultimately read your portfolio?) and choose fonts that reflect the message and style you want to communicate to that audience. After selecting the fonts you like, search for your chosen fonts on CSS Font Stack. CSS Font Stack estimates the percentage of Windows and Mac computers that have each font installed. Be sure the combination of your two fonts has both Mac and Windows users covered. For example, if one of your font choices is common on Mac but not common on Windows, be sure your second font choice is common on Windows.
  3. Now repeat the above process, this time selecting two fonts that you think would look nice for the headings on your portfolio website.
  4. For your third font in each category, write the generic font family name, either serif or sans-serif.
  5. Next, open your web portfolio's external style sheet in your text editor, and its home page in a browser.
  6. Find the style definition for the body tag. Look at the properties that are currently used to define the body style. Add a font-family property, or if there's already one there, modify it by adding the fonts that you listed in your table. List them in order, separated by a comma. If any font name is more than one word, remember to enclose it in quotation marks. For example, assume you selected Century Gothic as your preferred font, Verdana as your browser-safe font, and sans-serif as your generic font family. Your font-family property would then look like this:

    font-family: "Century Gothic",Verdana,sans-serif;

  7. Save the file and refresh your browser to see what effect the change had on your home page.
  8. Now add a font-family style for h1, h2, and h3 headings. Note: When the same style applies to multiple elements, you can define that using one style definition, as in the following example:
     h1,h2,h3 { font-family: Rockwell,"Times New Roman",serif; }
  9. Save the file and refresh your browser to see what effect the change had on your website's headings.
  10. Now experiment with the other properties listed at the top of this page. Apply them one-at-a-time to various elements and see what effect they have on the page. Try to use these styles to enhance your site's readability. Try to stylize your site using typography and color so that it uses contrast, size, hierarchy, and space effectively. Keep the styles that work, and delete the ones that don't.

Handouts/Online Documents

All done?

Show your instructor your results before continuing on to the next module.

WebD2: Applying Typography in CSS (2024)

FAQs

How to apply a font to everything in CSS? ›

Yes, you can change the font of the whole HTML page by setting the font family property on the root element of your HTML document, which is the html tag. This CSS rule sets the font of all elements in the HTML document to Arial.

How to use typography in CSS? ›

#font-style
  1. default font-style: normal; The text is not slanted. Hello world.
  2. font-style: italic; Use the italic version of the font: the letters are slightly slanted. Hello world.
  3. font-style: oblique; Use the oblique version of the font: the letters are more slanted than italic. Hello world.

Which CSS property allows you to specify font typefaces _____? ›

the "font" css property is a shorthand for several font-related properties. The one you want is "font-family" which only allows you to specify the typeface you want to use.

How many types of font families are there? ›

Font Families: Serif, Sans-serif, and others

In CSS (and in typography in general) there are five basic types, or families, of fonts: serif, sans serif, cursive, fantasy, and monospace.

How do I change the font of all text in CSS? ›

Using the font-family property

This approach utilizes the font-family property to change the font style. Use the element selector to select the body from the HTML page. The built-in CSS property font family can be utilized to apply a font family to the whole document.

What is the rule for fonts in CSS? ›

With the @font-face rule, web designers do not have to use one of the "web-safe" fonts anymore. In the @font-face rule you must first define a name for the font (e.g. myFirstFont), and then point to the font file.

How do you use typography effectively? ›

10 Quick Tips For Great Typography
  1. Justify Left. In Western culture, people read top to bottom, left to right, so justify your text left. ...
  2. Use One Font. ...
  3. Skip a Weight. ...
  4. Double Point Size. ...
  5. Align to One Axis. ...
  6. Pick Any Font. ...
  7. ‍Group by Using Rules. ...
  8. ‍Avoid the Corners.

How can I style text in CSS? ›

CSS provides four common properties to alter the visual weight/emphasis of text:
  1. font-style : Used to turn italic text on or off. ...
  2. font-weight : Sets how bold the text is. ...
  3. text-transform : Allows you to set your font to be transformed.
Mar 27, 2024

Why is typography important in CSS? ›

Typography simply is a technique in user interface design to create readable, appealing, attractive, and easy-to-eye text for users to read. Typography plays an integral role in any website's design. Good typography is soothing to the eyes and creates a long-lasting impression on the minds of the user.

Can I use any font in CSS? ›

The CSS @font-face Rule

Web fonts allow Web designers to use fonts that are not installed on the user's computer. When you have found/bought the font you wish to use, just include the font file on your web server, and it will be automatically downloaded to the user when needed.

Which snippet of CSS is commonly used to center an element horizontally? ›

Margin: auto 0 snippet of CSS is commonly used to center a website horizontally. Explanation: Auto is the keyword which basically tells browser to set the left and the right margin by some margin.

Which CSS font style property is mostly used to specify text in italic? ›

The font-style property is mostly used to specify italic text. This property has three values: normal - The text is shown normally. italic - The text is shown in italics.

How to use fancy fonts in CSS? ›

The CSS font-family Property

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). The font names should be separated with a comma.

What is a loop in typography? ›

Loop/Lobe. In a double-storey 'g', the loop is the enclosed or partially enclosed counter below the baseline that is connected to the bowl by a link. The enclosed or partially enclosed extenders on cursive 'p', 'b', 'l', and similar letters are also called loops.

How to specify font-weight in CSS? ›

Syntax. /* Single values */ font-weight: normal; font-weight: bold; font-weight: 400; /* Multiple Values */ font-weight: normal bold; font-weight: 300 500; The font-weight property is described using any one of the values listed below.

How to change font for entire page in HTML? ›

To change font type purely with HTML, use the CSS font-family property. Set it to the value you want and place it inside a style attribute.

How do I add a font to CSS? ›

The @font-face CSS rule explained below is the most common approach for adding custom fonts to a website.
  1. Step 1: Download the font. ...
  2. Step 2: Create a WebFont Kit for cross-browsing. ...
  3. Step 3: Upload the font files to your website. ...
  4. Step 4: Update and upload your CSS file. ...
  5. Step 5: Use the custom font in your CSS declarations.
Jan 9, 2023

How do I add multiple font styles in CSS? ›

To specify multiple font families in CSS, you use the font-family property and list the font families separated by commas. This way, if the browser cannot display the first font, it will try the next one in the list, and so on.

How to set default font in CSS? ›

You can change the default font for all pages using the below steps:
  1. Click "Insert" and select "CSS Editor" from the dropdown menu.
  2. Locate the Body {} class at the top of the list.
  3. Locate the font family attribute and font size as highlighted below. body { background-color:#FFF; ...
  4. Make the change and click OK.

Top Articles
Latest Posts
Article information

Author: Pres. Carey Rath

Last Updated:

Views: 6210

Rating: 4 / 5 (61 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Pres. Carey Rath

Birthday: 1997-03-06

Address: 14955 Ledner Trail, East Rodrickfort, NE 85127-8369

Phone: +18682428114917

Job: National Technology Representative

Hobby: Sand art, Drama, Web surfing, Cycling, Brazilian jiu-jitsu, Leather crafting, Creative writing

Introduction: My name is Pres. Carey Rath, I am a faithful, funny, vast, joyous, lively, brave, glamorous person who loves writing and wants to share my knowledge and understanding with you.