Lesson 2: Mastering Inline CSS
Learn how to apply styles directly to HTML elements and understand when to use inline CSS effectively.
Start LearningWhat is Inline CSS?
Inline CSS is applied directly to HTML elements using the style
attribute. This method allows you to apply unique styles to individual elements without affecting other elements on the page.
Inline CSS Syntax
The syntax for inline CSS is straightforward. Add a style
attribute to any HTML element:
<element style="property1: value1; property2: value2;"> Content goes here </element>
- Highest specificity - overrides other styles
- Only affects the specific element it's applied to
- Not reusable across multiple elements
- Can make HTML harder to read if overused
Practical Examples
Explore these practical examples of inline CSS in action:
Basic Text Styling
<h1 style="color: #2196F3; font-family: 'Arial', sans-serif; text-align: center;"> Welcome to CSS Tutorial </h1> <p style="font-size: 18px; line-height: 1.6; color: #333; padding: 10px; background-color: #f5f5f5;"> This paragraph demonstrates inline CSS styling. </p>
Result:
Welcome to CSS Tutorial
This paragraph demonstrates inline CSS styling.
Element Positioning & Effects
<div style="width: 200px; height: 150px; background: linear-gradient(135deg, #4CAF50, #2E7D32); border-radius: 10px; box-shadow: 0 5px 15px rgba(0,0,0,0.2); display: flex; align-items: center; justify-content: center; color: white; font-weight: bold; margin: 20px auto;"> Styled Box </div>
Result:
When to Use Inline CSS
While inline CSS has its limitations, it's perfect for specific scenarios:
Quick Testing & Prototyping
Ideal for testing styles during development without modifying external files.
Email Templates
Essential for HTML emails where external stylesheets are often blocked.
Dynamic Styling
Perfect for styles generated by JavaScript based on user interactions.
Unique Element Styles
Best for one-off styles that won't be reused elsewhere in your project.
Interactive Inline CSS Demo
Try creating your own inline CSS styles and see the results instantly:
CSS Properties
Result Preview
Generated Inline CSS
background-color: #f5f5f5;
font-size: 16px;
padding: 10px;
border-radius: 0px;"
Best Practices for Inline CSS
Follow these guidelines to use inline CSS effectively:
Next Steps
Now that you understand inline CSS, continue to the next lesson to learn about embedded stylesheets.