Skip to content

05. style Attribute

The style attribute allows you to apply CSS styles directly to HTML elements. While convenient, inline styles should be used sparingly as they reduce maintainability and reusability. Understanding when and how to use the style attribute helps balance convenience with best practices.

<p style="color: red; font-size: 18px;">Red text</p>
<div style="background-color: blue; color: white; padding: 20px;">
Styled content
</div>
<div style="display: none;" id="hidden">Hidden content</div>
<div style="border: 1px solid red;">Testing</div>
<p style="color: #333; font-family: Arial;">Email content</p>
<!-- Styles can't be reused -->
<p style="color: blue;">Text 1</p>
<p style="color: blue;">Text 2</p>
<!-- Difficult to update multiple instances -->
<p style="color: blue;">Text</p>
<!-- Good: For dynamic or one-off styles -->
<div style="display: none;">Hidden</div>
<!-- Avoid: For repeated styles -->
<p style="color: blue;">Text</p>
<p style="color: blue;">More text</p>
<!-- Good: External CSS -->
<link rel="stylesheet" href="styles.css">
<!-- Avoid: Inline for everything -->
<div style="...">Content</div>