HTML provides the ability to display images on web pages, enhancing the visual appeal and communication of content. Images are embedded in HTML using the
<img>
tag, which is self-closing and requires attributes to define the source, alternative text, dimensions, and more.
<img>
Tag:
<img>
Tag: The primary tag for embedding images.src
: Specifies the path to the image.alt
: Provides alternative text for accessibility.width
and height
: Define the size of the image.title
: Adds a tooltip to the image.alt
text ensures images are accessible to screen readers.Embedding a simple image with mandatory attributes.
<!DOCTYPE html>
<html>
<head>
<title>Basic Image Example</title>
</head>
<body>
<h1>Welcome to Our Gallery</h1>
<img src="landscape.jpg" alt="A beautiful landscape with mountains and a river" width="600" height="400">
<p>This is a serene landscape showcasing the beauty of nature.</p>
</body>
</html>
Output:
A 300x200 pixel image of a landscape will be displayed on the webpage.
<!DOCTYPE html>
<html>
<head>
<title>Responsive Image Example</title>
</head>
<body>
<h1>Explore the Universe</h1>
<img
src="space-small.jpg"
srcset="space-small.jpg 480w, space-medium.jpg 800w, space-large.jpg 1200w"
sizes="(max-width: 600px) 480px, (max-width: 1024px) 800px, 1200px"
alt="A breathtaking view of the galaxy">
<p>The image adjusts based on screen size for an optimal experience.</p>
</body>
</html>
A landscape image with the specified dimensions (600x400 pixels).
Output:
None, as images are visual.
srcset
Responsive images adapt their size and resolution to fit different devices.
<!DOCTYPE html>
<html>
<head>
<title>Responsive Image Example</title>
</head>
<body>
<h1>Explore the Universe</h1>
<img
src="space-small.jpg"
srcset="space-small.jpg 480w, space-medium.jpg 800w, space-large.jpg 1200w"
sizes="(max-width: 600px) 480px, (max-width: 1024px) 800px, 1200px"
alt="A breathtaking view of the galaxy">
<p>The image adjusts based on screen size for an optimal experience.</p>
</body>
</html>
Provides multiple image versions for varying screen sizes.
sizes
:
Defines the image's display width based on the viewport.
Output:
None, as this example adapts images visually.
<img>
vs Background Images in CSS
Aspect |
| CSS Background Images | ||||||
---|---|---|---|---|---|---|---|---|
|
|
| ||||||
Accessibility |
|
| ||||||
|
|
| ||||||
|
|
| ||||||
<!DOCTYPE html>
<html>
<head>
<title>Background Image Example</title>
<style>
.banner {
background-image: url('sunrise.jpg');
background-size: cover;
background-position: center;
width: 100%;
height: 400px;
display: flex;
align-items: center;
justify-content: center;
color: white;
font-size: 2em;
}
</style>
</head>
<body>
<div class="banner">
Welcome to the Sunrise View
</div>
</body>
</html>
The
sunrise.jpg
image spans the entire div, creating an eye-catching banner.
background-size: cover;
ensures the image covers the entire element without distortion.
Output:
None, as this involves CSS styling.
Image maps allow users to interact with different areas of an image, linking to different destinations.
<!DOCTYPE html>
<html>
<head>
<title>Image Map Example</title>
</head>
<body>
<h1>Interactive World Map</h1>
<img src="world-map.jpg" alt="World Map" usemap="#worldmap" width="800" height="400">
<map name="worldmap">
<area shape="rect" coords="100,50,200,100" href="https://www.usa.gov" alt="USA">
<area shape="circle" coords="300,150,50" href="https://www.uk.gov" alt="UK">
<area shape="poly" coords="500,200,600,250,550,300" href="https://www.india.gov.in" alt="India">
</map>
</body>
</html>
The
<area>
tags define regions on the image linked to different URLs.
Supports
rect
(rectangle),
circle
, and
poly
(polygon)
Useful for maps, diagrams, and infographics.
Output:
None, as this is interactive.
Lazy loading defers the loading of offscreen images, improving page load time.
<!DOCTYPE html>
<html>
<head>
<title>Lazy Loading Images</title>
</head>
<body>
<h1>Lazy Loading Example</h1>
<img src="placeholder.jpg" data-src="high-res.jpg" alt="High Resolution Example" class="lazy" width="800" height="600">
<script>
document.addEventListener("DOMContentLoaded", () => {
const lazyImages = document.querySelectorAll('.lazy');
lazyImages.forEach(image => {
const observer = new IntersectionObserver(entries => {
entries.forEach(entry => {
if (entry.isIntersecting) {
image.src = image.dataset.src;
observer.unobserve(image);
}
});
});
observer.observe(image);
});
});
</script>
</body>
</html>
Images load only when they come into the viewport.
Reduces initial page load time and bandwidth usage.
Output:
None; dynamic script handles the lazy loading behavior.
This chapter covers HTML Images in-depth, including concepts, practical examples, and advanced features. Feel free to ask for expansions or additional topics!
Newsletter
Subscribe to our newsletter for weekly updates and promotions.
Wiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesAds Policies