Lab exercises are designed to help you gain practical experience with web technologies. You should elaborate on these exercises to confirm you understand and deploy the learning you find here in your own code.
CSS fonts
This is a quick demonstration of how to work with fonts.
Let’s begin with a simple document, index.html.
We have a paragraph (block) with three <span>
elements (inline) containing text.
|
|
We can add a few styles to style.css to highlight the text.
|
|
The result is some large text with the <span>
elements clearly indicated.
Line height
The line-height property controls the space between the lines. The default value is usually 1.1.
|
|
It is advised to not use units for line-height as it can cause problems. Unitless values should be relative to the font size.
Reducing the line-height squeezes the text causing overlap.
A value of 1.0 should make the space between text baselines the same as the font size, leaving no space for descenders such as in p, g and q.
Expanding to a larger value is good for readability in normal paragraphs.
|
|
Letter and word spacing
We have some fine-grained control over the way a font is presented using letter-spacing to modify the spacing between letters and word-spacing to modify the space between words.
|
|
Notice the :nth-of-type selector pseudoclass allows us to select the second and third paragraphs individually to add the new styles.
Obviously we have taken this to the extreme and messing with these parameters is not always recommended, however it can be useful for things like titles.
Changing fonts
Swapping fonts is easy enough using the font-family property.
Here we use the (usually much better) system-ui font.
|
|
The system-ui font is always available and can appear different depending on the platform.
Notice that by swapping the font we have actually changed the size of the text and the content has shifted position slightly.
Not all fonts are available on all platforms. Custom fonts can be added by linking to the necessary files.
Fonts come in all sorts of formats,
Google fonts
An easy way to get nice fonts quickly is to use google fonts. Simply visit the site, select a font and choose which variants to include.
Once you have selected what you want in your site, you should see options in the side panel to use on the web.
Find the @import formatted output and copy it.
You can paste it into your document directly.
|
|
Alternatively, to keep things simple, we can simply add the @import statement to our CSS file.
|
|
In this case, we’ve added a few different weights (thin, light and normal) of the Roboto font and used them in the different paragraphs.
Notice we also limited the paragraph width to 70ch (70 characters) to improve readability on larger device widths. We also centered the paragraphs by setting margin-inline to auto.
@font-face
Looking through google fonts provides a lot of options but it doesn’t do anything special, the google font system simply links out to files served on the google servers.
If you want to use a different font then @font-face can be used to specify custom font files (e.g. otf, woff etc.).
You can see how to do so by visiting the url in our @import statement. This code can be included directly in a CSS file without using google fonts.
To host your own fonts, download them into your project (e.g. in a fonts folder) and use a custom @font-face rule to define when the files should be used.
More stuff about text
There’s plenty more to learn about formatting text.
Research the text-decoration, text-transform and text-shadow properties.
Also learn about breaking text and the white-space property.
See this article for more details of how fonts and line-height work. In truth, it’s complicated.