HTML Styles — The style Attribute
Learn how to use the HTML style attribute to add CSS directly to elements — covering background color, text color, font family, font size, and text alignment. Your first step into making pages actually look good.
- What is style?
- Syntax
- 3 Ways to Add CSS
- Background Color
- Text Color
- Font Family
- Font Size
- Text Alignment
- Combining Styles
- Common Mistakes
- Try It
- Summary
- FAQ
What is the HTML style Attribute?
The HTML style attribute adds inline CSS directly to any HTML element. It tells the browser exactly how that one element should look — its color, size, font, background, alignment, spacing, and more.
Think of it this way: HTML elements are the rooms in a house. The style attribute is your interior decorator — you're telling the browser "paint this room blue, make the ceiling 12 feet high, use oak flooring." Without CSS, every room looks identical. With it, each one can be completely different.
Every CSS property you can use in a stylesheet can also be used in the style attribute. That makes it a great learning tool — you don't need a separate CSS file to experiment and see results immediately.
Syntax
The style attribute uses CSS property:value pairs inside double quotes. Each pair ends with a semicolon, and multiple pairs are chained together in the same string:
<!-- Single property --> <p style="color: red;">Red text</p> <!-- Multiple properties separated by semicolons --> <p style="color: white; background-color: blue; font-size: 18px;"> Styled paragraph </p>
3 Ways to Add CSS to HTML
The style attribute is just one of three ways to apply CSS. Knowing all three helps you understand when inline styles are appropriate — and when they aren't:
Background Color
The background-color property sets the background of any HTML element. You can apply it to the entire page via the <body> tag, or to any individual element like a heading, paragraph, or div.
You can specify the color three ways: as a color name (like powderblue), as a hex code (like #86efac), or as an RGB value (like rgb(196,181,253)). All three work identically — the hex and RGB methods just give you more precise control.
<!-- Full page background --> <body style="background-color: powderblue;"> <h1>This is a heading</h1> <p>This is a paragraph.</p> </body> <!-- Individual element backgrounds --> <h1 style="background-color: #fef3c7;">Yellow heading</h1> <p style="background-color: #dbeafe;">Blue paragraph background</p>
This is a heading
This is a paragraph.
Text Color
The color property sets the text color of an element. Just like background-color, you can use color names, hex codes, or RGB values. The same three formats work for both properties — once you know one, you know the other.
<!-- Color names --> <h1 style="color: blue;">Blue Heading</h1> <p style="color: red;">Red paragraph</p> <!-- Hex color codes --> <p style="color: #7c3aed;">Purple paragraph</p> <!-- RGB values --> <p style="color: rgb(5, 150, 105);">Green paragraph</p>
Font Family
The font-family property sets the typeface used for text. A crucial rule: always provide a fallback font. If the user's device doesn't have your first-choice font, the browser tries the second. The pattern is font-family: "Preferred Font", fallback-category;
The three fallback categories are sans-serif (clean, modern fonts like Arial), serif (traditional fonts with decorative strokes, like Georgia), and monospace (fixed-width fonts like Courier, great for code).
<h1 style="font-family: Verdana, sans-serif;">Verdana Heading</h1> <p style="font-family: Courier New, monospace;">Courier New paragraph</p> <p style="font-family: Georgia, serif;">Georgia — classic serif font</p>
Font Size
The font-size property controls how large or small text appears. The most common unit for beginners is px (pixels) — it's fixed and predictable. Percentages are relative to the parent element's font size, which makes them useful for scaling but trickier to predict.
<!-- Using pixels (most predictable for beginners) --> <h1 style="font-size: 40px;">Big Heading</h1> <p style="font-size: 16px;">Normal paragraph</p> <p style="font-size: 12px;">Small fine print</p> <!-- Using percentage --> <h1 style="font-size: 300%;">Very large heading</h1> <p style="font-size: 160%;">Larger paragraph</p>
Text Alignment
The text-align property controls the horizontal alignment of text inside an element. There are four values — and justify is the one beginners most often forget about.
<h1 style="text-align: center;">Centered Heading</h1> <p style="text-align: left;">Left aligned (default)</p> <p style="text-align: right;">Right aligned</p> <p style="text-align: justify;">Justified — stretches text to fill the full line width.</p>
Centered Heading
Left aligned (default)
Right aligned
Justified — each line of text stretches to fill the full width of the container.
Combining Multiple Styles
Separating properties with semicolons lets you chain as many as you need in one style attribute. Here's a complete example combining everything from this lesson — background, color, font family, font size, and alignment all in one page:
<!DOCTYPE html> <html lang="en"> <body style="background-color: #f8fafc; font-family: Arial, sans-serif;"> <h1 style="color: #7c3aed; text-align: center; font-size: 36px;"> TipsInLearning </h1> <h2 style="color: #3b82f6; font-size: 24px;"> Free Web Development Courses </h2> <p style="color: #475569; font-size: 16px; line-height: 1.8;"> Learn HTML, CSS, and Bootstrap completely free. </p> <p style="background-color: #7c3aed; color: white; padding: 10px; text-align: center; border-radius: 8px;"> Start Learning Today! </p> </body> </html>
TipsInLearning
Free Web Development Courses
Learn HTML, CSS, and Bootstrap completely free.
Start Learning Today!
Common Mistakes
These are the errors that catch beginners out most often with the style attribute:
When you have multiple properties, every single one needs a semicolon after it — including the last one. Writing color: red font-size: 16px will silently fail — the browser won't apply either property. The correct form is color: red; font-size: 16px;
CSS uses a colon, not an equals sign. Writing color = "red" is HTML attribute syntax. Inside the style attribute, you need CSS syntax: color: red;. The colon separates the property from its value; the semicolon ends the declaration.
Inline styles work perfectly for learning and for one-off overrides. But if you put style="color:purple" on 50 paragraphs and then want to change them all to blue, you have to edit 50 lines. External CSS lets you change one rule and update the entire site. Learn inline styles to understand CSS, then graduate to external stylesheets.
Try It — Style Your First Webpage
Questions About HTML Styles
The questions beginners ask most often about the style attribute: