style
CSS styles written directly on an element.
- Applies to
- All HTML elements
Example
#<p style="color: #fbbf24; font-weight: bold;">Text with an inline style</p>
<div style="width: 60%; height: 10px; border-radius: 99px; background: linear-gradient(90deg, #8b5cf6, #38bdf8);"></div>Definition and usage
#The style attribute accepts CSS declarations that apply to that one element only. The syntax is the same as in a stylesheet, just without the selector and curly braces: style="color: tomato; padding: 8px".
Styles in the attribute take precedence over most stylesheet rules, so they are hard to override. That is why they are mostly useful for quick tests, HTML emails, and values set dynamically by JavaScript.
For everyday work it is better to keep the appearance in a CSS file and connect it to elements through classes. The code is then easier to read, and the styles can be reused.
Syntax
#<element style="property: value; another: value">β¦</element>Browser support
#Widely available
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#- If you repeat the same style in several places, move it into a class.
- Sites with a strict Content Security Policy (CSP) may block inline styles.
Related entries
#Spotted a mistake or missing an example? Write to us.