Nearby lessons

12 of 24

๐Ÿ“ท HTML Images

๐Ÿ“Œ What are HTML Images?

HTML <img> tags are used to embed images in web pages. This guide covers syntax, attributes, folder structures, and best practices for beginners.

๐Ÿงพ Introduction to HTML Images

The HTML <img> element embeds images in documents. It is a self-closing tag that requires at least two attributes: src (source of the image) and alt (alternative text for screen readers and SEO).

๐Ÿ“Œ Basic Syntax

A basic image element includes the src and alt attributes. Example: <img src="image.jpg" alt="Description of image">

๐Ÿ–ผ๏ธ Basic Image Example

Code Example
PREVIEW READY
Loading Editor...
Live Preview

โš™๏ธ Image Attributes

AttributeDescription
srcPath to the image file (URL or relative path)
altAlternative text for accessibility and SEO
alignAligns image with surrounding text (left, right, middle) - Deprecated
borderAdds a border around image (use CSS instead)
hspaceHorizontal space around image (Deprecated)
vspaceVertical space around image (Deprecated)
widthWidth in pixels (use CSS for better control)
heightHeight in pixels (use CSS for better control)
loading"lazy" for deferred load, "eager" for immediate
srcsetMultiple image sources for responsive images
sizesImage display sizes for different viewports
decoding"async", "sync", or "auto" for decoding method

๐Ÿ“ Sized Image Example

You can control the image size using width and height attributes, although using CSS is preferred.
Code Example
PREVIEW READY
Loading Editor...
Live Preview

๐Ÿ”— Using Images as Links

You can make an image clickable by wrapping it inside an <a> (anchor) tag. This is useful for logos, thumbnails, and buttons.
Code Example
PREVIEW READY
Loading Editor...
Live Preview

๐Ÿ“ Different Folder Structures for Images

How to use the <img> tag correctly based on different folder structures.
Code Example
PREVIEW READY
Loading Editor...
Live Preview

โŒ Common Errors in Image Usage

  • ๐Ÿšซ Missing alt text (hurts accessibility)
  • ๐Ÿšซ Broken path in src (image won't load)
  • ๐Ÿšซ Using absolute file path (C:/Users/...) instead of web path
  • ๐Ÿšซ Forgetting to specify width/height in layout-sensitive areas
Code Example
PREVIEW READY
Loading Editor...
Live Preview

โš ๏ธ Avoid: Missing Alt Text

Images without alt text create problems for screen readers and SEO. Always include meaningful alternative descriptions.
Code Example
PREVIEW READY
Loading Editor...
Live Preview

๐Ÿ–ผ๏ธ Image with Caption using Figure & Figcaption

Use <figure> to group an image with a caption using <figcaption>. This is useful for articles, galleries, and accessibility.
Code Example
PREVIEW READY
Loading Editor...
Live Preview

๐Ÿง  Common Image Formats and Their Uses

AttributeDescription
JPG/JPEGBest for photos with lots of colors. Compressed and small file size, but lossy quality.
PNGBest for images with transparency or text. Lossless compression.
GIFSupports animations and transparency, but limited to 256 colors.
SVGBest for logos and icons. Vector-based and resolution-independent.
WebPModern format with great compression and quality. Supported by most modern browsers.
AVIFNewest format with even better compression than WebP. Growing browser support.

๐Ÿ’ก Image Best Practices

  • โœ… Always include meaningful alt text for accessibility
  • โœ… Use loading="lazy" for below-the-fold images
  • โœ… Optimize images for web (proper format and compression)
  • โœ… Prefer CSS for image sizing instead of width/height attributes
  • โœ… Use srcset and sizes for responsive images
  • โœ… Consider using modern formats like WebP or AVIF for better performance

๐Ÿง  Test Your Knowledge

5 Questions

Progress: 0 / 5
Keep Going!HTML Audio