Essential CSS
JavaScript Roadmap
Learn JavaScript
Mastering Web Typography
Practical Astro
Understanding Async JS
Unorthodox Tailwind
API Masterclass
Build and Deploy
The Beginner Freelancer
Real World Node
Profile Billing Logout
Essential CSS
Lesson Plan
  1. Introduction
    1. Welcome to Essential CSS
    2. Getting started with the course
    3. What are file formats?
    4. Web standards
    5. What is HTML?
    6. What is CSS?
    7. Devtools, your indispensable best friend
  2. CSS Key Concepts
    1. Some background about CSS
    2. CSS Syntax and Terminology
    3. Selectors
    4. Cascade and Specificity
    5. Origin and Importance
    6. Inheritance
    7. How browsers handle CSS
  3. Basic Mental Model
    1. Everything on the web is a box
    2. The box model
    3. How big is your box?
    4. The relationship between px and em
    5. The `box-sizing` property
    6. The `display` property
    7. Formatting contexts
    8. Block boxes
    9. Inline boxes
    10. What is inline-block?
  4. Flexbox and Grid
    1. The parent-child relationship
    2. Sizing in Flexbox
    3. Flex directions
    4. Basic grid concepts
    5. Setting up a grid
    6. Placing grid items
    7. Box alignment
  5. Building web layouts
    1. Arranging your boxes on the web
    2. Positioning schemes
    3. Floating boxes
  6. Typography
    1. Making your text look good
    2. What is a font?
    3. Typesetting with CSS

Typesetting with CSS

4m:18s

Back in 1996, when CSS first became a W3C recommendation. There was a font property section defining font-family, font-style, font-weight, font-size and font-variant, plus font as a shorthand.

There have been more font-related properties added to the specification over the years but here we will cover the basic ones that can really make a difference in the readability of your text.

The font-family property

This property tells the browser which font to use for a given element, and as mentioned in the previous lesson, you can define this with the @font-face rule. It is highly recommended to put the family name in quotes to prevent errors when parsing the font name.

The @font-face rule is only needed for fonts that are not installed in the users computer. So you wanted to use a system font, like Arial or Times New Roman which almost every system has out-of-the-box, you can declare it directly without needing to set up the @font-face rule.

You can also specify a font stack, which is a list of comma-seperated font names. The order of these names matter as the one which comes first takes precedence. The browser will try to find the first font that can be loaded, and if it cannot find the glyph in the specified font file, it will continue down the list.

This is why sometimes you come across websites that have particular glyphs in a different font from the rest of the text.

There are also generic font families which leave it up to the browser to decide which font to use. These are serif, sans-serif, cursive, fantasy and monospace. These are often put at the very end of the font stack as the final fallback option.

font-style and font-weight

The font-style property selects between the normal (or upright), italic and oblique styles of a font. If an italic or oblique font is needed but not available, the browser will attempt to synthesize one by slanting the text. However, the end result of this is usually not very good and typographers tend to hate it when that happens.

Also, for East Asian scripts like Chinese. Japanese and Korean, there is no concept of italics. For those cases, it would be much better to set the font-style to normal instead of italic.

Similarly, the font-weight property selects between the different weights for a font. Aside from the numbered values from 100 to 900 in increments of 100, there are also keywords. normal is the equivalent of 400 and bold is the equivalent of 700.

Again, if the browser cannot find a font with the bold weight, it will attempt to synthesize the heavier weight as well, and the end result is no better than the synthesized italics. So ideally, it would be good to have the 3 commonly used styles of normal, italic and bold fonts accounted for so no synthesis needs to happen.

The font-size property

This property determines the height of the glyphs in a font. We talked about this a little in the lesson on The relationship between px and em. In addition to the different units discussed in How big is your box?, there are some lesser used keyword values for sizing your text.

The keyword values of xx-small, x-small, small, medium, large, x-large, xx-large and xxx-large are mapped to a table of font sizes computed by the browser. For the purposes of consistency across browsers, most people default to using CSS lengths instead.

The default font size for most browers is 16px. This value is related to 12pt, which was settled on in print as the default size based on the average distance a book was held from your face.

For the most part, you will probably be using px, em or rem to size your text.

The line-height property

Back in the lesson on Inline boxes, we mentioned that the height of a line box is determined by the text’s font-size and the line-height property. You can use this property to set the distance between lines of text.

Without explicitly setting a value for line-height, its value will default to normal, which is determined by font metrics. Font metrics are encoded in the font by the typeface designers, and are out of the developer’s control.

We can, however, override these default font metric line height values using the line-height property. You can use a unitless number, a CSS length or a percentage. Negative values are not allowed (and the browser will just ignore it).

p {
font-size: 18px;
line-height: 1.5;
}
p {
font-size: 18px;
line-height: 1.5em;
}
p {
font-size: 18px;
line-height: 150%;
}

The above 3 rules will give you the same effect. However, line-height is an inherited value. As such, for the unitless number value, if the font size of any descendent elements change, the line height will change as well.

For example, if there is a span element within the p element with its font-size set 1.2em, the computed value of the line-height for the unitless value will end up different from the rest.

First span resolves to 24.96px, while the other elements resolve to 20.8px