HTML CSS Bootstrap JavaScript jQuery MySQL PHP Data Mining

HTML Images

Images make web pages more visually attractive and informative. HTML uses the <img> tag to embed images into a web page.


The <img> Tag

The <img> tag is an empty element — it has no closing tag. It requires at minimum two attributes: src and alt:

<img src="image.jpg" alt="Description of image">
Attribute Description
srcPath or URL to the image file
altAlternative text if image cannot be displayed
widthWidth of the image in pixels
heightHeight of the image in pixels
titleTooltip text shown on hover
loadinglazy to defer loading off-screen images

The src Attribute

The src attribute specifies the path to the image. It can be an absolute or relative URL:

<!-- Relative path (image in same folder) -->
<img src="photo.jpg" alt="Photo">

<!-- Relative path (image in subfolder) -->
<img src="images/photo.jpg" alt="Photo">

<!-- Absolute URL (image from internet) -->
<img src="https://www.example.com/images/photo.jpg" alt="Photo">

The alt Attribute

The alt attribute provides alternative text for the image. It is displayed when:

  • The image cannot be found or loaded
  • The user is using a screen reader (visually impaired)
  • The browser has images disabled
<img src="logo.png" alt="RedoHub Logo">
<img src="cat.jpg" alt="A fluffy orange cat sitting on a windowsill">

<!-- Decorative image — empty alt is fine -->
<img src="divider.png" alt="">
Important: Never skip the alt attribute. It is required for accessibility and SEO. Without it, screen readers skip the image entirely.

Width and Height

Always specify width and height so the browser can reserve space for the image before it loads — preventing layout shifts:

<img src="photo.jpg" alt="Landscape" width="600" height="400">
Tip: Use CSS to make images responsive: img { max-width: 100%; height: auto; } — this makes images scale down on smaller screens while keeping the aspect ratio.

Image as a Link

Wrap an image with an <a> tag to make it clickable:

<a href="https://www.redohub.com">
    <img src="logo.png" alt="RedoHub" width="120">
</a>

Image Floating

Use CSS float to wrap text around an image:

<img src="photo.jpg" alt="Photo" style="float: left; margin-right: 15px;">
<p>This text wraps around the image on the right side.</p>

Lazy Loading

The loading="lazy" attribute defers loading of off-screen images until the user scrolls near them — improving page speed:

<img src="large-photo.jpg" alt="Large photo" width="800" height="600" loading="lazy">

Common Image Formats

Format Extension Best For
JPEG.jpg / .jpegPhotographs — small file size, lossy compression
PNG.pngGraphics with transparency, logos, icons
GIF.gifSimple animations, limited to 256 colors
SVG.svgVector graphics — scalable without quality loss
WebP.webpModern format — smaller than PNG/JPEG, supports transparency
AVIF.avifNewest format — best compression, great quality