KeynouProgramming
Articles
Sign InGet Started
© 2026 Programming Keynou. All rights reserved.
Privacy PolicyTerms of ServiceContact
Back to Articles

Inserting Images in HTML: The img Tag Explained

12/9/2025
Backend Development with Django
CI/CDNext.jsSystem Design

Inserting Images in HTML: The <img> Tag Explained

Images are at the heart of modern web pages—improving user experience, visually explaining concepts, and powering content-heavy sections like product galleries or branding. For backend developers working with Django, or anyone learning system design and delivering code through CI/CD, a strong understanding of HTML's image facilities is vital. This article offers a comprehensive, technically detailed, beginner-friendly guide to the <img> tag—with real-world use cases, sample code, nuanced explanations, and specific best practices (including performance and accessibility).

Why Does the <img> Tag Matter in System Design and Backend Development?

In backend-driven web applications—particularly with frameworks like Django—images are dynamically rendered in templates, fetched from static or media directories, and often need to be optimized for performance. Additionally, with modern CI/CD pipelines, ensuring your image assets are efficiently managed, transferred, and served impacts the speed and reliability of your deployments. System design decisions (like using a CDN or optimizing image formats) become relevant as your platform scales.

What is the <img> Tag in HTML?

The <img> tag is an HTML element that embeds an image in a web page. In plain English: it's a piece of code you write inside your web pages, telling the browser exactly where to fetch the image from and how to display it. The <img> tag is self-closing and void, meaning it doesn't need a closing tag (you write <img ... /> and that's it).

Core Attributes of the <img> Tag: src, alt, and title

  • src (Source): The URL or path to the image file you want to display. Without src, the browser has no image to show.
  • alt (Alternative Text): Textual fallback if the image cannot be loaded. Crucial for accessibility—screen readers use this to describe images for visually impaired users.
  • title (Tooltip Text): Optional. Displays a small tooltip text when you hover your mouse over the image.

Core Syntax: A Minimal Example


<img src="logo.png" alt="My Company Logo" />

Breakdown: This embeds an image called logo.png and, if the image is missing or can't be rendered, the user sees "My Company Logo" as descriptive alt text.

How Does <img> Work, Technically?

When your HTML page gets served (via Django or static hosting), the browser parses the document, sees the <img> tag, fetches the file from the src location (which can be a local relative path or a remote absolute URL), and then renders it within the flow of your web page. If the src can't be loaded (e.g., a missing or incorrect path) the browser shows the alt text in its place.

Advanced <img> Attributes Explained

  • width and height: Control the display dimensions (measured in pixels). Always set these to prevent layout shifts (improves Core Web Vitals and perceived performance).
  • loading: Defines how images are loaded: eager (default, loads when encountered) or lazy (loads only when scrolled into view). Use loading="lazy" to optimize page speed, especially in image-heavy pages.
  • srcset and sizes: For responsive images. These attributes let the browser choose the most appropriate image based on the user's device resolution or viewport size.
  • crossorigin: Allows using images hosted on different domains. This interacts with browser security (CORS), and is critical when you serve images from a CDN.
  • referrerpolicy: Controls how much referrer information should be sent when fetching the image. Helps balance security and analytics needs.
  • usemap: Links the <img> to a client-side image map for clickable areas.

Practical Example 1: CI/CD and Image Asset Delivery

In a CI/CD workflow (Continuous Integration/Continuous Deployment), your static files (images, CSS, JS) are collected and deployed to a static hosting platform or CDN. For Django, the collectstatic command ensures all images are in STATIC_ROOT or MEDIA_ROOT, available for the web server or CDN to serve. Incorrect paths or missing images often lead to “broken image” icons.


<img src="{% static 'images/product-1.jpg' %}" alt="Featured Product" loading="lazy" width="400" height="300" />

In this Django example, the {% static %} tag ensures the correct static image path—critical after static file collection in CI/CD.

Practical Example 2: Serving Responsive Images with srcset and sizes

“Responsive images” allow you to serve images tailored for device capabilities. For instance, a mobile phone with high pixel density (“Retina” screen) may need a higher-resolution image than a desktop with a standard monitor.


<img 
  src="avatar-small.jpg"
  srcset="
      avatar-small.jpg 400w,
      avatar-medium.jpg 800w,
      avatar-large.jpg 1200w
  "
  sizes="(max-width: 600px) 400px, (max-width: 1000px) 800px, 1200px"
  alt="User's photo"
  width="400" height="400"
/>

  • srcset: provides alternative images: 400, 800, and 1200 pixels wide.
  • sizes: tells the browser how much space the image will take up depending on the viewport width. The browser downloads the optimal size for the device, saving bandwidth and speeding load times.

Practical Example 3: Images and Accessibility (with alt Attribute)

Accessible applications (a major concern in professional web system design) always use the alt attribute, ensuring that screen reader software can describe visual elements.


<img src="chart-sales.png" alt="Bar chart showing quarterly sales growth from Q1 to Q4, 2024" />

Best practice: Make the alt text concise but descriptive enough to convey function and purpose.

Practical Example 4: Images Served via CDN in Next.js and Django

For modern apps built with Django and frontend frameworks like Next.js, serving images from a Content Delivery Network (CDN) is standard for scalability, speed, and fault-tolerance in a system design. The CDN has its own domain and may require CORS headers, thus sometimes needing the crossorigin attribute.


<img 
  src="https://cdn.yoursite.com/uploads/banner.jpg"
  alt="Welcome Banner"
  crossorigin="anonymous"
  loading="lazy"
  width="1200"
  height="400"
/>

Next.js, for example, auto-optimizes images using the next/image component, but if you render static images in Django templates, ensure your STATIC_URL or MEDIA_URL settings point to your CDN or static server.

Real-World Case Study: Image Handling in a Django E-commerce Site

Scenario: You need to display product images that are stored in your Django MEDIA_ROOT. Each product has various thumbnail sizes for different parts of the app (gallery, cart, detail page).

  • Models: Save images using a models.ImageField in your Django Product model.
  • Templates: Use the {{ product.image.url }} in your HTML template.
  • Image tags:
    
    <img src="{{ product.image.url }}" alt="{{ product.name }}" width="600" height="600" loading="lazy" />
            
  • CI/CD: Confirm MEDIA_URL points to your CDN/static server—if not, update your deployment pipeline to sync media/ to CDN.
  • System design: For scale, pre-generate thumbnail sizes and use srcset for each image resolution you need—serving only what the client device can display.

Issues to Avoid: Common Pitfalls with <img>

  • Missing alt attributes: Not only is this an accessibility failure, but it's a negative SEO signal.
  • Large, unoptimized images: Bloats page load times, increases costs in CI/CD and CDN bandwidth, and impacts user experience, especially on mobile.
  • Hard-coded paths: Always use Django's {% static %} or proper dynamic URLs to ensure compatibility after static/media collection.
  • Not specifying width and height: Causes browser layout shifts (“jumping” effect).
  • Incorrect CORS settings when serving from CDN: If you use subdomains or third-party CDNs, use the crossorigin attribute as needed, and ensure your server responds with correct headers.

Summary and Next Steps

The HTML <img> tag is a foundational element for displaying images on the web. In backend web development with Django, understanding the nuances of this tag allows you to deliver performant, accessible, and scalable applications—especially as you integrate CI/CD workflows and design for high availability (using CDNs). Key technical best practices include:

  • Always use alt text for accessibility and SEO.
  • Set width and height to prevent layout shifts.
  • Use srcset and sizes for responsive, bandwidth-efficient images.
  • Optimize image files before deployment to minimize load times and bandwidth in your CI/CD pipeline.
  • Serve images from a CDN at scale, and configure CORS properly for cross-domain delivery.
  • In frameworks like Next.js, prefer built-in image components for optimal loading, but understand how raw <img> works for custom or backend-rendered scenarios.

By grounding your web applications in these technical best practices, you ensure your images are accessible, performant, and ready for the demands of production deployment.

0 Comments

Comments

Loading comments...

Popular Posts

Recent Posts

Related Posts