👀 Live Preview
Inline styles applied directly on elements:
This paragraph uses style="color: #dc2626;".
This one combines font-size, line-height, padding, and background.

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.
On elements.
property: value.
color, margin.
Overrides class.
element.style.
Big projects.
styleThe 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.
Inline styles beat most stylesheet rules (except those with !important). That power is useful for overrides but can make debugging harder if overused.
Add style to any HTML element with one or more CSS declarations:
<tagname style="property: value;">For example, to set the text color of a paragraph to red:
<p style="color: red;">This is a red paragraph.</p>property: value; (semicolon between rules).background-color).style="color: blue; font-size: 18px;".HTMLElement.style (camelCase for multi-word properties).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.
<div style="color: #1e40af; background-color: #dbeafe; padding: 1rem; border-radius: 8px;">
Styled box
</div>| Topic | Detail | Example |
|---|---|---|
| Format | CSS declarations | "color: red;" |
| Scope | Single element | That tag only |
| Specificity | Very high | Beats most classes |
| JavaScript | el.style.color | camelCase names |
| Alternative | External CSS | <link rel="stylesheet"> |
| Alternative | Class attribute | Reusable styles |
| Element | Supported? | Notes |
|---|---|---|
<div>, <p>, <span> | Yes | Common inline styling targets |
<h1> – <h6> | Yes | Headings, titles |
<table>, <td> | Yes | Legacy table layouts (prefer CSS classes today) |
<img>, <a> | Yes | Size, color, borders |
| Most HTML elements | Yes | Global attribute in HTML |
style vs stylesheet vs class| Method | Pros | Cons |
|---|---|---|
style attribute | Fast, no extra file, high specificity | Hard to reuse; clutters HTML |
| External CSS file | Reusable, cacheable, organized | Extra HTTP request (minimal with HTTP/2) |
<style> in head | Page-level rules in one place | Not shared across pages |
class attribute | Semantic HTML + CSS separation | Requires stylesheet definition |
Red paragraph, a styled heading and paragraph, and dynamic updates with JavaScript.
Inline styles applied directly on elements:
This paragraph uses style="color: #dc2626;".
This one combines font-size, line-height, padding, and background.
The simplest use of the style attribute:
<p style="color: red;">This is a red paragraph.</p>Multiple properties on heading and paragraph:
<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>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.
Update styles at runtime through the element.style object:
<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>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.
style="display: none;" removes content from the accessibility tree; use thoughtfully.prefers-reduced-motion and dark mode; inline styles often cannot.CSS in attribute.
property: value.
That element only.
No extra file.
The style attribute has been supported since the earliest days of HTML and CSS. Every modern browser applies inline styles reliably.
Inline CSS is one of the most widely supported features in web browsers.
Bottom line: Individual CSS property support varies, but the style attribute itself works in all browsers.
style for small, specific one-off styling taskselement.style for interactive UI changesstyle (security risk)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.
styleBookmark these when styling HTML elements.
On element
PurposeSyntax
FormatOverrides class
CascadeJavaScript
DynamicBig projects
Maintainstyle="color: red; font-size: 18px;".element.style.propertyName with camelCase, e.g. el.style.fontSize = "20px".style is an attribute on one element. <style> is an element in the document head that holds CSS rules for many selectors.Open the editor and experiment with color, font-size, and spacing on a live page.
5 people found this page helpful