HTML img src Attribute (2024)

❮ HTML <img> tag

Example

An image is marked up as follows:

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

Try it Yourself »

Definition and Usage

The required src attribute specifies the URL of the image.

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".

Notes: 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; it can suddenly be removed or changed.

2. Relative URL - Links to an image that is hosted within the website. Here, the URL does not include the domain name. If the URL begins without a slash, it will be relative to the current page. Example: src="img_girl.jpg". If the URL begins with a slash, it will be relative to the domain. Example: src="/images/img_girl.jpg".

Tip: It is almost always best to use relative URLs. They will not break if you change domain.

Note: A broken link icon and the alt text are shown if the browser cannot find the image.

Browser Support

Attribute
src Yes Yes Yes Yes Yes

Syntax

Attribute Values

Value Description
URL The URL of the image.

Possible values:

  • An absolute URL - points to another web site (like src="http://www.example.com/image.gif")
  • A relative URL - points to a file within a web site (like src="image.gif")

❮ HTML <img> tag

W3schools Pathfinder

Track your progress - it's free!

HTML img src Attribute (2024)

FAQs

What is the src attribute of IMG? ›

The <img> src attribute is used to specify the URL of the source image. It points to the location of the image file that the browser should display on the webpage.

How to put img src in HTML? ›

First, the title attribute is title =”(your title)”. Next, set your alt attribute, which explains the image in detail. Finally, set the height and width of your image. Use the code <img src=”(your title)” alt=”Image” height=”(your image height)” width=”(your image width)”>.

How to pass image src 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 add a src attribute in HTML? ›

HTML <source> src Attribute
  1. <audio controls>
  2. <source src="horse.ogg" type="audio/ogg">
  3. <source src="horse.mp3" type="audio/mpeg">
  4. </audio>

What is an example of image src? ›

Example: <img src="animal. jpg" height="180" width="300" alt="animal image">

Why is my img src not working in HTML? ›

Img src Not Working

That means, when a web page loads, the browser has to retrieve the image from a web server and display it on the page. The broken link icon means that the browser could not find the image. If you've just added the image, then check that you included the correct image URL in the source attribute.

What is the HTML tag for IMG? ›

The <img> tag is used to embed an image in an HTML 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.

Why is my image not showing in HTML? ›

The path or filename is incorrect

Verify that your HTML source code has the correct path to images. Moving your website or renaming folders may cause the links to images in the code to break. You can use the inspect website elements function of your browser for this.

How to align img src in HTML? ›

HTML <img> align attribute is used to set the alignment of an image. It is an inline element. It is used to specify the alignment of the image according to surrounding elements. Set align to “left,” “center,” or “right” for horizontal alignment.

How do you display an image in HTML? ›

To display an image on your web page, you'll use the <img> element. It's a self-closing tag, which means you don't need a closing </img> tag. Instead, you place the image source and other attributes within the opening tag. The src attribute specifies the image file's location.

How do I insert a local image in HTML? ›

To insert an image in HTML, use the image tag and include a source and alt attribute. Like any other HTML element, you'll add images to the body section of your HTML file. The HTML image element is an “empty element,” meaning it does not have a closing tag.

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 set img src? ›

To use an image on a webpage, use the <img> tag. The tag allows you to add image source, alt, width, height, etc. The src is to add the image URL. The alt is the alternate text attribute, which is text that is visible when the image fails to load.

How to source an image in HTML? ›

In order to put a simple image on a web page, we use the <img> element. This is a void element (meaning, it cannot have any child content and cannot have an end tag) that requires two attributes to be useful: src and alt . The src attribute contains a URL pointing to the image you want to embed in the page.

How to display an image in HTML from a local drive? ›

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.

What is IMG data SRC? ›

The src tag is used to specify the source of an image, while the data-src tag is used to specify a source that should be used when loading an image. The data-src tag is also used to lazy load images, meaning that the image will only be loaded when it's needed.

How to add img src in CSS? ›

So to add images, we use the <img> inline element. Through this HTML element, we will only be able to add the image, source, and link. Then with the help of CSS, we will mention the proportions and other needed styles of the image.

What is the src and align attribute of the img tag? ›

<img> tag has following attributes: src::It is used to determine the source of image within the database of your computer. Then we have the align attribute which helps us determine the position or to be more exact,the alignment of image with context to the webpage.

Does IMG use src or href? ›

src indicates a resource that is embedded in the current document, such as, img, video, audio, or script. The browser will allocate screen real estate depending upon the physical dimensions of the embedded object. href has no such provision.

Top Articles
Latest Posts
Article information

Author: Barbera Armstrong

Last Updated:

Views: 6069

Rating: 4.9 / 5 (59 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Barbera Armstrong

Birthday: 1992-09-12

Address: Suite 993 99852 Daugherty Causeway, Ritchiehaven, VT 49630

Phone: +5026838435397

Job: National Engineer

Hobby: Listening to music, Board games, Photography, Ice skating, LARPing, Kite flying, Rugby

Introduction: My name is Barbera Armstrong, I am a lovely, delightful, cooperative, funny, enchanting, vivacious, tender person who loves writing and wants to share my knowledge and understanding with you.