04. Types of CSS (Inline, Internal, External & @import)
🟢 Beginner
1. Definition
CSS can be included in HTML documents via four distinct methods: Inline CSS (style="..." attribute), Internal CSS (<style> tag inside <head>), External CSS (separate .css file linked via <link>), and @import directives inside stylesheets.
2. Why It Is Used
Using the proper inclusion method determines maintainability, page load performance, browser caching, and selector specificity control. External CSS is the universal production gold standard.
3. How It Works
When the HTML parser encounters a <link rel="stylesheet"> tag in <head>, it initiates a parallel HTTP request for the external file. In contrast, @import blocks parallel downloading until parent stylesheets finish loading, slowing page performance.
4. Syntax
<!-- External Linking Method (Recommended) -->
<link rel="stylesheet" href="../assets/css/custom.css">
5. Inclusion Methods Comparison Table
| Method | Syntax Location | Reusability | Specificity | Production Recommendation |
|---|---|---|---|---|
| Inline CSS | style="..." on HTML tag | Single Element | Highest (1,0,0,0) | Avoid! High clutter & specificity |
| Internal CSS | <style> in HTML <head> | Single Page | Standard | Acceptable for single standalone files |
| External CSS | Separate .css file linked via <link> | Entire Website | Standard | Gold Standard Best Practice |
| @import | Inside CSS file | Stylesheet level | Depends on placement | Avoid! Blocks parallel downloads |
6. Beginner Example
<!-- Inline CSS Example -->
<h1 style="color: #2563eb; text-align: center;">Inline Styled Heading</h1>
7. Intermediate Example
<!-- Internal CSS Example -->
<head>
<style>
body { background-color: #f8fafc; }
.alert-box { padding: 15px; background-color: #fef3c7; border-left: 4px solid #f59e0b; }
</style>
</head>
8. Advanced Example (External Modular Link)
<head>
<link rel="stylesheet" href="../assets/css/base.css">
<link rel="stylesheet" href="../assets/css/layout.css">
<link rel="stylesheet" href="../assets/css/components.css">
</head>
9. Line-by-Line Code Explanation
<link rel="stylesheet" href="../assets/css/base.css">: Links modular CSS stylesheets in sequential order so resets load before layout rules.
10. Expected Terminal / Browser Output
A web page cleanly formatted by external stylesheets where changing one rule in layout.css updates thousands of pages simultaneously.
11. Common Mistakes & Pitfalls
- Using @import heavily: Nesting multiple
@importcalls inside CSS creates serial download waterfalls that delay page rendering.
12. Important Rules & Limitations
- Inline styles override class and ID rules regardless of stylesheet ordering due to highest specificity (1,0,0,0).
13. Multilingual Explanation: English
External CSS (using <link>) is the gold standard for production web applications because it enables browser caching and global reusability.
14. Multilingual Explanation: Hindi (Roman Script)
External CSS (.css file) sabse accha method hai kyunki ise ek baar likhkar poori website par reuse kiya ja sakta hai aur browser ise cache kar leta hai.
15. Multilingual Explanation: Marathi (Roman Script)
External CSS file (.css) vaparne sarvat yogya maantle jaate. Yabaddal mule sarva pages ekch stylesheet dware style karta येतात.
16. Practice Questions
- Why is External CSS preferred over Inline CSS for production websites?
- What is the performance drawback of using
@importinside CSS files?
17. Practical Coding Challenge
Convert an internal <style> block containing button styles into an external css/components.css file and link it to index.html.