TExcelTAD
Home › HTML › HTML Styles

HTML Styles

By default every browser draws HTML with plain, sensible styles. To change colours, fonts, sizes and alignment you use CSS. The quickest way to try CSS is the style attribute, which adds styles directly to a single element.

The style attribute

<tagname style="property: value; property: value;">

Each declaration is a CSS property, a colon, a value and a semicolon. You can put as many declarations as you like inside one style attribute.

html · try it yourself
<h1 style="color: teal;">A teal heading</h1>
<p style="color: white; background-color: slateblue; padding: 8px;">
  White text on a slate-blue background.
</p>

Colours

color sets the text colour, and background-color sets the colour behind an element. You can use colour names, hex codes or rgb() values (more in the Colors lesson).

html · try it yourself
<body style="background-color: #fdf6e3;">
  <h2 style="color: #b58900;">Warm heading</h2>
  <p style="color: rgb(38, 139, 210);">Blue paragraph text.</p>
  <p style="background-color: gold;">Gold background.</p>
</body>

Fonts and sizes

Property Example value Effect
font-family Georgia, serif Typeface; list fallbacks after commas
font-size 20px, 1.5em, 120% Text size
font-weight bold, 400, 700 Thickness of the letters
font-style italic Slanted text
html · try it yourself
<p style="font-family: Georgia, serif; font-size: 22px;">A classic serif look.</p>
<p style="font-family: 'Courier New', monospace;">Typewriter style.</p>
<p style="font-family: system-ui, sans-serif; font-weight: 700;">Bold and modern.</p>
<p style="font-size: 12px; font-style: italic;">Small italic print.</p>

Tip: Always end a font-family list with a generic family like serif, sans-serif or monospace. If none of your preferred fonts are installed, the browser falls back to that.

Text alignment

text-align lines text up to the left, right, center or justify (both edges).

html · try it yourself
<h2 style="text-align: center;">Centred title</h2>
<p style="text-align: right;">Right-aligned signature — Ada</p>
<p style="text-align: justify; width: 260px;">
  Justified text stretches every full line so that both the left and the right edge are straight, like a newspaper column.
</p>

Combining styles

You can mix properties freely. Here is a little "card" made from a single <div> with several declarations:

html · try it yourself
<div style="max-width: 260px; padding: 16px; border: 2px solid #e34c26; border-radius: 12px;
            background-color: #fff5f2; font-family: system-ui, sans-serif;">
  <h3 style="margin-top: 0; color: #e34c26;">Tip of the day</h3>
  <p style="margin-bottom: 0;">Drink a glass of water before your coffee.</p>
</div>

Common style properties

Property What it changes
color Text colour
background-color Background colour
font-family Typeface
font-size Size of text
text-align Horizontal alignment
padding Space inside the border
margin Space outside the border
border Line around the element

When to use inline styles

Inline styles are perfect for learning and quick tests, but they get repetitive: if you want every paragraph blue, you'd have to repeat style="color: blue" on each one. Real sites put their CSS in a <style> block or a separate .css file — covered in the HTML CSS lesson.

Note: The old <font> and <center> elements once did this job. They are obsolete in HTML5 — always use CSS instead.

Test yourself

Which property changes the colour of the text itself?

  1. background-color
  2. font-color
  3. color
  4. text-color

How are multiple declarations separated in a style attribute?

  1. Commas
  2. Semicolons
  3. Spaces only
  4. New lines

Which declaration centres text?

  1. align: center;
  2. text-align: center;
  3. center: true;
  4. font-align: middle;