HTML Images - GeeksforGeeks (2024)

The HTML <img> tag is used to embed an image in web pages by linking them. It creates a placeholder for the image, defined by attributes like src, width, height, and alt, and does not require a closing tag.

There are two ways to insert the images into a webpage:

  • By providing a full path or address (URL) to access an internet file.
  • By providing the file path relative to the location of the current web page file.

We will first discuss inserting the image into the webpage & simultaneously, we will understand both the above approaches.

Adding Images on a Webpage

The <img> tag embeds images in web pages, requiring only attributes without a closing tag. Images enhance design and content quality, linked via the <img> tag to preserve space and optimize webpage performance.

Syntax

<img src="url" alt="some_text" width="" height="">

Attribute: The <img> tag has following attributes:

AttributeDescription
srcSpecifies the path to the image file.
altProvides alternate text for the image, useful for accessibility and when the image cannot be displayed.
crossoriginAllows importing images from third-party sites with cross-origin access, typically used with canvas.
heightSpecifies the height of the image.
widthSpecifies the width of the image.
ismapSpecifies an image as a server-side image map.
loadingSpecifies whether the browser should defer image loading or load it immediately.
longdescSpecifies a URL to a detailed description of the image.
referrerpolicySpecifies which referrer information to use when fetching the image.
sizesSpecifies image sizes for different page layouts.
srcsetSpecifies a list of image files to use in different situations, allowing for responsive images.
usemapSpecifies an image as a client-side image map.

HTML img src Attribute

The src attribute in <img> tag specifies the image’s location. If the URL is correct, the browser retrieves and displays the image; otherwise, it shows a broken link icon.

Example : In this example we displays an image using the <img> tag with src attribute pointing to a GeeksforGeeks logo. It enhances content by visually representing the website’s identity.

HTML
<!DOCTYPE html><html><head> <title>Welcome To GFG</title></head><body> <h2>GeeksforGeeks</h2> <p>This is the demo of <img /> tag.</p> <img src="https://media.geeksforgeeks.org/wp-content/uploads/20210915115837/gfg3-300x300.png" alt="GFG image" /></body></html>

Output:

HTML Images - GeeksforGeeks (1)

HTML Images Example Output

HTML img alt Attribute

The alt attribute in <img> tag provides a text alternative if the image fails to load. It aids accessibility for users unable to view images due to slow internet, src errors, or screen reader usage.

Example : The example illustrates the use of the alt attribute in the <img> tag.

HTML
<!DOCTYPE html><html><head> <title>Alt Attribute Example</title></head><body> <p>Using Alt Attribute</p> <img src="https://media.geeksforgeeks.org/wp-content/cdn-uploads/20190710102234/download3.png" alt="This is GeeksforGeeks logo" /></body></html>

Output:

HTML Images - GeeksforGeeks (2)

HTML Images using alt attribute

Setting Width and Height of an Image

The width and height attributes are used to specify the width and height of an image. The attribute values are specified in pixels by default. The width and height attributes are always declared in pixels. Please refer to the How to set the width and height of an image using HTML? article for further details.

Example: In this example we sets the width and height of an image using the <img> tag’s width and height attributes. It showcases the GeeksforGeeks logo with dimensions of 300×300 pixels.

HTML
<!DOCTYPE html><html><head> <title> Setting width and height of image </title></head><body> <p> Setting width and height of image</p> <img src="https://media.geeksforgeeks.org/wp-content/cdn-uploads/20190710102234/download3.png" alt="GeeksforGeeks logo" width="300" height="300" /></body></html>

Output:

HTML Images - GeeksforGeeks (3)

HTML Image width and height Attribute example Output

Adding Titles to an Image in HTML

To add a title to an image, include the title attribute in the <img> tag, providing descriptive text for enhanced user interaction.Please refer to the HTML title Attribute article for further details.

Example: In this example we are showing an image with width, height, and title attributes using the <img> tag. The title attribute provides descriptive text for user interaction.

HTML
<!DOCTYPE html><html><head> <title> Inserting an image using "img" tag </title></head><body> <p>Inserted image using image tag</p> <img src="https://media.geeksforgeeks.org/wp-content/cdn-uploads/20190710102234/download3.png" alt="GeeksforGeeks logo" width="200" height="200" title="Logo of GeeksforGeeks" /></body></html>

Output:

HTML Images - GeeksforGeeks (4)

Adding titles to Image

Setting Style of an Image

In this example, we are using the border property to decorate the image. By default, every picture has a border around it. By using the border attribute, the thickness of the border can be changed. A thickness of “0” means that there will be no border around the picture. Please refer to the HTML <img> border Attribute for further details.

Example: This example illustrates the use of style property inside the <img> tag in HTML.

HTML
<!DOCTYPE html><html><head> <title>Setting border to image</title></head><body> <p>Setting style to the Image</p> <img src="https://media.geeksforgeeks.org/wp-content/cdn-uploads/20190710102234/download3.png" alt="GeeksforGeeks logo" width="200" height="200" border="5" /></body></html>

Output:

HTML Images - GeeksforGeeks (5)

Setting style to the Image Example Output

Setting Alignment of an Image in HTML

Aligning an image in HTML involves using the align attribute within the <img> tag to position it horizontally. Options include left, right, or center, enhancing page layout and visual appeal. Please refer to the HTML <img> align Attribute for further details.

Example: In this example we aligns an image to the right using the “align” attribute within the <img> tag. It positions the GeeksforGeeks logo for visual enhancement.

HTML
<!DOCTYPE html><html><head> <title>Aligning an image</title></head><body> <p>Aligning an Image</p> <img src="https://media.geeksforgeeks.org/wp-content/cdn-uploads/20190710102234/download3.png" alt="GeeksforGeeks logo" align="right" /></body></html>

Output:

HTML Images - GeeksforGeeks (6)

Aligning an Image Example Output

Adding Image as a Link

To add an image as a link, enclose the <img> tag within an <a> tag, setting the image’s source with the href attribute. This creates a clickable image linking to external content, such as images, videos, or other web pages.

File paths are of two types:

  • Absolute File Paths: It always contains the root element along with the complete directory list required to locate the file.
  • Relative File Paths: It is the hierarchical path representation that locates the file or folder on a file system beginning from the current directory.

Example: In this example we embeds the GeeksforGeeks logo as a clickable link using the <a> tag, wrapping the <img> tag with the link destination specified in the href attribute.

HTML
<!DOCTYPE html><html><head> <title>Adding image as link</title></head><body> <h3>GeekforGeeks</h3> <p>Adding image as link</p> <a href="https://ide.geeksforgeeks.org/tryit.php"> <img src="https://media.geeksforgeeks.org/wp-content/cdn-uploads/20190710102234/download3.png" alt="GeeksforGeeks logo" /> </a></body></html>

Output:

HTML Images - GeeksforGeeks (7)

Image As link Example Output

Adding Animated Image

To add an animated image in HTML, use the <img> tag with the src attribute pointing to a GIF file, providing engaging motion to enhance webpage content.

Example: This example explains the addition of an image in the GIF format in HTML.

HTML
<!DOCTYPE html><html><body> <h3>Adding a gif file on a webpage</h3> <img src="smiley.gif" alt="smiley" style="width: 200px; height: 200px" /> </body> </html>

Output:

HTML Images - GeeksforGeeks (8)

Common Image Format: Here is the commonly used image file format that is supported by all the browsers.

S.No.

Abbreviation

File Type

Extension

1.

PNG

Portable Network Graphics..png
2.

JPEG.

Joint Photographic Expert Group image..jpg, .jpeg, .jfif, .pjpeg, .pjp
3.

SVG

Scalable Vector Graphics..svg.
4.

GIF

Graphics Interchange Format..gif
5.

ICO

Microsoft Icon..ico, .cur
6.

APNG

Animated Portable Network Graphics..apng

Supported Browsers

  • Chrome
  • Edge
  • Firefox
  • Opera
  • Safari

HTML Images – FAQs

How do you insert an image in HTML?

Use the <img> tag with the src attribute. Example: <img src=”image.jpg” alt=”Description”>

What is the purpose of the alt attribute in an image tag?

The alt attribute provides alternative text for an image if it cannot be displayed and improves accessibility by describing the image to screen readers.

How do you specify the width and height of an image?

Use the width and height attributes. Example: <img src=”image.jpg” alt=”Description” width=”200″ height=”100″>

What is the src attribute?

The src (source) attribute specifies the path to the image file.

How do you link an image to a webpage?

Wrap the <img> tag inside an <a> tag. Example: <a href=”https://example.com”><img src=”image.jpg” alt=”Example”></a>



S

Shubrodeep Banerjee

HTML Images - GeeksforGeeks (9)

Improve

Previous Article

HTML Links Hyperlinks

Next Article

HTML Favicon

Please Login to comment...

HTML Images - GeeksforGeeks (2024)
Top Articles
Kent Short Term Rental Regulation: A Guide For Airbnb Hosts
Can I 1031 Exchange into a REIT?
English Bulldog Puppies For Sale Under 1000 In Florida
Katie Pavlich Bikini Photos
Gamevault Agent
Pieology Nutrition Calculator Mobile
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Compare the Samsung Galaxy S24 - 256GB - Cobalt Violet vs Apple iPhone 16 Pro - 128GB - Desert Titanium | AT&T
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Craigslist Dog Kennels For Sale
Things To Do In Atlanta Tomorrow Night
Non Sequitur
Crossword Nexus Solver
How To Cut Eelgrass Grounded
Pac Man Deviantart
Alexander Funeral Home Gallatin Obituaries
Energy Healing Conference Utah
Geometry Review Quiz 5 Answer Key
Hobby Stores Near Me Now
Icivics The Electoral Process Answer Key
Allybearloves
Bible Gateway passage: Revelation 3 - New Living Translation
Yisd Home Access Center
Pearson Correlation Coefficient
Home
Shadbase Get Out Of Jail
Gina Wilson Angle Addition Postulate
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Walmart Pharmacy Near Me Open
Marquette Gas Prices
A Christmas Horse - Alison Senxation
Ou Football Brainiacs
Access a Shared Resource | Computing for Arts + Sciences
Vera Bradley Factory Outlet Sunbury Products
Pixel Combat Unblocked
Movies - EPIC Theatres
Cvs Sport Physicals
Mercedes W204 Belt Diagram
Mia Malkova Bio, Net Worth, Age & More - Magzica
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Teenbeautyfitness
Where Can I Cash A Huntington National Bank Check
Topos De Bolos Engraçados
Sand Castle Parents Guide
Gregory (Five Nights at Freddy's)
Grand Valley State University Library Hours
Hello – Cornerstone Chapel
Stoughton Commuter Rail Schedule
Nfsd Web Portal
Selly Medaline
Latest Posts
Article information

Author: Dan Stracke

Last Updated:

Views: 5867

Rating: 4.2 / 5 (63 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Dan Stracke

Birthday: 1992-08-25

Address: 2253 Brown Springs, East Alla, OH 38634-0309

Phone: +398735162064

Job: Investor Government Associate

Hobby: Shopping, LARPing, Scrapbooking, Surfing, Slacklining, Dance, Glassblowing

Introduction: My name is Dan Stracke, I am a homely, gleaming, glamorous, inquisitive, homely, gorgeous, light person who loves writing and wants to share my knowledge and understanding with you.