HTML Images
Images are an essential part of web pages. They make websites visually appealing, convey information quickly, and improve user experience.we use the <img>
tag to show images on a web page. It doesn’t need a closing tag
HTML Image Syntax
Explanation of Attributes:
- src (Source): Defines the image location (file path or URL).
- alt (Alternative text): Describes the image if it cannot load; also used by screen readers for accessibility.
- width and height: Defines the size of the image (in pixels or percentages).
- Self-closing tag:
<img>
does not require</img>
.
Inserting an Image
To insert an image in HTML, we use the <img>
tag. The most important attribute is src, which tells the browser where the image file is located, and alt, which provides text if the image can’t load.
Example
Notes:
- Image names are case-sensitive.
- Always use the correct file extension (.jpg, .png, .gif, etc.).
<img>
is an empty tag (no closing tag).
Image Path (Location)
When adding an image in HTML, the src attribute tells the browser where to find the image file. This is called the image path.
- If the image is in the same folder as your HTML file, just write the file name:
- If the image is inside another folder, write the folder name with the file:
- For images on the internet, use the full URL:
Setting Image Size
In HTML, you can control how big or small an image looks by setting its width and height. These can be written in pixels (px) or as a percentage (%) of the page.
Tip: It’s better to use CSS for resizing, because it gives you more control and keeps your HTML cleaner:
Adding Border to Image
In older versions of HTML, people used the border attribute inside the tag to add borders around images. But today, that method is outdated. The modern and recommended way is to use CSS for styling borders, because it’s more flexible and keeps design separate from content.
Old way:
Modern way (CSS):
Image Alignment
In modern HTML, if you want to control where an image appears on a page, you use CSS. There are two common ways:
- float → lets the image move to the left or right, and the text will wrap around it.
- text-align → is used on the parent element (like a div) to center, left, or right-align the image.
<p>This text will wrap around the image.</p>
Or center using CSS
<img src="cat.jpg" alt="Centered Cat" />
</div>
Animated Images
HTML allows you to show animated images such as GIFs in the same way you add normal images. A GIF plays automatically when loaded and doesn’t need any special code.
Tip: You can also use CSS animations or animated SVGs for smoother and more customizable effects, but GIFs are the easiest way to add simple animations.
Responsive Images
Responsive images adjust size automatically based on screen size.
Image as a Link
You can make an image clickable:
<img src="google.png" alt="Google Logo" width="120" />
</a>
Image Maps (Clickable Areas)
You can define clickable areas inside one image.
<map name="worldmap">
<area shape="rect" coords="34,44,270,350" href="usa.html" alt="USA"/>
<area shape="circle" coords="477,300,100" href="europe.html" alt="Europe"/>
</map>
Background Images (CSS)
Instead of <img>
, you can set an image as a background.
<h2>Text on background</h2>
</div>