Web Development

Dr Derek Bridge
School of Computer Science & Information Technology
University College Cork

Lecture Objectives

  • learn about CSS text properties
  • learn about CSS font properties
  • learn how to use downloadable fonts

Warning

Fog of details ahead!

Do not 'learn off'.
Look-up when needed.

Text Formatting

Multicolumn Text

A large quantity of text might look better if the text is laid out in columns, like in a newspaper.

In CSS, on the container, specify column-width, e.g.:
column-width: 20em;
You get as many 20em columns as will fit.

Multicolumn Text Example

Hyphenation

What can we do if a word is too long for its column?

  • Either:
    • CSS: set hyphens: manual on the container;
    • HTML: put ­ inside the word at possible places where it can be hyphenated.
  • Or:
    • CSS: set hyphens: auto on the container;
    • HTML: make sure the container has (or inherits) a lang attribute.

Font Properties

There are various font properties, e.g.


p {
    font-family: "Times New Roman";
    font-size: 12px;
    font-style: italic;
    font-weight: bold;
}

Font Sizes

  • In CSS, we can specify the font-size property, e.g.:
    
    header {
        font-size: 2em;
    }
  • There are two types of values used for font sizes:
    • absolute font sizes; and
    • relative font sizes.

Absolute Font Sizes

These values do not depend on any others.

Pixels e.g. 16px
Points = (1/72th inch) e.g. 12pt

On the web, we might use px occasionally;
otherwise, avoid absolute font sizes.

Relative Font Sizes

These values depend on other values,
e.g. in some cases, the parent's value.

em e.g. 2em relative to parent's font size
rem (root em) e.g. 2rem relative to root's font size

You can also use percentages, e.g. 80%, which are relative to the parent's font size, but this isn't common with font sizes.

Relative Sizes Example

The text in the h2 is 48px and the text in the p is 24px.

Advice for Font Size

  • For your root font, use pixels;
  • Then use em or rem for all other rules.

:root {
    font-size: 16px;
}

h1 {
    font-size: 2em;
}
    
section h2 {
    font-size: 1.5em;
}
    
section section h3 {
    font-size: 1.17em;
}
    
section section section h4 {
    font-size: 1em;
}
        

Fonts

Some fonts are proportional;
others are monospace.

Some fonts have serifs;
others are sans-serif.

Fonts on the Web

  • You do not know what fonts will be available on your user's machine:
    • different platforms come with different fonts installed;
    • many software packages install further fonts.

Font Families

  • Use the font-family property but give it multiple values (a font stack) to cover multiple platforms, e.g.:
    
    body {
        font-family: Verdana, Geneva, Tahoma, sans-serif;
    }
  • First give the ideal font, then some good alternatives, then some common alternatives, then a generic.
  • The generics are: serif, sans-serif, cursive, fantasy and monospace.

Downloadable Fonts

CSS has downloadable fonts.
Use the @font-face @rule, e.g.


@font-face {
    font-family: MuseoSans;
    src: url('fonts/museosans-500-webfont.woff2') format('woff2');
}
    
body {
    font-family: MuseoSans, Verdana, Geneva, Tahoma, sans-serif;
}
    

The browser requests the museosans-500-webfont.woff2 file from the URL.

Choosing Fonts

Think about copyright!

Think about readability!

Free Fonts!

You can download font files from
https://fontlibrary.org/
https://fonts.google.com/
https://www.freefaces.gallery/

FOUT and FOIT

  • These fonts must be fetched by clients before they can be used.
  • Browsers have adopted two approaches:
Flash Of Unstyled Text (FOUT) Flash Of Invisible Text (FOIT)
the user first sees one of the fallback fonts then, after a delay, the font changes to the downloaded one the user first sees no text then, after a delay, the text appears using the downloaded font

G'luck!