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.
<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).
<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 |
<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-familylist with a generic family likeserif,sans-seriformonospace. 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).
<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:
<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?
background-colorfont-colorcolortext-color
How are multiple declarations separated in a style attribute?
- Commas
- Semicolons
- Spaces only
- New lines
Which declaration centres text?
align: center;text-align: center;center: true;font-align: middle;