CSS Tutorial menu

CSS Text and Fonts

Most of a web page is text, so text and font styling makes a big difference to how it looks and reads.

Text Properties

PropertyExampleWhat it does
colorcolor: #333333;Text colour
text-aligntext-align: center;left, right, center or justify
text-decorationtext-decoration: underline;Adds or removes lines (use none to remove)
text-transformtext-transform: uppercase;uppercase, lowercase, capitalize
line-heightline-height: 1.6;Space between lines
letter-spacingletter-spacing: 2px;Space between letters
word-spacingword-spacing: 5px;Space between words
text-shadowtext-shadow: 2px 2px 4px gray;A shadow behind the text

Example

html
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
  text-align: center;
  text-transform: uppercase;
  letter-spacing: 3px;
  text-shadow: 2px 2px 3px #aaaaaa;
}
p {
  line-height: 1.7;
  text-align: justify;
}
.old-price {
  text-decoration: line-through;
  color: gray;
}
</style>
</head>
<body>

<h1>Eid Sale</h1>
<p>Our Eid sale is now live at the Tariq Road branch. Every shirt, shoe and bag in the shop has a discount, and there are gift boxes for children too. The sale runs until the end of the week.</p>
<p>Kurta: <span class="old-price">Rs 3,000</span> Rs 2,200</p>

</body>
</html>
Try it Yourself »

A line-height between 1.5 and 1.8 makes long paragraphs much easier to read. Write it without a unit, so it scales with the font size.

Font Family

font-family chooses the typeface. Give a list of fonts, separated by commas. The browser uses the first one it has, and the last item should be a generic family as a backup.

Example

css
body {
  font-family: "Segoe UI", Arial, sans-serif;
}

Put quotes around font names that contain spaces.

Generic familyLookExamples
serifSmall strokes at letter endsTimes New Roman, Georgia
sans-serifClean, no strokesArial, Verdana, Helvetica
monospaceEvery letter the same widthCourier New, Consolas
cursiveHandwriting styleComic Sans MS

Font Size, Weight and Style

  • font-size sets the size, for example 16px, 1.2rem or 120%
  • font-weight sets the thickness: normal, bold, or numbers from 100 to 900
  • font-style makes text italic or normal

Example

html
<!DOCTYPE html>
<html>
<head>
<style>
html { font-size: 16px; }
.small  { font-size: 0.875rem; }
.normal { font-size: 1rem; }
.large  { font-size: 1.5rem; font-weight: bold; }
.quote  { font-style: italic; font-family: Georgia, serif; }
</style>
</head>
<body>

<p class="small">Small text (14px)</p>
<p class="normal">Normal text (16px)</p>
<p class="large">Large bold text (24px)</p>
<p class="quote">"Practice every day, even for twenty minutes."</p>

</body>
</html>
Try it Yourself »

px, em or rem?

UnitBased on
pxA fixed size
emThe font size of the parent element
remThe font size of the root <html> element

rem is a good choice for font sizes. If a user sets a bigger default font in their browser, everything grows with it.

Using Google Fonts

Google Fonts offers free fonts you can load from the web. Add a <link> in the <head>, then use the font name in CSS:

Example

html
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;700&display=swap" rel="stylesheet">

Example

css
body {
  font-family: "Poppins", sans-serif;
}

Load only the weights you need. Every extra weight makes the page a little slower.

Exercise

Exercise

Why should a font-family list end with a generic family such as sans-serif?

Learn CSS with a teacher

Join the Web Development course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp