HTML Images
π· Introduction to HTML Images
Images are a vital part of modern web content β they enhance visual appeal, convey meaning, and create engaging experiences. In HTML, images are embedded using the <img> tag. This tag is self-closing and does not wrap any content.
>>βA picture is worth a thousand words β and in HTML, it's just one tag away!β π§
π€ Basic Image Syntax
Basic HTML Image
<img src="image.jpg" alt="Description of image" />Here's what each attribute means:
- src β The image file path or URL (required) π
- alt β Text shown when the image fails to load (required for accessibility) βΏ
Note
π‘ Always use the alt attribute for better SEO and screen reader support!
π Setting Width and Height
You can control image size using width and height attributes (in pixels) or with CSS.
Image with Width and Height
<img src="dog.jpg" alt="Cute Dog" width="300" height="200" />π¨ Styling Images with CSS
CSS Styling for Images
img {
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.2);
max-width: 100%;
}This ensures images are responsive and look modern β¨
π Clickable Images
Wrap the image in an anchor tag to make it a link:
Clickable Image Example
<a href="https://example.com">
<img src="thumb.jpg" alt="Click me!" />
</a>πΌοΈ Image Formats
| Format | Usage |
|---|---|
| .jpg / .jpeg | Photos, small file size |
| .png | Transparency support, high quality |
| .gif | Simple animations |
| .webp | Modern, small & fast |
| .svg | Vector graphics, resolution-independent |
π¦ Images from External URLs
Using Image URL
<img src="https://via.placeholder.com/150" alt="Placeholder Image" />Note
β οΈ Always make sure you have permission to use external images to avoid copyright issues.
π§ͺ Practical Example
Image Card Example
<div class="profile">
<img src="avatar.png" alt="User avatar" width="100" />
<h3>Jane Doe</h3>
<p>Frontend Developer</p>
</div>>>βImages can communicate complex ideas faster than text β use them wisely.β ποΈ