Plain black text on a white background works, but most websites use colour to show their brand, highlight important information and make pages easier to scan. The quickest way to add colour to a single element in HTML is the style attribute, which holds CSS declarations directly on the element.
With style you can change the text colour, the background colour, the font size, alignment and much more: <p style="color: navy;">. Colours themselves can be written in several ways: as a name like tomato, a HEX code like #ff6347, an RGB value like rgb(255, 99, 71) or an HSL value like hsl(9, 100%, 64%). All four can describe the same colour.
This lesson is your first step into styling. You'll learn the colour formats, how inline styles work, how to keep text readable with good contrast, and why real projects move most styling into CSS files, which the CSS tutorial covers in depth.
The style attribute
<p style="color: white; background-color: teal; padding: 8px;">Inline styled text</p>- The value is one or more CSS declarations: a property, a colon, a value and a semicolon.
colorsets the text colour;background-colorsets the background.- Other common properties:
font-size,font-family,text-align,padding,border. - Inline styles apply to that one element only, and they override most rules in style sheets.
Four ways to write a colour
| Format | Example | Notes |
|---|---|---|
| Name | tomato, navy, gold | 140+ named colours; easy to read |
| HEX | #ff6347 or short #f63 | Red, green and blue as two-digit hex pairs |
| RGB | rgb(255 99 71) | Each channel from 0 to 255 |
| HSL | hsl(9 100% 64%) | Hue (0 to 360), saturation and lightness |
Add transparency with an alpha value: #ff634780, rgb(255 99 71 / 50%) or hsl(9 100% 64% / 0.5). HSL is handy for making lighter or darker shades of one colour by changing only the lightness.
Readable colour and when to use inline styles
- Contrast is the difference between text and background brightness. Low contrast, such as light grey on white, is hard to read for many people, especially on phones outdoors. Aim for a contrast ratio of at least 4.5 to 1 for normal text; browser developer tools show the ratio when you inspect a colour.
- Don't rely on colour alone to carry meaning: an error message should say "Error", not just be red.
- When to use inline styles: quick experiments, one-off tweaks, HTML emails and values set by JavaScript. For a whole site, put styles in a CSS file so one change updates every page, as explained in adding CSS to HTML.
HTML Colors Syntax
Colour and style syntax
<p style="color: navy;">Navy text</p>
<p style="background-color: #fff3c4;">Pale yellow background</p>
<p style="color: rgb(220 38 38);">Red with RGB</p>
<p style="color: hsl(150 60% 30%);">Green with HSL</p>
<div style="background: rgb(0 0 0 / 60%); color: white;">Semi-transparent black</div>
<h1 style="font-size: 40px; text-align: center;">Big centred heading</h1>HTML Colors Summary
- The
styleattribute holds CSS declarations for one element. colorchanges text colour andbackground-colorchanges the background.- Colours can be names, HEX, RGB or HSL, and can include transparency.
- Keep text readable with strong contrast, at least 4.5 to 1 for body text.
- Use inline styles for one-off cases; use CSS files for real sites.
HTML Colors Examples
Each example runs in the page. Edit the code and press Run to see what changes.
The same colour four ways
<p style="color: white; background-color: tomato; padding: 8px;">Named: tomato</p>
<p style="color: white; background-color: #ff6347; padding: 8px;">HEX: #ff6347</p>
<p style="color: white; background-color: rgb(255 99 71); padding: 8px;">RGB: rgb(255 99 71)</p>
<p style="color: white; background-color: hsl(9 100% 64%); padding: 8px;">HSL: hsl(9 100% 64%)</p>Line-by-line explanation
- Lines 1 to 4All four lines produce exactly the same orange-red background.
- Line 1
paddingadds space inside the coloured box so the text isn't cramped.
Shades with HSL and transparency
<div style="background: hsl(210 80% 92%); padding: 10px;">Lightness 92%</div>
<div style="background: hsl(210 80% 70%); padding: 10px;">Lightness 70%</div>
<div style="background: hsl(210 80% 45%); padding: 10px; color: white;">Lightness 45%</div>
<div style="background: hsl(210 80% 25%); padding: 10px; color: white;">Lightness 25%</div>
<p style="background: hsl(210 80% 45% / 30%); padding: 10px;">Same blue at 30% opacity</p>Line-by-line explanation
- Lines 1 to 4Changing only the lightness gives a family of matching blues, from pale to dark.
- Lines 3 to 4Darker backgrounds need white text to keep good contrast.
- Line 5The value after the slash makes the colour see-through.
Readable and unreadable contrast
<p style="color: #c8c8c8; background: white; padding: 8px;">Light grey on white is hard to read.</p>
<p style="color: #1f2937; background: white; padding: 8px;">Dark grey on white is easy to read.</p>
<p style="color: #b91c1c; font-weight: bold;">Error: please enter your email address.</p>Line-by-line explanation
- Line 1Very low contrast, which fails accessibility guidelines.
- Line 2High contrast, comfortable for long reading.
- Line 3The message says "Error" in words, so it doesn't rely on red alone.
Common HTML Colors Mistakes
Forgetting the colon or semicolon
<p style="color red background-color yellow">Hi</p><p style="color: red; background-color: yellow;">Hi</p>Each declaration needs property: value;, or the browser ignores it.
Using the old font and bgcolor attributes
<font color="red">Sale!</font>
<body bgcolor="yellow"><p style="color: red;">Sale!</p>
<body style="background-color: yellow;"><font> and bgcolor are obsolete. Use CSS properties instead.
Styling every element inline
<p style="color: #333; font-size: 16px;">One</p>
<p style="color: #333; font-size: 16px;">Two</p>
<p style="color: #333; font-size: 16px;">Three</p><style> p { color: #333; font-size: 16px; } </style>
<p>One</p><p>Two</p><p>Three</p>Repeated inline styles are hard to change; one CSS rule styles them all.
HTML Colors in Real Projects
Colour in practice
Brand guidelines list colours as HEX codes, design tools like Figma export HEX and RGB values, and CSS frameworks define palettes in HSL. Inline styles survive mostly in HTML emails, where many email apps ignore style sheets, and in values set by JavaScript, such as a progress bar's width or a user-chosen theme colour.
HTML Colors Exercises
Answer each one and check it instantly. If you get stuck, open the hint before the answer.
Complete the inline style to make the text blue.
<p style=": blue;">Blue text</p>
color sets the text colour.Which property changes the background colour?
bgcolor is an obsolete attribute, not a CSS property.Which of these is the same colour as #000000?
Why is light grey text on a white background a problem?
The style declarations are written incorrectly. Fix them so the box has white text on a dark green (#14532d) background with 12px padding.
<div class="box" style="color=white, background-color=#14532d, padding=12px">Welcome!</div>- Text is white
- Background is dark green
- Padding is 12px
HTML Colors Quiz
5 questions to check what you learned.
What does the style attribute contain?
- HTML tags
- CSS declarations
- JavaScript
- A file name
In
rgb(255 0 0), what colour is shown?- Green
- Red
- Blue
- Black
What does the value after the slash in
rgb(0 0 0 / 50%)control?- Transparency (alpha)
- Brightness
- Hue
- Size
Which colour format makes it easiest to create lighter and darker shades?
- Names
- HEX
- HSL
- None
Why are inline styles avoided for whole websites?
- Browsers ignore them
- They don't support colours
- They must be repeated on every element and are hard to update consistently
- They make pages invalid
HTML Colors Challenge
A colourful notice
Create a notice box:
- A
<div class="notice">with an inline style - Background colour
#fef3c7and text colour#78350f - Padding of 16px and a
<strong>word inside
- The notice has a pale yellow background
- The text is dark brown
- 16px padding
- Contains a strong element