HTML Tutorial
HTML XHTML
HTML Media
HTML Advanced
Here's a tutorial on HTML images:
Images are an important part of many web pages, and HTML (Hypertext Markup Language) provides several elements for adding images to a web page.
The most commonly used element for adding images to a web page is the <img>
element. The <img>
element is a self-closing element, which means that it doesn't have a closing tag. Instead, you can use the src
attribute to specify the URL of the image you want to display.
Here's an example of how to use the <img>
element to add an image to a web page:
<img src="image.jpg" alt="A beautiful sunset">
In this example, the src
attribute is used to specify the URL of the image file "image.jpg". The alt
attribute is used to provide a text description of the image for users who can't see the image, either because of a slow internet connection, a broken link, or a screen reader.
It's important to use descriptive and meaningful text for the alt
attribute to ensure accessibility and improve the user experience.
In addition to the src
and alt
attributes, the <img>
element also supports several other attributes, such as width
, height
, title
, and style
. These attributes can be used to control the size, alignment, and appearance of the image.
Here are some examples of how to use these attributes:
<img src="image.jpg" alt="A beautiful sunset" width="500" height="300"> <img src="image.jpg" alt="A beautiful sunset" title="Click to enlarge"> <img src="image.jpg" alt="A beautiful sunset" style="float: right; margin: 10px;">
In these examples, the width
and height
attributes are used to set the dimensions of the image, the title
attribute is used to provide a tooltip when the user hovers over the image, and the style
attribute is used to apply CSS styles to the image, such as floating it to the right and adding a margin.
By using the <img>
element and its attributes, you can add images to your web page and control their size, alignment, and appearance to create a visually appealing and engaging user experience.
How to display images in HTML:
Use the <img>
element to display images in HTML.
<img src="image.jpg" alt="Description of the image">
HTML image source attribute:
The src
attribute specifies the source (URL or file path) of the image.
<img src="image.jpg" alt="Description">
Adding alt text to HTML images:
The alt
attribute provides alternative text for accessibility and is displayed if the image cannot be loaded.
<img src="image.jpg" alt="Description of the image">
HTML image size attributes:
Use the width
and height
attributes to set the size of the image.
<img src="image.jpg" alt="Description" width="300" height="200">
Responsive images in HTML:
Set the max-width
property to make images responsive.
<img src="image.jpg" alt="Description" style="max-width: 100%;">
HTML image alignment:
Use the align
attribute to specify the alignment of the image.
<img src="image.jpg" alt="Description" align="left">
Linking images in HTML:
Wrap the <img>
element with an <a>
tag to make the image a clickable link.
<a href="destination.html"><img src="image.jpg" alt="Description"></a>
HTML image borders:
Apply borders to images using the border
attribute or CSS.
<img src="image.jpg" alt="Description" style="border: 1px solid black;">
Styling images with CSS in HTML: Apply CSS styles to images for enhanced styling.
<style> img { border-radius: 10px; } </style>
HTML image maps: Use image maps to define clickable areas within an image.
<img src="planets.jpg" alt="Solar System" usemap="#solarsystem"> <map name="solarsystem"> <area shape="circle" coords="90,58,3" href="mercury.html" alt="Mercury"> <!-- Other areas --> </map>
Lazy loading images in HTML:
Use the loading
attribute to enable lazy loading.
<img src="image.jpg" alt="Description" loading="lazy">
HTML image captions:
Wrap the image and caption in a <figure>
and <figcaption>
for semantic markup.
<figure> <img src="image.jpg" alt="Description"> <figcaption>Caption for the image.</figcaption> </figure>
HTML background images:
Set background images using the background
property in CSS.
<style> body { background: url("background.jpg") no-repeat center center fixed; background-size: cover; } </style>