HTML Images (2024)

Images can improve the design and the appearance of a web page.

Example

<img src="pic_trulli.jpg" alt="Italian Trulli">

Try it Yourself »

Example

<img src="img_girl.jpg" alt="Girl in a jacket">

Try it Yourself »

Example

<img src="img_chania.jpg" alt="Flowers in Chania">

Try it Yourself »

HTML Images Syntax

The HTML <img> tag is used to embed an image in a web page.

Images are not technically inserted into a web page; images are linked to web pages. The <img> tag creates a holding space for the referenced image.

The <img> tagis empty, it contains attributes only, and does not have a closing tag.

The <img> tag has two required attributes:

  • src - Specifies the path to the image
  • alt - Specifies an alternate text for the image

Syntax

<img src="url" alt="alternatetext">

The src Attribute

The required src attribute specifies the path (URL) to the image.

Note: When a web page loads, it is the browser, at that moment, that gets the image from a web server and inserts it into the page. Therefore, make sure that the image actually stays in the same spot in relation to the web page, otherwise your visitors will get a broken link icon. The broken link icon and the alt text are shown if the browser cannot find the image.

The alt Attribute

The required alt attribute provides an alternate text for an image, if the user for some reason cannot view it (because of slow connection, an error in the src attribute, or if the user uses a screen reader).

The value of the alt attribute should describe the image:

Example

<img src="img_chania.jpg" alt="Flowers in Chania">

Try it Yourself »

If a browser cannot find an image, it will display the value of the alt attribute:

Example

<img src="wrongname.gif" alt="Flowers in Chania">

Try it Yourself »

Tip: A screen reader is a software program that reads the HTML code, and allows the user to "listen" to the content. Screen readers are useful for people who are visually impaired or learning disabled.

Image Size - Width and Height

You can use the style attribute to specify the width and height of an image.

Example

<img src="img_girl.jpg" alt="Girl in a jacket" style="width:500px;height:600px;">

Try it Yourself »

Alternatively, you can use the width and height attributes:

Example

<img src="img_girl.jpg" alt="Girl in a jacket" width="500" height="600">

Try it Yourself »

The width and height attributes always define the width and height of the image in pixels.

Note: Always specify the width and height of an image. If width and height are not specified, the web page might flicker while the image loads.

Width and Height, or Style?

The width, height, and style attributes are all valid in HTML.

However, we suggest using the style attribute. It prevents styles sheets from changing the size of images:

Example

<!DOCTYPE html>
<html>
<head>
<style>
img {
width: 100%;
}
</style>
</head>
<body>

<img src="html5.gif" alt="HTML5 Icon" width="128" height="128">

<img src="html5.gif" alt="HTML5 Icon" style="width:128px;height:128px;">

</body>
</html>

Try it Yourself »

Images in Another Folder

If you have your images in a sub-folder, you must include the folder name in the src attribute:

Example

<img src="/images/html5.gif" alt="HTML5 Icon" style="width:128px;height:128px;">

Try it Yourself »

Images on Another Server/Website

Some web sites point to an image on another server.

To point to an image on another server, you must specify an absolute (full) URL in the src attribute:

Example

<img src="https://www.w3schools.com/images/w3schools_green.jpg" alt="W3Schools.com">

Try it Yourself »

Notes on external images: External images might be under copyright. If you do not get permission to use it, you may be in violation of copyright laws. In addition, you cannot control external images; they can suddenly be removed or changed.

Animated Images

HTML allows animated GIFs:

Example

<img src="programming.gif" alt="Computer Man" style="width:48px;height:48px;">

Try it Yourself »

Image as a Link

To use an image as a link, put the <img> tag inside the <a> tag:

Example

<a href="default.asp">
<img src="smiley.gif" alt="HTML tutorial" style="width:42px;height:42px;">
</a>

Try it Yourself »

Image Floating

Use the CSS float property to let the image float to the right or to the left of a text:

Example

<p><img src="smiley.gif" alt="Smiley face"style="float:right;width:42px;height:42px;">
The image will float to the right of the text.</p>

<p><img src="smiley.gif" alt="Smiley face"style="float:left;width:42px;height:42px;">
The image will float to the left of the text.</p>

Try it Yourself »

Tip: To learn more about CSS Float, read our CSS Float Tutorial.

Common Image Formats

Here are the most common image file types, which are supported in all browsers (Chrome, Edge, Firefox, Safari, Opera):

AbbreviationFile FormatFile Extension
APNGAnimated Portable Network Graphics.apng
GIFGraphics Interchange Format.gif
ICOMicrosoft Icon.ico, .cur
JPEGJoint Photographic Expert Group image.jpg, .jpeg, .jfif, .pjpeg, .pjp
PNGPortable Network Graphics.png
SVGScalable Vector Graphics.svg

Chapter Summary

  • Use the HTML <img> element to define an image
  • Use the HTML src attribute to define the URL of the image
  • Use the HTML alt attribute to define an alternate text for an image, if it cannot be displayed
  • Use the HTML width and height attributes or the CSS width and height properties to define the size of the image
  • Use the CSS float property to let the image float to the left or to the right

Note: Loading large images takes time, and can slow down your web page. Use images carefully.

HTML Exercises

HTML Image Tags

TagDescription
<img>Defines an image
<map>Defines an image map
<area>Defines a clickable area inside an image map
<picture>Defines a container for multiple image resources

For a complete list of all available HTML tags, visit our HTML Tag Reference.

Video: HTML Images

HTML Images (1)

HTML Images (2)


W3schools Pathfinder

Track your progress - it's free!

HTML Images (2024)

FAQs

How do you add an image in HTML? ›

The HTML <img> tag is used to embed an image in a web page. Images are not technically inserted into a web page; images are linked to web pages. The <img> tag creates a holding space for the referenced image. The <img> tag is empty, it contains attributes only, and does not have a closing tag.

How to display different images in HTML? ›

Example
  1. <picture>
  2. <source srcset="img_avatar.png">
  3. <source srcset="img_girl.jpg">
  4. <img src="img_beatles.gif" alt="Beatles" style="width:auto;">
  5. </picture>

How do I link an image in HTML? ›

Place the <img> tag inside an <a> tag and provide href . As others mentioned in comments you want to surround the image in an anchor <a> tag and apply the href attribute to the anchor tag as you would a normal hyperlink.

How to display data as image in HTML? ›

For an image, the data type would be “data:image/[format]; base64,” where [format] is the format of the image file (e.g. PNG, JPEG, GIF). Embed the data URL into your HTML: To display the image, you can use an HTML image tag (<img>) and set the “src” attribute to the data URL.

How do I add a local image in HTML? ›

In order to insert an image in HTML from a folder you will need to use the <img> tag. The src attribute is used to specify the location of the image. You can link to an image using either an absolute or relative file path.

How to insert image in input tag in HTML? ›

The <input type="image"> defines an image as a submit button. The path to the image is specified in the src attribute.

How do I pass an image to a URL in HTML? ›

There are two ways to specify the URL in the src attribute: 1. Absolute URL - Links to an external image that is hosted on another website. Example: src="https://www.w3schools.com/images/img_girl.jpg".

How do I hyperlink an image? ›

Put the anchor tag <a href=””> before the image url, and </a> after. Paste the destination URL you want to add within the quotation marks for <a href=””>. Make sure you put “http://” before the link. Now enjoy the hyperlink in your customized image if you have followed these steps properly.

How do I put an image anywhere in HTML? ›

You can use the float property to position images and text side by side, or use the position property to place them in a specific location on the page. This code will make images float to the left with a 10 pixel margin between the image and text, and make the text flow around the image.

Why is my image not showing up in HTML? ›

First, make sure that the file is in the same folder as the HTML. If the file is in another folder, you will need to provide the relative path from your HTML file to the image. Make sure that the file type is supported by the browser (e.g. supported types include PNG, JPEG, and GIF).

How do I display a selected image in HTML? ›

In this method, we will set the display property inside the <img> tag using the style attribute. We have to set “display: none” for the image. After that, when the user clicks on the button. We will call a JavaScript function that will access the image to change its display property to block.

How do I put an image somewhere in HTML? ›

You can use the float property to position images and text side by side, or use the position property to place them in a specific location on the page.

How to add image in button using HTML? ›

Inside the div, we will add an email <input> field to get the email of the user. To make an image button in HTML, we will use the <img> tag inside the <button> tag. So in this way, the image will behave like a button. In the src attribute of the image tag, we write the address of the image we want to use.

What is the correct HTML for inserting a background image? ›

Within the starting <body> tag in your Html file, type <Body background=” “> Give the path of the image we want to add.

How do I upload an image to a user in HTML? ›

The foundation of file uploading in HTML lies in the <input type="file"> element. This simple yet powerful element creates a button on your webpage, enabling users to securely select files from their device's storage.

Top Articles
The benefits and availability of Dual SIM smartphones
‘Mandatory reconsideration' in 2024
Chs.mywork
NOAA: National Oceanic &amp; Atmospheric Administration hiring NOAA Commissioned Officer: Inter-Service Transfer in Spokane Valley, WA | LinkedIn
Play FETCH GAMES for Free!
Hannaford Weekly Flyer Manchester Nh
Maria Dolores Franziska Kolowrat Krakowská
Stolen Touches Neva Altaj Read Online Free
My Vidant Chart
Find your energy supplier
Craigslist Greenville Craigslist
Cooking Fever Wiki
10 Free Employee Handbook Templates in Word & ClickUp
Learn2Serve Tabc Answers
Tcu Jaggaer
My.tcctrack
Dtab Customs
Classic | Cyclone RakeAmerica's #1 Lawn and Leaf Vacuum
Inter-Tech IM-2 Expander/SAMA IM01 Pro
Bing Chilling Words Romanized
Is The Yankees Game Postponed Tonight
Cvs El Salido
Dulce
College Basketball Picks: NCAAB Picks Against The Spread | Pickswise
Myql Loan Login
Sienna
Unable to receive sms verification codes
Feathers
Great ATV Riding Tips for Beginners
Farm Equipment Innovations
Miller Plonka Obituaries
Average weekly earnings in Great Britain
How to Destroy Rule 34
Studentvue Columbia Heights
8005607994
Michael Jordan: A timeline of the NBA legend
888-333-4026
Bob And Jeff's Monticello Fl
Torrid Rn Number Lookup
Doe Infohub
Brown launches digital hub to expand community, career exploration for students, alumni
Nimbleaf Evolution
Noga Funeral Home Obituaries
The Average Amount of Calories in a Poke Bowl | Grubby's Poke
Ups Customer Center Locations
Euro area international trade in goods surplus €21.2 bn
Ajpw Sugar Glider Worth
Elvis Costello announces King Of America & Other Realms
786 Area Code -Get a Local Phone Number For Miami, Florida
Grace Charis Shagmag
BYU Football: Instant Observations From Blowout Win At Wyoming
Latest Posts
Article information

Author: Gov. Deandrea McKenzie

Last Updated:

Views: 5855

Rating: 4.6 / 5 (46 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Gov. Deandrea McKenzie

Birthday: 2001-01-17

Address: Suite 769 2454 Marsha Coves, Debbieton, MS 95002

Phone: +813077629322

Job: Real-Estate Executive

Hobby: Archery, Metal detecting, Kitesurfing, Genealogy, Kitesurfing, Calligraphy, Roller skating

Introduction: My name is Gov. Deandrea McKenzie, I am a spotless, clean, glamorous, sparkling, adventurous, nice, brainy person who loves writing and wants to share my knowledge and understanding with you.