HTML style Attribute

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The style global attribute holds CSS declarations applied to that one element — for example color: navy; or padding: 1rem;. This is often called inline CSS.

MDN recommends putting real site styles in a separate CSS file (or a <style> block). Use the attribute mainly for quick tests, tiny one-offs, or styles set from script. Prefer class for reusable look-and-feel. See also how to add CSS to HTML.

Baseline · Widely available.

Not deprecated and not experimental. A standard global attribute on every HTML element since early browsers (Baseline since about July 2015).

Not for meaning or hiding.

Per MDN: do not use style to convey semantics. Do not hide irrelevant content with display:none in style — use hidden instead. The page should stay correct if all styling is removed.

HTML Syntax

syntax.html
<element style=" declarations">

declarations are CSS property/value pairs separated by semicolons (same idea as inside a CSS rule). Equivalent JS: el.style.color = "navy".

Attribute Values

The value is a CSS declaration list. Common patterns:

Value / patternDescriptionTry it
color: …;One property on the elementTry it
prop: val; prop: val;Several declarations in one attributeTry it
vs classInline one-off vs reusable stylesheet rulesTry it
Empty / omittedNo inline styles from this attributeTry it

Live Preview

Pick a preset or type CSS declarations. They are written into the element’s style attribute.

Use valid CSS declarations separated by semicolons. Invalid CSS is ignored by the browser.

Preview

Styled with the style attribute.

style="color: #0f172a; background: #fef3c7; padding: 0.75rem;"

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Basic Inline Color

One CSS property on a paragraph.

HTML
<p style="color: navy;">
  This text uses the style attribute.
</p>
Try It Yourself

How It Works

  1. Add style on the element you want to change.
  2. Write a CSS declaration — property, colon, value, optional semicolon.
  3. The browser applies it only to that element (high specificity for that property).
  4. Meaning stays in HTML — this <p> is still a paragraph if styles are removed.

Example 2 — Several Declarations

Separate each property with a semicolon inside one attribute.

HTML
<div style="background: #ffe7e8; border: 2px solid #e66465; padding: 1rem;">
  <p style="margin: 0; text-align: center;">
    Quick inline box for testing.
  </p>
</div>
Try It Yourself

How It Works

  1. List declarations like in a CSS rule body: prop: value;
  2. Parent and child can each have their own style.
  3. Good for demos — MDN notes this pattern for quick styling and tests.
  4. For production UI, move shared rules into a stylesheet + class.

Example 3 — style vs class

Same look: one-off inline vs a reusable class (preferred for real pages).

HTML
<!-- One-off (fine for a quick test) -->
<p style="color: #166534; font-weight: bold;">Inline style</p>

<!-- Reusable (preferred for site design) -->
<style>
  .note { color: #166534; font-weight: bold; }
</style>
<p class="note">Class-based style</p>
Try It Yourself

How It Works

  1. Inline style ties CSS to one element in the markup.
  2. class + CSS rule can style many elements with one change.
  3. Maintainability — editing a stylesheet is easier than hunting attributes.
  4. Use both wisely — inline for rare exceptions; classes for the design system.

Edge Cases & Pitfalls

Most problems come from overusing inline CSS or using it to hide or imply meaning.

semantics

Not semantic

Color or bold in style does not mark importance. Use proper HTML (<strong>, headings) for meaning.

hide

Prefer hidden to hide

MDN: do not use style alone to mark content as irrelevant. Use hidden.

!important

Hard to override

Inline styles beat most stylesheet rules unless you use !important (messy). Prefer classes when you need easy overrides.

csp

Content Security Policy

Some sites block inline styles via CSP (style-src). External CSS + classes are safer in locked-down apps.

quotes

Quotes in values

If a CSS value needs quotes (fonts, URLs), use the other quote style inside the attribute, or escape carefully.

Browser Support

style is an HTML global attribute for inline CSS in the browser. It is not a Node.js, Deno, or Bun API.

✓ Widely available

style

Supported in all modern browsers on every HTML element. Prefer stylesheets for maintainable design.

98% Global support (approx.)
Google Chrome All modern
Full support
Mozilla Firefox All modern
Full support
Apple Safari All modern
Full support
Microsoft Edge All modern
Full support
Opera All modern
Full support
Samsung Internet Modern
Full support
Internet Explorer All
Supported
style Widely supported

Bottom line: Safe to use for quick tests and one-offs. For site-wide design, use CSS files and class names.

Key Takeaways

  • Role: CSS declarations on one element (inline CSS).
  • Prefer stylesheets + class for real sites (MDN).
  • Not for semantics or for hiding content — use proper HTML / hidden.
  • Global: allowed on any HTML element.

One line: style is inline CSS for one element — great for demos; use CSS files and class for design.

Frequently Asked Questions

It is a global attribute that holds CSS declarations applied to that one element, for example color: red; or margin: 1rem;. This is often called inline CSS.
No. MDN recommends defining styles in separate CSS files (or a style element) for real sites. The style attribute is mainly for quick tests, tiny one-off tweaks, or dynamically set styles.
class names reusable rules in a stylesheet. style puts CSS on a single element and is harder to reuse or override cleanly. Prefer class for shared look-and-feel.
The style attribute styles one element. The style element in the document head (or a linked stylesheet) can style many elements with selectors.
Technically yes, but MDN says do not use style to convey that content is irrelevant. Prefer the hidden attribute for content that should not be presented.
No. It is a standard global attribute and Baseline widely available. The caution is about overuse and misusing it for meaning, not deprecation.

Next: summary

Continue with the next HTML attribute in the series.

summary tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

8 people found this page helpful