Skip to content

The style Attribute

Global Attribute

The style attribute allows you to apply CSS styles directly to an HTML element using inline CSS declarations. While convenient for quick styling and dynamic changes, it should be used sparingly in favor of external stylesheets.

<element style="property: value; property: value;">Content</element>
ValueDescription
CSS declarationsOne or more CSS property-value pairs separated by semicolons
Result
Result
Result
Result
<div id="progress-bar" style="width: 0%; background: #3b82f6; height: 20px;"></div>
<script>
let progress = 0;
const bar = document.getElementById('progress-bar');
setInterval(() => {
progress = Math.min(progress + 10, 100);
bar.style.width = progress + '%';
}, 500);
</script>
<!-- Email templates or CMS content where external CSS isn't available -->
<div style="background: #f0f0f0; padding: 20px;">
User-generated content with safe inline styles
</div>
<!-- Testing styles before moving to stylesheet -->
<button style="background: #10b981; color: white; padding: 10px 20px;">
Prototype Button
</button>
<!-- Avoid: Inline styles for static content -->
<div style="margin: 20px; padding: 15px; background: #f0f0f0;">
Content
</div>
<!-- Better: Use classes -->
<div class="content-box">
Content
</div>
<style>
.content-box {
margin: 20px;
padding: 15px;
background: #f0f0f0;
}
</style>
Result
Result
// Single property
element.style.backgroundColor = '#3b82f6';
element.style.fontSize = '18px';
// Multiple properties
element.style.cssText = 'color: white; background: #3b82f6; padding: 20px;';
// Using setProperty
element.style.setProperty('color', 'white');
element.style.setProperty('--custom-var', '#3b82f6');
// Get inline style value
const color = element.style.color;
// Get computed style (includes all styles, not just inline)
const computed = window.getComputedStyle(element);
const fontSize = computed.fontSize;
Result

Inline styles can set and use CSS custom properties:

Result

Inline styles have very high specificity (1,0,0,0):

Result
<!-- Avoid -->
<div style="display: flex; justify-content: space-between; align-items: center; padding: 20px; background: #f0f0f0;">
<span style="font-weight: bold; color: #333;">Title</span>
<button style="padding: 8px 16px; background: #3b82f6; color: white; border: none; border-radius: 4px;">
Action
</button>
</div>
<!-- Better -->
<div class="header">
<span class="header-title">Title</span>
<button class="btn btn-primary">Action</button>
</div>
<!-- Good: Dynamic value from JavaScript -->
<div id="progress" style="width: 75%;"></div>
<!-- Avoid: Static value that could be in CSS -->
<div style="width: 75%;"></div>
<!-- Acceptable: Simple inline adjustment -->
<img src="photo.jpg" style="max-width: 100%;" alt="Photo">
<!-- Avoid: Complex styles belong in CSS -->
<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 20px; padding: 30px; background: linear-gradient(to right, #f0f0f0, #ffffff);">
Complex layout
</div>

Never directly insert user input into the style attribute:

// Dangerous - potential XSS vulnerability
element.style = userInput;
// Safer - use specific properties
element.style.backgroundColor = userInput;
// Best - validate and sanitize
if (/^#[0-9A-Fa-f]{6}$/.test(userInput)) {
element.style.backgroundColor = userInput;
}
<!-- Poor performance: Many inline styles -->
<div style="color: red;">Item 1</div>
<div style="color: red;">Item 2</div>
<div style="color: red;">Item 3</div>
<!-- ... 100 more items -->
<!-- Better: Use a class -->
<div class="item">Item 1</div>
<div class="item">Item 2</div>
<div class="item">Item 3</div>
<style>
.item { color: red; }
</style>
// Inefficient: Multiple reflows
element.style.width = '100px';
element.style.height = '100px';
element.style.background = '#3b82f6';
// Better: Single reflow
element.style.cssText = 'width: 100px; height: 100px; background: #3b82f6;';
// Or use classes
element.classList.add('styled-box');

The style attribute is supported by all browsers.

BrowserSupport
ChromeAll versions
FirefoxAll versions
SafariAll versions
EdgeAll versions
  • class - Apply predefined CSS classes
  • id - Target elements with CSS selectors