HTML Styles (Inline CSS Basics)
HTML Styles (Inline CSS Basics)
The HTML style attribute lets you apply CSS directly to a single element using inline styling. While it's the fastest way to see CSS in action, understanding when (and when not) to use it is an essential modern web development skill.
The style Attribute Syntax
<tagname style="property: value; property2: value2;">Content</tagname>Common Inline CSS Properties
color— sets text colorbackground-color— sets background colorfont-size— sets text sizefont-family— sets the fonttext-align— aligns text (left, center, right, justify)padding/margin— controls spacingborder— adds a border around the element
<p style="color: blue; font-size: 20px; text-align: center;">
Styled paragraph text.
</p>Inline vs Internal vs External CSS (Quick Preview)
Inline CSS is just one of three ways to apply styles — we cover the full comparison in the next lesson, HTML & CSS Integration. For now, remember:
- Inline CSS — written directly in the
styleattribute of one element. - Internal CSS — written inside a
<style>tag in the document's<head>. - External CSS — written in a separate
.cssfile and linked to the page.
Why Inline Styles Should Be Used Sparingly
Modern web development best practices strongly favor external CSS over inline styles because inline styles:
- Mix content (HTML) with presentation (CSS), making code harder to maintain.
- Cannot be reused across multiple elements.
- Have the highest CSS specificity, making them difficult to override.
- Bloat HTML file size on larger projects, hurting page performance.
Inline styles are still useful for quick prototyping, one-off overrides, or when generating dynamic styles with JavaScript.
Key Takeaways
- The
styleattribute applies CSS directly to a single HTML element. - Inline styles use
property: value;pairs separated by semicolons. - Inline CSS has the highest specificity and is hardest to override or reuse.
- Prefer external CSS for real projects; use inline styles sparingly.
Press Run to execute.
Press Run to execute.
Add inline styles to give the div a light gray background, 20px padding, and centered text, and make the h3 red.
Press Run to execute.
This is a self-check — compare your result with the expected output above.
Was this page helpful?