Home 〉 HTML Tutorials 〉 HTML img Tag | Complete Guide to HTML Image Tag with Syntax & Attributes
HTML img Tag | Complete Guide to HTML Image Tag with Syntax & Attributes !
What is the correct way to use images in HTML?
In this detailed blog post on the HTML img tag, we learned its syntax, all key attributes, inline styling methods, and practical examples. This tag is an inline, empty HTML element used to display images from a source path. Proper use of attributes like 'alt' and 'loading' ensures better performance, accessibility, and SEO. Additional topics like the '<picture>' tag and image optimization were also covered for enhanced learning. The complete blog post on HTML img tag is as follows.
In HTML, the '<img>' tag is used to embed images in a web page. Images are a vital part of modern websites, enhancing visual appeal and improving user experience. The '<img>' tag does not require a closing tag and it helps in displaying images from a given source ('src').
Syntax ✍
<img src="image.jpg" alt="Description of image">
The '<img>' tag is an empty and inline-level element. It does not have any content between opening and closing tags, and it behaves inline within text.
Let's explore the commonly used attributes of the '<img>' tag:
Specifies the path or URL of the image.
<!DOCTYPE html>
<html>
<body>
<img src="/images/image.jpg" alt="Image of a Garden">
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
Specifies alternate text for the image if it fails to load.
<!DOCTYPE html>
<html>
<body>
<img src="not-found.png" alt="Image not found">
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
Displays "Image not found" text if the image cannot be loaded.
Sets the width and height of the image in pixels.
<!DOCTYPE html>
<html>
<body>
<img src="/images/image.jpg" alt="Beautiful Garden" width="200" height="150">
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
Displays the image with dimensions 200px by 150px.
Shows a tooltip when the user hovers over the image.
<!DOCTYPE html>
<html>
<body>
<img src="/images/image.jpg" alt="Garden Image" title="The Beautiful Garden">
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
A tooltip appears when the user hovers over the image.
Determines how the image should load. Values: 'lazy', 'eager'.
<!DOCTYPE html>
<html>
<body>
<img src="/images/image.jpg" alt="Garden Image" loading="lazy">
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
Loads the image lazily, improving page performance.
Adds custom CSS to the image directly.
Adds width, height, border, border-radius, and margin using inline CSS.
Use these inline styles to improve presentation:
<!DOCTYPE html>
<html>
<body>
<img src="/images/image.jpg" alt="Styled Image" style="width:150px; height:100px; border:2px solid #000; border-radius:8px; margin:10px;">
<img src="/images/image.jpg" alt="Flowers" style="width:200px; height:150px; border-radius:10px; margin:10px; border:1px solid #ccc;">
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
This makes the image cleaner, with padding and border styling.
The HTML '<img>' tag is essential for embedding images into web pages. By understanding the attributes like 'src', 'alt', and 'width', and using good inline styling, you can make your images not only visually appealing but also SEO-friendly. Make sure to always include the 'alt' attribute for accessibility and better Google indexing.
It provides alternate text when the image cannot be displayed and helps screen readers understand the image content.
It is an inline-level and empty element.
You can use the 'style' attribute to apply inline CSS like width, border, padding, and more.
It delays loading images until they are visible in the viewport, improving performance.