Skip to content

02. Inline CSS

Inline CSS applies styles directly to HTML elements using the style attribute. While convenient for quick styling, inline styles have limitations and should be used sparingly. Understanding when and how to use inline CSS helps maintain clean, maintainable code.

<p style="color: red; font-size: 18px;">Red text</p>
<div style="background-color: blue; color: white; padding: 20px;">
Styled div
</div>
<div style="border: 1px solid red;">Testing</div>
<div style="background-color: var(--bg-color);">
Dynamic styling
</div>
<p style="color: #333; font-family: Arial;">Email content</p>

Styles can’t be reused:

<!-- Repeated styles -->
<p style="color: blue;">Text 1</p>
<p style="color: blue;">Text 2</p>

Difficult to update:

<!-- Change color in many places -->
<p style="color: blue;">Text</p>

Can’t use :hover, :focus, etc.:

<!-- No hover effect possible -->
<a style="color: blue;">Link</a>
<!-- Good: For dynamic or one-off styles -->
<div style="display: none;" id="hidden">Content</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>