MODULE 3 β CSS Fundamentals
π Lesson 3.4 β External CSS
π¨ Creating Professional Websites Using Separate CSS Files
π― Learning Objectives
After completing this lesson, students will be able to:
β Understand External CSS
β Create Separate CSS Files
β Link CSS Files with HTML
β Style Multiple Webpages
β Organize Code Professionally
β Understand Industry Standard Web Development
π Introduction
In the previous lessons, we learned:
Inline CSS
<h1 style="color:red;">
Welcome
</h1>
CSS written inside HTML.
Internal CSS
<style>
h1{
color:red;
}
</style>
CSS written inside the same HTML file.
Both methods work.
But professional websites may have:
- Hundreds of Pages
- Thousands of Elements
- Complex Designs
Managing CSS inside HTML becomes difficult.
To solve this problem we use:
π External CSS
π€ What is External CSS?
External CSS means:
Writing CSS in a separate file.
Example:
index.html
and
style.css
HTML contains content.
CSS file contains design.
π Why Use External CSS?
Advantages:
β Cleaner Code
β Easier Maintenance
β Reusable CSS
β Professional Method
β Faster Development
ποΈ Project Structure
Example:
Website Folder
β
βββ index.html
β
βββ style.css
π Step 1 β Create HTML File
Create:
index.html
Example
<!DOCTYPE html>
<html>
<head>
<title>External CSS Example</title>
</head>
<body>
<h1>
NextGen Dev Hub
</h1>
<p>
Learning External CSS
</p>
</body>
</html>
π Step 2 β Create CSS File
Create:
style.css
Example
h1{
color:blue;
}
p{
color:green;
}
π Step 3 β Link CSS File
Inside HTML Head Section:
<link
rel="stylesheet"
href="style.css">
Complete HTML File
<!DOCTYPE html>
<html>
<head>
<title>External CSS Example</title>
<link
rel="stylesheet"
href="style.css">
</head>
<body>
<h1>
NextGen Dev Hub
</h1>
<p>
Learning External CSS
</p>
</body>
</html>
Understanding Link Tag
<link
rel="stylesheet"
href="style.css">
rel
rel="stylesheet"
Means:
This file contains CSS.
href
href="style.css"
Means:
Location of CSS file.
π― How External CSS Works
HTML File
β
Browser Reads HTML
β
Browser Finds CSS Link
β
Loads CSS File
β
Applies Styles
β
Displays Styled Webpage
π First External CSS Example
HTML:
<h1>
Welcome Students
</h1>
CSS:
h1{
color:red;
}
Output
Red Heading
π¨ Styling Multiple Elements
CSS:
h1{
color:blue;
font-size:40px;
text-align:center;
}
p{
color:green;
font-size:20px;
}
HTML:
<h1>
NextGen Dev Hub
</h1>
<p>
Learning Web Designing
</p>
Output
Blue Heading
Green Paragraph
π Styling Entire Webpage
CSS:
body{
background-color:lightyellow;
}
Result
Entire webpage gets light yellow background.
Example
body{
background-color:lightgray;
}
π― Complete Example Project
Β
index.html
<!DOCTYPE html>
<html>
<head>
<title>Student Profile</title>
<link
rel="stylesheet"
href="style.css">
</head>
<body>
<h1>
Rahul Kumar
</h1>
<p>
Polytechnic Student
</p>
</body>
</html>
style.css
body{
background-color:lightyellow;
}
h1{
color:blue;
text-align:center;
}
p{
color:green;
font-size:20px;
}
Β
π― Expected Output
Light Yellow Background
Blue Centered Heading
Green Paragraph
π One CSS File for Multiple Pages
Example:
Website
β
βββ index.html
βββ about.html
βββ contact.html
β
βββ style.css
All pages use:
<link
rel="stylesheet"
href="style.css">
Benefit
Change CSS once.
Entire website updates automatically.
π« Real World Example
Large websites like:
- Amazon
- YouTube
use External CSS.
Because they contain thousands of pages.
π Comparing CSS Types
| Feature | Inline | Internal | External |
|---|---|---|---|
| Easy for Beginners | β | β | β |
| Reusable | β | β | β |
| Professional | β | β οΈ | β |
| Best for Large Websites | β | β | β |
| Easy Maintenance | β | β οΈ | β |
π» Complete Practical Example
index.html
<!DOCTYPE html>
<html>
<head>
<title>My Website</title>
<link
rel="stylesheet"
href="style.css">
</head>
<body>
<h1>
My First Website
</h1>
<p>
Learning External CSS
</p>
</body>
</html>
style.css
body{
background-color:lightblue;
}
h1{
color:white;
background-color:blue;
text-align:center;
}
p{
color:black;
font-size:20px;
}
β οΈ Common Beginner Mistakes
Mistake 1
Wrong CSS File Name
HTML:
href="style.css"
Actual File:
styles.css
Result:
CSS will not work.
Mistake 2
Placing CSS File in Wrong Folder
HTML cannot find the file.
Mistake 3
Forgetting Link Tag
Wrong:
<head>
<title>Website</title>
</head>
Correct:
<head>
<title>Website</title>
<link
rel="stylesheet"
href="style.css">
</head>
π Advantages of External CSS
β Professional
β Reusable
β Easy Maintenance
β Faster Development
β Used in Industry
β Disadvantages
β Requires separate file
β Beginners may forget linking
π Best Practices
β Keep CSS in separate files.
β Use meaningful file names.
β Place CSS inside a dedicated folder.
β Use External CSS for projects.
π Mini Project β Student Profile Website
Create:
index.html
<h1>
Your Name
</h1>
<p>
Your College
</p>
Create:
style.css
Apply:
β Blue Heading
β Green Paragraph
β Light Yellow Background
π Practical Activity
Create:
StudentWebsite
β
βββ index.html
β
βββ style.css
Display:
β Name
β College
β Branch
β Career Goal
Apply styles using External CSS.
π Assignment
Assignment 3.4
- What is External CSS?
- Why is External CSS used?
- Which tag links CSS files?
- What does
hrefmean? - What does
rel="stylesheet"mean? - Create an HTML file and CSS file.
- Link them together.
π§ Quiz
Q1. Which tag links External CSS?
A. <css>
B. <style>
C. <link>
D. <script>
β Answer: C
Q2. Which attribute specifies the CSS file location?
A. src
B. href
C. link
D. file
β Answer: B
Q3. Which CSS method is most professional?
A. Inline CSS
B. Internal CSS
C. External CSS
D. All Same
β Answer: C
Q4. Where is CSS written in External CSS?
A. HTML Body
B. HTML Head
C. Separate CSS File
D. Paragraph
β Answer: C
Q5. Which file extension is used for CSS?
A. .html
B. .js
C. .css
D. .txt
β Answer: C
π Lesson Summary
β
External CSS is written in a separate .css file.
β HTML and CSS remain separate.
β
CSS files are linked using the <link> tag.
β External CSS is the professional and industry-standard method.
β One CSS file can style multiple webpages.
β External CSS makes websites easier to maintain.
Β