Metadata
- Date :: 13-04-2025
- Tags :: web-dev
Notes
π¨ Three Ways to Add CSS to HTML
π Introduction
Once youβre comfortable with HTML, the next step in web development is to learn how to style your web pages using CSS (Cascading Style Sheets).
There are three different methods of adding CSS to your HTML documents:
-
Inline CSS
-
Internal CSS
-
External CSS
Each has its own use case, advantages, and limitations. Letβs dive into each method in detail.
πΉ 1. Inline CSS
π What is Inline CSS?
Inline CSS is written directly inside the HTML elementβs opening tag using the style attribute.
π Syntax
<tagname style="property: value;">
Content goes here
</tagname>β Example:
<h1 style="color: blue;">This is a blue heading</h1>This applies the style only to this specific h1 element. No other h1 will be affected.
π‘ When to Use Inline CSS?
-
For quick testing or debugging
-
When applying style to a single unique element
-
When JavaScript dynamically injects style
-
In emails where external CSS is often not supported
β Why Itβs Not Ideal for Large Projects
-
Not scalable: Repeating styles across many elements becomes tedious.
-
Clutters HTML: Makes code messy and harder to read.
-
Hard to maintain: Difficult to update styles later.
πΈ 2. Internal CSS
π What is Internal CSS?
Internal CSS is written within the <style> tag inside the <head> section of the HTML document.
π Syntax
<head>
<style>
selector {
property: value;
}
</style>
</head>β Example:
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
color: red;
font-size: 30px;
}
</style>
</head>
<body>
<h1>This heading is red</h1>
</body>
</html>π§ Breakdown
-
Selector: Targets elements (e.g.,
h1,p,.class,#id) -
Curly Braces
{}: Enclose the CSS rules -
Property: What you want to change (e.g., color)
-
Value: What you want to set it to (e.g., red)
π‘ When to Use Internal CSS?
-
For single-page websites
-
When experimenting with styling during development
-
When styles are not reused across pages
β Limitations
-
Styles only apply to the current HTML document
-
Not reusable across multiple web pages
-
Can still clutter the head section with large amounts of CSS
πΉ 3. External CSS
π What is External CSS?
External CSS is written in a separate .css file and linked to the HTML file using a <link> tag in the <head> section.
π Linking Syntax
<head>
<link rel="stylesheet" href="styles.css">
</head>This tells the browser:
βHey, get the styling from this separate CSS file!β
β Example of styles.css
h1 {
color: green;
font-family: Arial, sans-serif;
}β Example HTML
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>This heading is green</h1>
</body>
</html>π§ How It Works
-
The CSS rules live in a
.cssfile -
You link that file to your HTML
-
The browser applies the styles to your HTML content
π‘ When to Use External CSS?
β
Ideal for multi-page websites
β
Allows for modular code and clean HTML
β
Makes it easier to maintain consistency across all pages
β
Best for real-world, production-grade projects
π§Ύ Typical File Structure:
project-folder/
β
βββ index.html
βββ about.html
βββ contact.html
βββ styles.css
All HTML files link to the same styles.css for consistency.
π Practice Challenge Overview
In the practice exercise, youβll work with a small website project that contains:
-
A home page with links to 3 subpages:
-
inline.html -
internal.html -
external.html
-
Each subpage uses one of the 3 styling methods:
| Page | Styling Method | Expected Color |
|---|---|---|
inline.html | Inline CSS | Blue |
internal.html | Internal CSS | Red |
external.html | External CSS | Green |
π Steps Recap:
-
Home Page (
index.html):-
Add 3 anchor (
<a>) links to each of the subpages.
<a href="./inline.html">Inline</a> <a href="./internal.html">Internal</a> <a href="./external.html">External</a> -
-
Inline Styling Page:
-
Add style directly to the
h1tag:
<h1 style="color: blue;">Style me in blue</h1> -
-
Internal Styling Page:
-
Use
<style>in the<head>:
<style> h1 { color: red; } </style> -
-
External Styling Page:
-
Create
styles.cssand link it:
/* styles.css */ h1 { color: green; }<!-- external.html --> <head> <link rel="stylesheet" href="styles.css"> </head> -
π Summary Table
| Method | Where CSS Lives | Scope | Best For | Example Use |
|---|---|---|---|---|
| Inline | Inside element tag | One specific element | Testing, emails, one-off tweaks | <h1 style="color:blue;"> |
| Internal | Inside <style> in HTML | One HTML document | Small sites or specific page style | <style> h1 { color: red; } </style> |
| External | In a .css file | Whole website | Scalable, multi-page websites | styles.css + <link> tag |
π§ Final Thoughts
-
Youβll encounter all three methods in real-world scenarios.
-
The best practice for professional projects is to use external CSS for clean, maintainable, and scalable code.
-
Mastering the three ways prepares you for:
-
Reading legacy code
-
Testing effectively
-
Organizing your own projects
-