HTML style Attribute

Beginner
⏱️ 6 min read
📚 Updated: Jun 2026
🎯 3 Examples
CSS & Presentation

Introduction

The style attribute in HTML lets you apply inline CSS directly to an element. You can set colors, fonts, spacing, borders, and many other visual properties without opening a separate stylesheet file.

Inline styles are handy for quick experiments, email templates, or one-off tweaks. For larger projects, external CSS files or classes usually keep code cleaner and easier to maintain.

What You’ll Learn

01

Inline CSS

On elements.

02

Syntax

property: value.

03

Common props

color, margin.

04

Specificity

Overrides class.

05

JavaScript

element.style.

06

When to avoid

Big projects.

Purpose of style

The primary purpose of the style attribute is to add specific styling to an individual HTML element. It is convenient for quick styling inside the HTML document itself.

While inline styles work everywhere, relying on them heavily makes code harder to update. External stylesheets and CSS classes separate structure (HTML) from presentation (CSS), which is a core best practice in web development.

💡
High specificity

Inline styles beat most stylesheet rules (except those with !important). That power is useful for overrides but can make debugging harder if overused.

📝 Syntax

Add style to any HTML element with one or more CSS declarations:

syntax.html
<tagname style="property: value;">

For example, to set the text color of a paragraph to red:

red-paragraph.html
<p style="color: red;">This is a red paragraph.</p>

Syntax Rules

  • Each declaration is property: value; (semicolon between rules).
  • Property names use standard CSS spelling (e.g. background-color).
  • Multiple properties go in one string: style="color: blue; font-size: 18px;".
  • Quote the attribute value with double quotes; wrap CSS values containing spaces as needed.
  • Global attribute — valid on almost every HTML element.
  • JavaScript access: HTMLElement.style (camelCase for multi-word properties).

💎 Values

The style attribute accepts CSS property–value pairs. Some common properties include:

  • color — Sets the text color.
  • font-size — Specifies the font size.
  • margin — Controls outer spacing around the element.
  • padding — Controls inner spacing inside the element.
  • background-color — Sets the background color.
  • text-align — Aligns text (left, center, right).
  • border — Adds a border around the element.
  • display — Controls layout (block, flex, none, etc.).

These are just a few examples. Any valid CSS property can appear in the style attribute, subject to browser support for that property.

style-values.html
<div style="color: #1e40af; background-color: #dbeafe; padding: 1rem; border-radius: 8px;">
  Styled box
</div>

⚡ Quick Reference

TopicDetailExample
FormatCSS declarations"color: red;"
ScopeSingle elementThat tag only
SpecificityVery highBeats most classes
JavaScriptel.style.colorcamelCase names
AlternativeExternal CSS<link rel="stylesheet">
AlternativeClass attributeReusable styles

Applicable Elements

ElementSupported?Notes
<div>, <p>, <span>YesCommon inline styling targets
<h1><h6>YesHeadings, titles
<table>, <td>YesLegacy table layouts (prefer CSS classes today)
<img>, <a>YesSize, color, borders
Most HTML elementsYesGlobal attribute in HTML

Inline style vs stylesheet vs class

MethodProsCons
style attributeFast, no extra file, high specificityHard to reuse; clutters HTML
External CSS fileReusable, cacheable, organizedExtra HTTP request (minimal with HTTP/2)
<style> in headPage-level rules in one placeNot shared across pages
class attributeSemantic HTML + CSS separationRequires stylesheet definition

Examples Gallery

Red paragraph, a styled heading and paragraph, and dynamic updates with JavaScript.

👀 Live Preview

Inline styles applied directly on elements:

This paragraph uses style="color: #dc2626;".

This one combines font-size, line-height, padding, and background.

Example — colored paragraph

The simplest use of the style attribute:

style.html
<p style="color: red;">This is a red paragraph.</p>
Try It Yourself

Example — inline styles on a page

Multiple properties on heading and paragraph:

inline-styles.html
<h1 style="color: blue; text-align: center;">Welcome to My Website</h1>

<p style="font-size: 16px; line-height: 1.5;">
  This is a paragraph with custom styles applied.
</p>
Try It Yourself

How It Works

In this example, style sets color and text alignment on the heading, and font size and line height on the paragraph—all without a separate CSS file.

Dynamic Values with JavaScript

Update styles at runtime through the element.style object:

dynamic-style.html
<p id="dynamicElement">This text will change style dynamically.</p>

<script>
  var el = document.getElementById("dynamicElement");
  el.style.color = "green";
  el.style.fontSize = "20px";
</script>
Try It Yourself

How It Works

JavaScript writes to element.style, which updates the element’s inline styles. Property names use camelCase in JS (fontSize instead of font-size). This is ideal for interactive UI such as toggling visibility or highlighting selections.

♿ Accessibility

  • Do not rely on color alone — Pair color changes with text labels, icons, or patterns so color-blind users understand meaning.
  • Maintain contrast — Inline colors must still meet WCAG contrast ratios for text and backgrounds.
  • Prefer classes for focus states — Keyboard focus outlines are easier to manage consistently in stylesheets than per-element inline rules.
  • Avoid hiding content carelesslystyle="display: none;" removes content from the accessibility tree; use thoughtfully.
  • Respect user preferences — Stylesheets can honor prefers-reduced-motion and dark mode; inline styles often cannot.

🧠 How style Works

1

Author adds style

CSS in attribute.

HTML
2

Browser parses CSS

property: value.

Parse
3

Styles apply

That element only.

Render
=

Instant presentation

No extra file.

Browser Support

The style attribute has been supported since the earliest days of HTML and CSS. Every modern browser applies inline styles reliably.

Universal · Fully supported

Works everywhere

Inline CSS is one of the most widely supported features in web browsers.

100% All browsers
Google Chrome All versions
Supported
Mozilla Firefox All versions
Supported
Apple Safari All versions
Supported
Microsoft Edge All versions
Supported
style attribute Excellent

Bottom line: Individual CSS property support varies, but the style attribute itself works in all browsers.

💡 Best Practices

✅ Do

  • Use style for small, specific one-off styling tasks
  • Keep declarations readable: one property per line in longer strings if needed
  • Use external stylesheets for site-wide design systems
  • Use JavaScript element.style for interactive UI changes
  • Validate contrast when setting colors inline

❌ Don’t

  • Cover every element in inline styles on large projects
  • Insert untrusted user input into style (security risk)
  • Duplicate the same inline styles on dozens of elements
  • Rely on inline styles when responsive media queries are needed
  • Forget that classes and CSS files are easier to update globally

Conclusion

The style attribute provides a convenient way to apply inline CSS directly to HTML elements. Whether for quick styling or dynamic updates through JavaScript, it gives you immediate control over how an element looks.

For long-term maintainability, combine inline styles sparingly with external stylesheets and the class attribute. That balance keeps your HTML clean while still letting you override styles when you truly need to.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about style

Bookmark these when styling HTML elements.

5
Core concepts
📝 02

property: value

Syntax

Format
💪 03

High specificity

Overrides class

Cascade
⚙️ 04

element.style

JavaScript

Dynamic
📁 05

Use CSS files

Big projects

Maintain

❓ Frequently Asked Questions

It applies inline CSS declarations directly to a single HTML element.
Yes. Separate them with semicolons: style="color: red; font-size: 18px;".
Generally yes, because inline styles have higher specificity than class rules in a stylesheet.
Use element.style.propertyName with camelCase, e.g. el.style.fontSize = "20px".
On large sites where the same styles repeat. Use external CSS and classes instead.
No. style is an attribute on one element. <style> is an element in the document head that holds CSS rules for many selectors.

Practice inline CSS

Open the editor and experiment with color, font-size, and spacing on a live page.

Try full page example →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful