HTML <img> src
Property: Specifying the Image Source URL
The src
attribute of the HTML <img>
tag is fundamental for displaying images on a webpage. It specifies the URL of the image you want to embed. This URL can point to an image file on the same server as the HTML document, or it can reference an image hosted on a different server. Without the src
attribute, the <img>
tag is essentially useless, as it has no image to display.
Purpose of the src
Attribute
The primary purpose of the src
attribute is to tell the browser where to find the image file. The browser then fetches this file and renders the image within the space allocated by the <img>
tag. 🖼️
Syntax
The syntax for using the src
attribute is straightforward:
<img src="URL" alt="description">
URL
: A string representing the absolute or relative URL of the image file.alt
: (Required) Specifies an alternate text for the image, if the image for some reason cannot be displayed.
Attribute Details
Attribute | Value | Description |
---|---|---|
`src` | URL (absolute or relative) | Specifies the URL of the image file to be displayed. |
Examples
Let’s explore various examples of how to use the src
attribute effectively.
Basic Example: Using a Relative URL
In this example, the src
attribute points to an image file located in the same directory as the HTML file.
<!DOCTYPE html>
<html>
<head>
<title>HTML img src Example</title>
</head>
<body>
<h1>Using a Relative URL</h1>
<img src="images/relative-image.jpg" alt="A beautiful landscape">
</body>
</html>
Note: For this to work, you need to have an images
folder in the same directory as your HTML file, and it should contain an image named relative-image.jpg
. 💡
Basic Example: Using an Absolute URL
In this example, the src
attribute points to an image file located on a different website.
<!DOCTYPE html>
<html>
<head>
<title>HTML img src Example</title>
</head>
<body>
<h1>Using an Absolute URL</h1>
<img src="https://dummyimage.com/400x200/007bff/fff&text=Absolute+URL" alt="A placeholder image">
</body>
</html>
Output:
The img
tag will display a placeholder image.
Using the src
Attribute with Different Image Formats
The src
attribute can be used with various image formats, such as JPEG, PNG, GIF, and WebP. Here’s an example using a PNG image:
<!DOCTYPE html>
<html>
<head>
<title>HTML img src Example</title>
</head>
<body>
<h1>Using a PNG Image</h1>
<img src="images/my-image.png" alt="A transparent PNG image">
</body>
</html>
Note: Ensure that the image format is supported by web browsers. JPEG, PNG, GIF, and WebP are widely supported. 🤔
Using the src
Attribute with Data URLs
Data URLs allow you to embed the image data directly into the HTML, eliminating the need for a separate image file.
<!DOCTYPE html>
<html>
<head>
<title>HTML img src Example</title>
</head>
<body>
<h1>Using a Data URL</h1>
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w+r8v///fwY0ABZEdFOFjRE7AAAAAElFTkSuQmCC" alt="A small embedded image">
</body>
</html>
Output:
The img
tag will display a small embedded image.
Note: While data URLs can be useful for small images, they increase the size of your HTML file and can impact performance for larger images. Avoid using data urls for large images. ⚠️
Handling Image Loading Errors
It’s important to handle cases where the image fails to load. The alt
attribute provides alternate text when the image cannot be displayed.
<!DOCTYPE html>
<html>
<head>
<title>HTML img src Example</title>
</head>
<body>
<h1>Handling Image Loading Errors</h1>
<img src="nonexistent-image.jpg" alt="Image failed to load">
</body>
</html>
Output:
If the image fails to load, the alt
text “Image failed to load” will be displayed.
Lazy Loading Images
Lazy loading defers the loading of images until they are about to enter the viewport, improving page load performance. Use the loading
attribute with a value of “lazy”.
<!DOCTYPE html>
<html>
<head>
<title>HTML img src Example</title>
</head>
<body>
<h1>Lazy Loading Images</h1>
<img src="large-image.jpg" alt="A large image" loading="lazy">
</body>
</html>
Note: Lazy loading can significantly improve the initial page load time, especially for pages with many images. 🚀
Tips and Best Practices
- Optimize Images: Use optimized images to reduce file size and improve loading times. Tools like TinyPNG or ImageOptim can help.
- Use Responsive Images: Use the
<picture>
element or thesrcset
attribute to provide different image sizes for different screen resolutions. - Provide
alt
Text: Always include descriptivealt
text for accessibility and SEO. - Test Image Paths: Double-check that your image paths are correct to avoid broken images.
- Use a CDN: Consider using a Content Delivery Network (CDN) to host and deliver your images for better performance, especially for websites with global audiences.
Conclusion
The src
attribute is a crucial part of the <img>
tag, allowing you to display images on your webpage. By understanding how to use relative and absolute URLs, different image formats, and best practices for image optimization, you can create visually appealing and performant web experiences. 🖼️💻