Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

Web Development - HTML Images


HTML Images

Images make your web pages more visual and engaging. In HTML, images are inserted with the <img> element.

Important: The <img> element is an empty element - it has no closing tag.


Basic Image

Use the src attribute to specify the image path and the alt attribute to describe the image.

Example

<img src="pic_trulli.jpg" alt="Trulli in Puglia, Italy">
Try it Yourself »

The alt text is shown if the image cannot load, and it helps screen readers describe the image.


Width and Height

You can set the size of an image with the width and height attributes (in pixels).

Example

Fixed size:

<img src="img_mountains.jpg" alt="Mountains" width="500" height="333">
Try it Yourself »

Tip: For responsive layouts, it is often better to control size with CSS instead of fixed width and height attributes.


Responsive Images

Make images scale with the screen by using CSS. Setting max-width: 100% and height: auto keeps the image inside its container.

Example

Responsive image with inline CSS:

<img src="img_city.jpg" alt="City skyline" style="max-width:100%; height:auto;">
Try it Yourself »

Image Paths

Use a relative path for images inside your project folder, or an absolute URL for images hosted elsewhere.

Example

Relative and absolute paths:

<!-- Relative path (image in images/ folder) -->
<img src="/images/logo.png" alt="Site logo">

<!-- Absolute URL -->
<img src="https://example.com/banner.jpg" alt="Banner">
Try it Yourself »

Tip: Keep your images in a folder like /images/ to stay organized.


Alt Text Best Practices

Write alt text that describes the image"s meaning, not just its file name. If the image is decorative, you can use an empty alt: alt="".

Example

Good vs poor alt text:

<!-- Good: describes the content -->
<img src="team.jpg" alt="Our team standing outside the office">

<!-- Poor: not descriptive -->
<img src="team.jpg" alt="image">
Try it Yourself »

Title Attribute (Tooltip)

Add a short tooltip with the title attribute. This is optional and appears when the user hovers the image.

Example

Image with a tooltip:

<img src="img_forest.jpg" alt="Forest trees" title="Beautiful green forest" width="400">
Try it Yourself »

Image as a Link

Wrap an image with an <a> element to make it clickable.

Example

Clickable image:

<a href="https://www.w3schools.com" target="_blank">
  <img src="images/w3schools.png" alt="W3Schools" width="160">
</a>
Try it Yourself »

Figure and Caption

Use <figure> and <figcaption> to group an image with a caption.

Example

Image with caption:

<figure>
  <img src="img_snow.jpg" alt="Snowy mountain" width="420">
  <figcaption>A snowy mountain at sunrise</figcaption>
</figure>
Try it Yourself »

Common Mistakes

  • Forgetting alt text.
  • Pointing src to the wrong path.
  • Using very large images without resizing or compression.

Tip: Use compressed images (JPEG, PNG, WebP) and sensible dimensions to keep pages fast.


Summary

  • Insert images with <img src="..." alt="...">.
  • Set size with attributes or CSS - CSS is better for responsive design.
  • Use meaningful alt text for accessibility.
  • Make images responsive with max-width:100%; height:auto;.
  • Wrap images in links to make them clickable.

Next, you will learn about HTML Lists - creating bullet lists and numbered lists to structure information.

Next » HTML Lists


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.