MyInternships.in

HTML Basics

HTML Images

Images bring web pages to life. The <img> tag embeds a picture, and a few key attributes control its source, size, and accessibility.


What is an HTML Image?

The <img> tag places an image on the page. It is an empty element — it has no closing tag. The src attribute gives the image's location, and the alt attribute provides descriptive text shown if the image fails to load or is read by a screen reader.

Images do not sit inside the <img> tag; they are referenced by URL. That URL can point to a file on your own site or to any image on the web.

Basic Usage

A basic image
Example
<img src="https://picsum.photos/200" alt="A random placeholder photo">

Example: Image With Size and Alt Text

Controlling dimensions
Example
<!DOCTYPE html>
<html>
<body>
  <h2>A photo</h2>
  <img src="https://picsum.photos/300/200"
       alt="A 300 by 200 placeholder landscape"
       width="300" height="200">
  <p>The width and height keep the layout stable while loading.</p>
</body>
</html>

More Examples

A clickable image (link)
Example
<a href="https://myinternships.in">
  <img src="https://picsum.photos/200" alt="Click to visit MyInternships.in">
</a>
A responsive image that scales to its container
Example
<!DOCTYPE html>
<html>
<body style="max-width:250px;">
  <img src="https://picsum.photos/600/400"
       alt="A responsive placeholder photo"
       style="width:100%; height:auto; border-radius:8px;">
</body>
</html>

Key img Attributes

AttributePurpose
srcThe image's URL (required).
altText alternative for accessibility and fallback.
widthThe display width in pixels.
heightThe display height in pixels.
loadingSet to "lazy" to defer off-screen images.
⚠️

Always include an alt attribute. It is essential for screen reader users and appears if the image cannot load. Use alt="" only for purely decorative images.

💡

Set width and height (or a CSS aspect ratio) so the browser reserves space before the image loads. This prevents the page from jumping around as images appear.

Key Takeaways

  • The <img> tag embeds an image and has no closing tag.
  • src sets the image URL; alt describes it for accessibility.
  • Set width and height to keep the layout stable.
  • Wrap an <img> in an <a> tag to make it clickable.

Related HTML Topics

Keep learning with these closely related tutorials.

Ready to use your HTML skills?

Find web development internships and fresher jobs across India.

Browse Web Dev Internships