MODULE 3 β CSS Fundamentals
π Lesson 3.3 β Internal CSS
π¨ Styling Multiple HTML Elements Using the <style> Tag
π― Learning Objectives
After completing this lesson, students will be able to:
β Understand Internal CSS
β
Use the <style> Tag
β Apply CSS to Multiple Elements
β Understand CSS Selectors
β Style Entire Webpages Easily
β Create Cleaner HTML Code
β Compare Inline CSS and Internal CSS
π Introduction
In the previous lesson, we learned:
ποΈ Inline CSS
Example:
<h1 style="color:red;">
Welcome Students
</h1>
This works well for small webpages.
But imagine a webpage with:
- 20 Headings
- 30 Paragraphs
- 10 Buttons
Writing:
style=""
again and again becomes difficult.
To solve this problem we use:
π Internal CSS
π€ What is Internal CSS?
Internal CSS is CSS written inside the:
<style>
tag.
The <style> tag is placed inside:
<head>
section.
π Structure of Internal CSS
<!DOCTYPE html>
<html>
<head>
<style>
CSS Code
</style>
</head>
<body>
HTML Code
</body>
</html>
π― Why Use Internal CSS?
Advantages:
β Cleaner Code
β Easy Maintenance
β Style Multiple Elements
β More Professional
β Better Organization
π First Internal CSS Example
<!DOCTYPE html>
<html>
<head>
<style>
h1{
color:red;
}
</style>
</head>
<body>
<h1>
Welcome Students
</h1>
</body>
</html>
Understanding the Code
CSS:
h1{
color:red;
}
Meaning:
All h1 headings become red.
Output
Red Heading
π― CSS Selector
In Internal CSS:
h1{
color:red;
}
The word:
h1
is called:
Selector
What is a Selector?
A selector tells CSS:
Which HTML element should be styled.
Example
p{
color:blue;
}
Meaning:
Apply blue color to all paragraphs.
π Styling Multiple Elements
Example:
<!DOCTYPE html>
<html>
<head>
<style>
h1{
color:red;
}
p{
color:blue;
}
</style>
</head>
<body>
<h1>Welcome</h1>
<p>Learning CSS</p>
</body>
</html>
Output
Heading β Red
Paragraph β Blue
π¨ Adding Background Color
Example:
h1{
background-color:yellow;
}
Result
Yellow background behind heading.
Example
p{
background-color:lightblue;
}
π Changing Font Size
Example
h1{
font-size:40px;
}
Meaning
Heading size = 40 pixels
Example
p{
font-size:20px;
}
π― Text Alignment
Example
h1{
text-align:center;
}
Result
Heading appears in center.
π Multiple Properties Together
Example:
h1{
color:white;
background-color:blue;
font-size:40px;
text-align:center;
}
Result
β White Text
β Blue Background
β Large Font
β Centered
π» Complete Example
<!DOCTYPE html>
<html>
<head>
<style>
h1{
color:white;
background-color:blue;
font-size:40px;
text-align:center;
}
p{
color:green;
font-size:20px;
}
</style>
</head>
<body>
<h1>
NextGen Dev Hub
</h1>
<p>
Learning Internal CSS
</p>
</body>
</html>
π― Expected Output
Blue Heading
White Text
Large Font
Green Paragraph
π Styling Multiple Headings
HTML
<h1>HTML</h1>
<h1>CSS</h1>
<h1>JavaScript</h1>
CSS
h1{
color:red;
}
Output
All headings become red.
Why Internal CSS is Better Than Inline CSS?
Inline CSS:
<h1 style="color:red;">
HTML
</h1>
<h1 style="color:red;">
CSS
</h1>
<h1 style="color:red;">
JavaScript
</h1>
Repeated code.
Internal CSS:
h1{
color:red;
}
Cleaner and easier.
π― Styling the Entire Body
The webpage body can also be styled.
Example:
body{
background-color:lightyellow;
}
Result
Entire webpage background becomes light yellow.
Example
body{
background-color:lightgray;
}
π Complete Student Profile Example
<!DOCTYPE html>
<html>
<head>
<style>
body{
background-color:lightyellow;
}
h1{
color:blue;
text-align:center;
}
p{
color:green;
font-size:20px;
}
</style>
</head>
<body>
<h1>
Rahul Kumar
</h1>
<p>
Polytechnic Student
</p>
</body>
</html>
π« Real World Example
College Website
body{
background-color:#f5f5f5;
}
h1{
color:darkblue;
}
p{
color:black;
}
β οΈ Common Beginner Mistakes
Mistake 1
Placing Style Tag Inside Body
Wrong:
<body>
<style>
h1{
color:red;
}
</style>
</body>
Correct:
<head>
<style>
h1{
color:red;
}
</style>
</head>
Mistake 2
Forgetting Curly Brackets
Wrong:
h1
color:red;
Correct:
h1{
color:red;
}
Mistake 3
Missing Semicolon
Wrong:
color:red
Correct:
color:red;
π Advantages of Internal CSS
β Cleaner Code
β Easy to Manage
β Styles Multiple Elements
β More Professional
β Suitable for Medium Projects
β Disadvantages
β CSS cannot be reused on multiple webpages.
β Large projects become difficult.
(External CSS solves this problem.)
π Best Practices
β
Write CSS inside <style> tag.
β
Place <style> inside <head>.
β Use meaningful colors.
β Keep code organized.
π Mini Project β Student Information Page
Create:
<h1>
Your Name
</h1>
<p>
Your College
</p>
<p>
Your Career Goal
</p>
Apply:
β Blue Heading
β Green Paragraphs
β Light Yellow Background
π Practical Activity
Create a webpage containing:
Your Name
Blue Color
Centered
College Name
Green Color
Branch
Orange Color
Career Goal
Purple Color
Apply:
body{
background-color:lightyellow;
}
π Assignment
Assignment 3.3
- What is Internal CSS?
- Which tag is used for Internal CSS?
- Where should the style tag be placed?
- What is a selector?
- Which property changes text color?
- Which property changes background color?
- Create a webpage using Internal CSS.
π§ Quiz
Q1. Which tag is used for Internal CSS?
A. <css>
B. <style>
C. <design>
D. <script>
β Answer: B
Q2. Internal CSS is usually written inside:
A. Body
B. Footer
C. Head
D. Paragraph
β Answer: C
Q3. Which selector styles all paragraphs?
A. body
B. h1
C. p
D. img
β Answer: C
Q4. Which property changes text color?
A. font
B. background
C. color
D. size
β Answer: C
Q5. Which property centers text?
A. align
B. center
C. text-align
D. position
β Answer: C
π Lesson Summary
β
Internal CSS is written inside the <style> tag.
β
The <style> tag is placed inside the <head> section.
β Internal CSS can style multiple HTML elements at once.
β Selectors identify which elements to style.
β Internal CSS is cleaner and more professional than Inline CSS.