HTML Styles
HTML Styles: Applying Inline CSS Properties to Elements
The HTML style attribute adds CSS directly to a single element so that you can change its colors, fonts, alignment, and spacing right away. You write one or more property: value; pairs inside the tag, and the browser applies them to that element only. This lesson covers the syntax, a working example, the limits of inline styles, and the mistakes beginners make most often.
If you haven't written a basic page yet, our HTML page structure lesson is a good place to start.
Syntax of the Style Attribute
Write style="property: value;" inside the opening tag of any visible HTML element. Separate multiple declarations with semicolons so the browser parses each rule cleanly.
Common properties used during quick prototypes include color for text color, background-color for surface fills, font-family for typography, font-size for scale, and text-align for alignment.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>HTML Styles Demo</title>
</head>
<body style="background-color: #f8fafc; font-family: system-ui, sans-serif; padding: 24px;">
<h2 style="color: #0f172a; text-align: center;">Inline Style Example</h2>
<p style="color: #334155; font-size: 16px; line-height: 1.6;">
This paragraph uses inline CSS properties for color, size, and line height.
</p>
<div style="background-color: #e0f2fe; color: #0369a1; padding: 12px; border-radius: 6px;">
Notice banner styled with a light blue background and padding.
</div>
</body>
</html>
How to Check It Works
Save the code as index.html and open it in a browser. Then right-click the banner, choose Inspect, and look at the Styles panel. Your inline declarations appear under the element. style. Try deleting one property in DevTools and watch the page change instantly. That's a quick way to learn what each property does.
Why Inline Styles Have Limits
An inline style has higher specificity than a class or ID rule in an external stylesheet, which is why it wins when the two conflict. That makes it handy for quick tests and for HTML email templates, but it causes trouble at scale. If the same inline style is repeated on dozens of elements, a site-wide change means editing every one of them, and it's easy to miss some.
Inline styles also can't express everything. They don't support pseudo-classes such as hover or media queries for responsive layouts. For production sites, move reusable rules into an external .css file and attach them with class names. The MDN guide to styling HTML with CSS explains the full approach. You can also continue with our CSS selectors and classes lesson.
Common HTML Style Attribute Mistakes
Missing Semicolons Between Declarations
Writing color: red font-size: 18px breaks both properties because the browser can't tell where the first one ends. Put a semicolon after every declaration. The final one is technically optional, but keeping it makes later edits safer.
Forgetting Units on Dimensions
Write font-size: 18px, not font-size: 18. CSS requires a unit for non-zero lengths, and without one the browser ignores the declaration. The only common exception is zero, as in margin: 0.
Using Deprecated font or center Tags
Old HTML4 tags like <font> and <center> have been removed from the standard. Use the CSS properties font-family and text-align instead.
Conclusion
The style attribute is the fastest way to try out CSS, and it earns its place in HTML emails and in dynamic values set by JavaScript. For everything else, keep a simple rule: use inline styles to experiment, then move the rules into a stylesheet with classes once they're worth keeping.
Frequently Asked Questions (FAQs)
When is the HTML style attribute appropriate in real projects?
It works best in HTML emails, where external stylesheets may be blocked, and when JavaScript calculates values such as coordinates or a progress bar width.
Do inline styles override external CSS files?
Yes. An inline style beats class and ID selectors in an external stylesheet, unless that stylesheet rule uses! important.
Can I use hover in an inline style?
No. Pseudo-classes and media queries need a stylesheet or a <style> block.
Why isn't my inline style working?
Check for a missing semicolon, a missing unit, a misspelled property name, or mismatched quotes around the attribute value.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.