Lesson 3: Mastering Embedded CSS
Learn how to apply styles within HTML documents using embedded stylesheets for efficient styling.
Start LearningWhat is Embedded CSS?
Embedded CSS (also called internal CSS) is placed within the <style>
tag in the <head>
section of an HTML document. This method allows you to define styles for the entire document in one place.
Embedded CSS Syntax
The syntax for embedded CSS is straightforward. Place your CSS rules inside a <style>
element:
<!DOCTYPE html> <html> <head> <style> selector { property1: value1; property2: value2; } </style> </head> <body> <!-- HTML content --> </body> </html>
- Applies to the entire HTML document
- Medium specificity - overrides external styles but not inline styles
- More efficient than inline CSS for multiple elements
- Easier to maintain than inline CSS
Practical Examples
Explore these practical examples of embedded CSS in action:
Basic Document Styling
<!DOCTYPE html> <html> <head> <style> body { background-color: #f0f9ff; font-family: 'Segoe UI', Tahoma, sans-serif; line-height: 1.6; margin: 0; padding: 20px; } h1 { color: #0d47a1; text-align: center; border-bottom: 2px solid #2196f3; padding-bottom: 10px; } p { color: #333; font-size: 18px; margin-bottom: 15px; } .highlight { background-color: #fffde7; padding: 5px; border-radius: 4px; } </style> </head> <body> <h1>Welcome to CSS Tutorial</h1> <p>This paragraph demonstrates embedded CSS styling.</p> <p class="highlight">This paragraph has a special highlight class.</p> </body> </html>
Result:
Advanced Component Styling
<!DOCTYPE html> <html> <head> <style> .card-container { display: flex; gap: 20px; justify-content: center; padding: 20px; } .card { width: 250px; background: white; border-radius: 10px; box-shadow: 0 5px 15px rgba(0,0,0,0.1); overflow: hidden; transition: transform 0.3s; } .card:hover { transform: translateY(-10px); } .card img { width: 100%; height: 180px; object-fit: cover; } .card-content { padding: 20px; } .card h3 { margin-top: 0; color: #0d47a1; } .btn { display: inline-block; background: #2196f3; color: white; padding: 8px 15px; border-radius: 4px; text-decoration: none; margin-top: 10px; } </style> </head> <body> <div class="card-container"> <div class="card"> <img src="https://picsum.photos/400/300?nature" alt="Nature"> <div class="card-content"> <h3>Nature Card</h3> <p>Beautiful nature landscapes for your inspiration.</p> <a href="#" class="btn">View More</a> </div> </div> <div class="card"> <img src="https://picsum.photos/400/300?city" alt="City"> <div class="card-content"> <h3>City Card</h3> <p>Urban landscapes and city architecture.</p> <a href="#" class="btn">View More</a> </div> </div> </div> </body> </html>
Result:
CSS Specificity with Embedded Styles
Understanding how embedded CSS interacts with other styling methods:
Selector Type | Specificity Value | Example |
---|---|---|
Inline Styles | 1,0,0,0 | style="color: blue;" |
ID Selectors | 0,1,0,0 | #header { ... } |
Class Selectors | 0,0,1,0 | .highlight { ... } |
Element Selectors | 0,0,0,1 | p { ... } |
!important
sparingly to override styles when necessary.
When to Use Embedded CSS
While embedded CSS has its limitations, it's perfect for specific scenarios:
Single-Page Websites
Ideal for small websites with only one HTML page where all styles are contained.
Prototyping & Testing
Perfect for quickly testing styles without creating external files.
Performance Optimization
Reduces HTTP requests for small projects, improving load time.
Component-Based Styles
Useful for styling specific components in modern frameworks.
Best Practices for Embedded CSS
Follow these guidelines to use embedded CSS effectively:
Next Steps
Now that you understand embedded CSS, continue to the next lesson to learn about external stylesheets.