matthew_thompson
matthew_thompson 2d ago β€’ 0 views

Sample code for inserting and displaying images in HTML

Hey everyone! πŸ‘‹ I'm trying to add images to my website, but I'm getting a bit confused about the HTML code. Can someone give me a simple example of how to insert and display images in HTML? Also, how do I control the size of the image and add a description for accessibility? Thanks in advance! πŸ™
πŸ’» Computer Science & Technology

1 Answers

βœ… Best Answer
User Avatar
hannah425 Dec 29, 2025

πŸ“š Inserting Images in HTML: A Comprehensive Guide

The <img> tag is used to embed an image in an HTML page. Images are not technically inserted into the HTML page; rather, HTML links to images. The <img> tag creates a holding space for the referenced image. Here's a detailed look at how to use it:

  • πŸ–ΌοΈ Basic Syntax: The fundamental structure of the <img> tag involves the src attribute, which specifies the URL of the image, and the alt attribute, which provides alternative text if the image cannot be displayed.
  • πŸ“ Specifying the Image Source (src): The src attribute is crucial as it tells the browser where to find the image file. This can be a URL pointing to an image on another website or a relative path to an image within your project's file structure.
  • ✍️ Alternative Text (alt): The alt attribute is essential for accessibility. It provides a text description of the image, which is displayed if the image fails to load or is viewed by screen readers. A good alt text should be concise and descriptive.
  • πŸ“ Setting Image Dimensions (width and height): You can control the size of the image using the width and height attributes. It's generally recommended to specify these attributes to prevent layout shifting as the page loads.
  • πŸ”— Linking Images: You can also wrap an image within an <a> tag to make it a clickable link. This is useful for creating image-based navigation or linking to larger versions of the image.

πŸ§ͺ Real-World Examples

Let's look at some practical HTML code examples to demonstrate how to insert and display images effectively.

  1. 🏞️ Simple Image Insertion

    This example shows the basic way to insert an image using the src and alt attributes.

    <img src="images/myimage.jpg" alt="A beautiful landscape">
  2. πŸ“ Controlling Image Size

    Here's how to specify the width and height of the image to control its dimensions on the page.

    <img src="images/logo.png" alt="Company Logo" width="200" height="100">
  3. πŸ”— Image as a Link

    This example demonstrates how to make an image clickable, linking it to another webpage.

    <a href="https://www.example.com"><img src="images/thumbnail.jpg" alt="Link to Example"></a>

πŸ’‘ Best Practices and Considerations

  • πŸ“ Image Optimization: Always optimize your images for the web to reduce file size and improve page load times. Tools like TinyPNG can be very helpful.
  • 🌍 Responsive Images: Consider using the <picture> element or the srcset attribute in the <img> tag to serve different image sizes based on the user's device.
  • βœ… Accessibility: Ensure your alt text is descriptive and meaningful for users with disabilities.

Join the discussion

Please log in to post your answer.

Log In

Earn 2 Points for answering. If your answer is selected as the best, you'll get +20 Points! πŸš€