HTML <img> Element

Publish in HTML Tutorial el 24/05/2025 15:54

The <img> element is one of the most fundamental and widely used HTML elements. It embeds an image into a web page. Unlike other elements that wrap content, <img> is a self-closing (void) element that displays external image files or data URIs.


Key attributes include:

  • src: Specifies the image URL (required)
  • alt: Provides alternative text for accessibility (required)
  • width/height: Sets display dimensions
  • loading: Controls lazy loading behavior
  • srcset/sizes: Enables responsive images


Basic Syntax


Rendered Example

Play Code


Examples

1. Basic Image with Alt Text

Always include meaningful alt text for accessibility and SEO.

Play Code


2. Responsive Image with srcset

Serve different images based on device resolution and screen size.

Play Code


3. Image with CSS Styling

Demonstrates how to style images with CSS.

Play Code


4. Image Swap with JavaScript

Click the image to toggle between two states.

Play Code


5. Lazy Loading with Intersection Observer

Advanced lazy loading technique for better performance.

Play Code


Tips and Tricks

  • Always include alt text - Critical for accessibility and SEO
  • Specify width and height - Prevents layout shifts during loading
  • Use modern formats - WebP typically offers better compression than JPEG/PNG
  • Implement lazy loading - Use loading="lazy" for below-the-fold images
  • Optimize images - Compress images before uploading
  • Use srcset for responsive images - Serve appropriately sized images
  • Consider CDNs - For faster delivery of images

También te puede interesar

HTML <style> Element
HTML <style> Element

The <style> HTML element contains style information for a document, or part of a document. ...

HTML <strong> Element
HTML <strong> Element

The <strong> element is used to indicate that its content has strong importance, seriousnes...

HTML <span> Element
HTML <span> Element

The <span> element is an inline container used to mark up a part of a text or document. Unl...

HTML <source> Element
HTML <source> Element

The <source> element is used to specify multiple media resources for media elements like &l...

HTML <small> Element
HTML <small> Element

The <small> element is used to represent side-comments and small print, typically for discl...

HTML <section> Element
HTML <section> Element

HTML <section> Element: The Complete Guide The <section> element is a semantic HTML ...