The HTML style global attribute
The HTML style attribute applies inline CSS directly to one element, e.g. <p style="color:red">. It is a global attribute. Inline styles have very high specificity and override stylesheet rules, so prefer external CSS and reserve style for one-off or JavaScript-set values.
Overview
The style attribute holds one or more CSS declarations that apply to a single element. Each declaration is a property: value pair, and multiple declarations are separated by semicolons.
Inline styles sit at the top of the cascade: they beat element, class and id selectors, and only an !important rule (or an inline !important) can override them. That power is also their weakness: inline styles cannot be reused, cached, or use media queries and pseudo-classes, and they make markup harder to maintain. Reach for an external or internal stylesheet for anything beyond a one-off, and use the style attribute mainly for values your JavaScript sets at runtime.
Syntax
<p style="color: #b91c1c; font-weight: 700;">Important text</p>
Values
| Value |
|---|
| One or more CSS declarations (property: value;) separated by semicolons. |
Example
<p style="color:#b91c1c; font-weight:700; padding:8px 12px; background:#fef2f2; border-radius:6px;">This paragraph is styled inline.</p>
Best practices
- Prefer external or internal CSS with a class for anything reusable. Keep presentation in your stylesheet.
- Use the
styleattribute mainly for values set dynamically by JavaScript. - Remember inline styles win the cascade; debugging an "unstyleable" element often means an inline style is overriding your CSS.
- A strict Content-Security-Policy can block inline styles. Rely on stylesheets for production apps.
Accessibility
Never use style to convey meaning by appearance alone, for example color to indicate an error. Information shown only through styling is invisible to screen-reader users and to anyone who overrides your colors, so always pair it with text or an appropriate ARIA attribute.
Frequently asked questions
What is the HTML style attribute?
style="margin:0;color:#333".Is an inline style stronger than a CSS class?
!important declaration can override an inline style.