8. Colours, Fonts and Text Styling

CSS gives you control over colour and typography. Colours can be names (black), hex codes (#0a0a0a) or rgb values. Use color for text and background-color for the background. font-family chooses the typeface (always list a fallback), font-size sets the size, font-weight makes text bold, and text-align moves text left, center or right. line-height controls the space between lines and makes paragraphs easier to read.

Key points

  • Units: px is fixed, rem scales with the user's font size, % is relative to the parent.
  • Always give a fallback font such as sans-serif.
  • Keep strong contrast between text and background so it is readable.
  • Use a few colours consistently rather than many.

Example: Colours and fonts

HTML and CSS Open in editor
<h1>Styled Heading</h1>
<p class="intro">Good typography makes text easy to read.</p>

<style>
  body { font-family: Georgia, serif; line-height: 1.7; }
  h1 { color: #ffffff; background: #000000; padding: 12px; text-align: center; }
  .intro { font-size: 1.25rem; color: #222; font-weight: bold; }
</style>
Show explanation

Try changing the font to Arial, sans-serif, or change the heading colours.

Try it yourself

Edit the code below and press Run. The result appears in the preview underneath.

Editor
Reset Preview runs in a safe sandbox. Scripts are disabled.
PREVIEW