π MODULE 5 β Responsive Web Design
πΌοΈ Lesson 5.4 β Responsive Images
π Making Images Automatically Fit Different Screen Sizes
π― Learning Objectives
After completing this lesson, students will be able to:
β Understand Responsive Images
β Prevent Image Overflow
β Resize Images Automatically
β Use Width and Height Properly
β Use max-width Property
β Create Mobile-Friendly Image Layouts
β Build Professional Responsive Websites
π Introduction
Images are one of the most important parts of a website.
Examples:
β Website Logos
β Product Images
β College Photos
β Portfolio Images
β Blog Images
β Banners
Imagine adding a large image:
Width = 2000px
On a desktop it may look fine.
But on a mobile phone:
Screen Width = 400px
The image becomes larger than the screen.
Result:
β Horizontal Scroll
β Broken Layout
β Poor User Experience
To solve this problem we use:
πΌοΈ Responsive Images
π€ What is a Responsive Image?
A Responsive Image automatically adjusts its size according to the screen size.
Simple Definition
Responsive Images automatically
resize themselves on different devices.
Example
Desktop:
+----------------------+
| |
| IMAGE |
| |
+----------------------+
Mobile:
+------------+
| |
| IMAGE |
| |
+------------+
Image automatically becomes smaller.
π Why Responsive Images Are Important?
Without Responsive Images:
β Images overflow
β Layout breaks
β Mobile experience becomes poor
β Website looks unprofessional
With Responsive Images:
β Better Appearance
β Mobile Friendly
β Faster Loading
β Professional Design
π― Adding an Image in HTML
Syntax:
<img src="image.jpg">
Example
<img src="college.jpg">
Image appears on webpage.
π Fixed Width Image
Example:
img{
width:1000px;
}
Problem:
On mobile screens:
Screen Width = 400px
Image Width = 1000px
Result:
β Image overflows
π― Responsive Image Solution
Use:
img{
max-width:100%;
}
Meaning:
Image can never become wider
than its parent container.
Example
img{
max-width:100%;
height:auto;
}
This is the most common responsive image technique.
π Why Use Height Auto?
Example:
img{
height:auto;
}
Meaning:
Maintain original image proportions.
Without height:auto:
Image may become stretched.
Correct Responsive Image Code
img{
max-width:100%;
height:auto;
}
π― First Responsive Image Example
HTML
<img src="nature.jpg">
CSS
img{
max-width:100%;
height:auto;
}
Result
Desktop:
Large image.
Mobile:
Smaller image.
Automatically adjusts.
π Width 100%
Another common technique:
img{
width:100%;
}
Meaning:
Image occupies full available width.
Example
img{
width:100%;
height:auto;
}
Difference
width:100%
Always fills container.
max-width:100%
Only shrinks when necessary.
Professional developers usually prefer:
max-width:100%;
π― Responsive Image Container
HTML
<div class="container">
<img src="college.jpg">
</div>
CSS
.container{
width:80%;
margin:auto;
}
img{
max-width:100%;
height:auto;
}
Result
Responsive image inside responsive container.
π Responsive Banner Image
HTML
<img src="banner.jpg">
CSS
img{
width:100%;
height:auto;
}
Result
Banner stretches across available width.
π― Responsive Profile Image
HTML
<img src="student.jpg">
CSS
img{
width:200px;
height:200px;
border-radius:50%;
}
Creates circular profile image.
Responsive Version
img{
max-width:100%;
height:auto;
border-radius:50%;
}
π Responsive Gallery Layout
HTML
<div class="gallery">
<img src="img1.jpg">
<img src="img2.jpg">
<img src="img3.jpg">
</div>
CSS
.gallery{
display:grid;
grid-template-columns:1fr 1fr 1fr;
gap:20px;
}
.gallery img{
width:100%;
height:auto;
}
Result
Responsive image gallery.
π― Using Media Queries with Images
Desktop:
img{
width:500px;
}
Mobile:
@media (max-width:768px)
{
img{
width:100%;
}
}
Result
Desktop:
Large image.
Mobile:
Full width image.
π Complete Example
HTML
<div class="container">
<h1>
Our College
</h1>
<img src="college.jpg">
<p>
Welcome to our college website.
</p>
</div>
CSS
.container{
width:80%;
margin:auto;
}
img{
max-width:100%;
height:auto;
border-radius:10px;
}
Result
Professional responsive image section.
π» Complete Practical Program
HTML
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<h1>
Responsive Image Example
</h1>
<img src="nature.jpg">
<p>
Learning Responsive Images
</p>
</div>
</body>
</html>
CSS
body{
font-family:Arial;
}
.container{
width:80%;
margin:auto;
}
img{
max-width:100%;
height:auto;
border-radius:10px;
border:2px solid blue;
}
π― Expected Output
Responsive image:
β Automatically Resizes
β Mobile Friendly
β No Horizontal Scroll
β Professional Design
π« Real World Examples
Amazon Product Images
Responsive
Flipkart Product Images
Responsive
College Website Galleries
Responsive
News Website Images
Responsive
Portfolio Website Images
Responsive
β οΈ Common Beginner Mistakes
Mistake 1
Fixed Width Images
Wrong:
img{
width:1200px;
}
Result:
Layout breaks on mobile.
Mistake 2
Missing Height Auto
Wrong:
img{
width:100%;
height:300px;
}
Image becomes stretched.
Correct:
height:auto;
Mistake 3
Very Large Images
Uploading:
8000px Γ 6000px
Slows website.
Optimize images.
π Best Practices
β
Use max-width:100%.
β
Use height:auto.
β Compress large images.
β Test on mobile devices.
β Use responsive containers.
π Mini Project β Responsive College Banner
Create:
College Banner
Containing:
- College Image
- College Name
- Description
Apply:
max-width:100%;
height:auto;
π Practical Activity
Create:
Responsive Student Profile
Include:
- Student Photo
- Name
- Branch
Use:
img{
max-width:100%;
height:auto;
}
π Assignment
Assignment 5.4
- What is a Responsive Image?
- Why are Responsive Images important?
- What does
max-width:100%do? - Why is
height:autoimportant? - Create a responsive image webpage.
- Create a responsive image gallery.
- Explain the difference between
width:100%andmax-width:100%.
π§ Quiz
Q1. Which property makes images responsive?
A. width:1000px
B. max-width:100%
C. width:2000px
D. height:500px
β Answer: B
Q2. Which property maintains image proportions?
A. width:auto
B. height:auto
C. image:auto
D. resize:auto
β Answer: B
Q3. Responsive images help prevent?
A. Overflow
B. Layout Breaks
C. Horizontal Scroll
D. All of These
β Answer: D
Q4. Which property is commonly used for responsive images?
A. max-width:100%
B. width:3000px
C. height:1000px
D. image-fit
β Answer: A
Q5. Responsive images are important for?
A. Mobile Devices
B. Tablets
C. Desktops
D. All of These
β Answer: D
π Lesson Summary
β Responsive Images automatically adapt to different screen sizes.
β
max-width:100% prevents image overflow.
β
height:auto maintains image proportions.
β Responsive Images improve website usability and appearance.
β Every modern website uses responsive images.