Definition and Usage
The style attribute lets you apply CSS styles directly to an element.
Applying styles directly to an element using the style attribute is called "inline styling".
Styling with the style attribute is one of the several ways to apply styles in HTML.
The style attribute is one of the global attributes that can be applied to all HTML elements.
Basic Example
<h2 style="color: white; background-color: black;">
style Attribute
</h2>
<p style="color: gray;">
This means applying styles only to the element by using the style attribute on the element you want to style with CSS.
</p>
style Attribute
This means applying styles only to the element by using the style attribute on the element you want to style with CSS.
Syntax
The style attribute declares CSS styles using CSS properties and values that are available in CSS. However, CSS selectors cannot be used.
To declare multiple styles, use a semicolon (;) after each declaration consisting of a property and value to separate them. The semicolon (;) after the last declaration can be omitted. Also, when declaring only one style, the semicolon (;) after the property and value can be omitted.
When Declaring Multiple Styles
<h2 style="color: white; background-color: black;">
style Attribute
</h2>
<!-- Or, the semicolon (;) after the last declaration can be omitted. -->
<h2 style="color: white; background-color: black">
style Attribute
</h2>
When Declaring a Single Style
<h2 style="color: gray;">
style Attribute
</h2>
<!-- Or, the semicolon (;) after the last declaration can be omitted. -->
<p style="color: gray">
style Attribute
</p>
CSS Selectors Cannot Be Used
The style attribute directly applies styles only to the element that contains the attribute. CSS selectors cannot be used.
<h2 style="div {color: gray;}">
style Attribute
</h2>
Things to Keep in Mind
It is generally best to avoid using inline styles that apply styles directly to an element with the style attribute.
They can be difficult to maintain, and you may need to use the same styles multiple times throughout a document. In addition, because CSS is written directly within elements in an HTML document, CSS styling information and HTML structural information are mixed together, making the code difficult to read and understand.
In addition, because you list properties and values directly without using CSS selectors, you cannot use pseudo-classes such as :hover and :focus or pseudo-elements such as ::before and ::after.
Lastly, inline styles that apply styles directly using the style attribute have a very high CSS specificity value, which is a numerical value indicating how specifically a selector is declared. Inline styles have the highest specificity among all selectors. Therefore, overusing inline styles can cause them to override other style rules. It is recommended to use inline styles sparingly and define styles in CSS files to maintain consistency and maintainability.
See CSS Specificity – Selector Order, Rules, and Cheat Sheet.
When Inline Styles Are Required
There are also cases where you must use inline styles to apply styles directly with the style attribute.
When sending emails using HTML templates, you can only apply inline styles to the template. This is because email clients do not support other methods of applying CSS due to strict security policies. For example, methods such as writing CSS code inside an HTML <style> tag or linking an external CSS file using an HTML <link> tag are not supported in emails.
Browser Compatibility
| Attribute |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
|---|---|---|---|---|
style
|
1 | 12 | 1 | 1 |
Specifications
| Specification | |
|---|---|
style
|
HTML Standard #the-style-attribute |