Fonts
The properties that allow you to control the appearance of text can be split into two groups:
- Those that directly affect the font and its appearance.
- Those that would have the same effect on text no matter what font you were using.
We can either use a font from the system or import an external one, from Google Fonts, for example, which will give you a script to add in the head of your html file.
Terminology
The most common terminology you'll use around fonts are serif, sans-serif and monospace fonts.
Serif fonts have extra details (serifs) on the ends of the main strokes of the letters. Known examples are Georgia and Times New Roman.
Sans-serif have straight ends to the letters, which translates into a cleaner design. Know examples are Arial and Helvetica.
Monospace has every letter with exactly the same width. Known examples are Courier and Courier New.
When combining them, you should attempt to mix 2, and no more than 2, font-families. One should be given to headings and the other to your other text blocks. Google Fonts is great at suggesting great pairings for a chosen font. Have a look!
Properties
We can change all sorts of different characteristics of our text elements.
font-family
Allows you to specify the font that should be used for any text inside of the chosen element:
The first defined font - Georgia - is applied to all of the elements in your body tag. If, for some reason, it fails, Times is its fallback, with serif being the C-plan. If a font name is made up of more than one word, it should be put in double quotes (eg. "Courier New").
font-size
Allows you to specify a size for the font, which can be set into all sorts of different values (units page to come):
font-weight
Allows you to display text as bold (can be written as a number or a selection of predefined words):
font-style
Allows you to display your text as italic, amongst other things:
text-transform
Allows you change the case(s) of each word to uppercase, lowercase, or capitalise (which causes the first letter of each word to appear capitalised), as well as a few other things:
text-decoration
Allows you to do a mix of things:
Pro-tip: use this one to remove the default behaviour that underlines links, if you don't want your links underlined!
line-height
Allows you to set the height of an entire line of text, increasing or decreasing the vertical gap between lines:
Note: If your line-height has the same height of your element, the text inside will be vertically centered.
letter-spacing vs. word-spacing
The former controls the space between each letter, while the latter the space between each word:
text-align vs. vertical align
While the latter vertically aligns your text, the former will align it horizontally:
Shall we have a go?
tl;dr
- First let's try switch up the font using one thats already built in. Try the below value:
If we put the rule onto the body element, it should change the font across the whole site. However, you should note that you should put this at the top of your css file. This way, if you want to override this for any other elements, like a heading for example, you won't be overruled.
-
Now let's try and change the size of something. How about a
heading
element? Find one, target it, and change the size to 30px. -
See if you can remove the underline from your links.
-
Pick some other rules and see if you can get them to work.
walkthrough
- Let's change the font of the body element - this should change the font for the whole site. We put this at the top of our CSS so we can change the fonts of other elements further down, and not be overruled. Try this:
- Let's try
font-size
. Find one of your heading elements. Anh2
or anh3
. Try changing the size:
- Remove the underline from your link. It is usually default behaviour on browsers to underline all links (anchor tags). If you don't want your links underlined, we can use
text-decoration
to do this. First we target all anchor tags, (or just the specific ones you want using a class or id), then add the property, like so:
- Try some more yourself :)
Remember - if you struggle with any of these, ask your tutor to go through them on the next class.