close
The Wayback Machine - https://web.archive.org/web/20240912125008/https://www.geeksforgeeks.org/html-images/
Open In App

HTML Images

Last Updated : 03 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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

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:

AltAttribute

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:

HTMLImageWidthHeightAttribute

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:

Screenshot-2024-07-30-205724

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:

Image-styling

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:

Aligning-an-Image

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: 

ImageAsLink

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:

ezgifcomgifmaker2_a5ac_v1

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

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>



Previous Article
Next Article

Similar Reads

Bootstrap 5 Images Aligning Images
Bootstrap 5 Aligning Images are used to set the alignment to the image. We will use float or text alignment classes to set the alignment of images. We can use the .mx-auto margin utility class to center the block-level images. Aligning Images used Classes: .float-start: This class is used to set the position of an element to left..float-end: This c
2 min read
Bootstrap 5 Images Responsive images
Bootstrap 5 Responsive images are used to resize the images according to their parent element and screen sizes. It means, the size of the image should not overflow its parent element and will grow and shrink according to the change in the size of its parent without losing its aspect ratio. Responsive images using Class:.img-fluid: This class is use
1 min read
HTML Canvas Images
In this article, we will see HTML Canvas Images. We generally use images on our web page through &lt;img&gt; element. In this article, we will see HTML Canvas images in HTML and JavaScript. The drawImage() Method is used to embed images or video on Canvas. With this method, we can also change the height and width of the image and clip the image. Im
3 min read
How to Create Avatar Images using HTML and CSS ?
This article will show you how to create an Avatar Image with the help of HTML and CSS. An Avatar is a visual representation of a user, often used in user profiles or comment sections on websites. Here, we have created a GFG logo avatar image. This avatar has 100px width and height, and border-radius to 50%. Example: C/C++ Code &amp;lt;!DOCTYPE htm
1 min read
How to display Base64 images in HTML ?
Base64 is a method for encoding binary data into ASCII text. Displaying Base64 images in HTML involves using the &lt;img&gt; tag with the src attribute set to a Base64 data URL, containing the image data encoded as ASCII text. This method embeds images directly into HTML. Base64 images are encoded binary data, allowing images to be embedded in web
3 min read
HTML Background Images
HTML background images are graphics applied to the background of HTML elements, often used in webpage design for aesthetic or branding purposes. They're set using CSS background-image property, allowing for single or repeating images, gradients, or patterns to enhance visual appeal. Understanding HTML Background ImagesHTML Background Images allows
3 min read
How to use SVG Images in CSS & HTML?
SVG, or Scalable Vector Graphics, is a unique image format for vector-based graphics. Reasons to use SVG images include maintaining quality when zoomed, easy editing, accessibility, small file size, scalability, and searchability. There are several ways to use SVG images in CSS and HTML. Table of Content Using SVG as an Using SVG as a CSS backgroun
2 min read
How to Fix Images Not Showing in HTML?
One common issue web developers encounters is images not displaying on their HTML pages. This problem can be caused by the variety of reasons including the incorrect file paths, permissions issues and browser-related problems. In this article, we will explore several methods to diagnose and fix the issue of images not showing up in HTML. Common Cau
3 min read
Lazy Loading images in HTML
What is Lazy Loading? Lazy loading is a strategy to identify resources as non-critical and load these only when needed. It's a way to optimize web pages, which translates into reduced page load times. To read more about it:  https://www.geeksforgeeks.org/what-is-lazy-loading/ Generally images are larger in size, so lazy loading can be used to defer
2 min read
Create a Resize and Compress Images in HTML CSS & JavaScript
While using the GeeksforGeeks Write Portal to write articles, we need to upload the images. As we need to resize the image as per GeeksforGeeks's requirement, we search for different tools and websites on the internet to resize and compress the image. But, as a web developer, we can create our own image resize and compressor using HTML, CSS, and Ja
7 min read
How to create a drag and drop feature for reordering the images using HTML CSS and jQueryUI ?
In this article, we are given an images gallery and the task is to re-arrange the order of images in the list or grid by dragging and dropping. The jQuery UI framework provides a sortable() function that helps in re-ordering list items by using the mouse. With this functionality, the list items become interchangeable. The jQuery UI provides a sorta
3 min read
HTML DOM images Collection Property
The images collection property in HTML is used to return the collection of &lt;img&gt; elements in the document. It can be used for knowing the count of images inserted in the document using the &lt;img&gt; tag. The &lt;input&gt; elements with type = image are not counted in the image property. Syntax:  document.imagesProperty: It returns the numbe
4 min read
How to Show Images on Click using HTML ?
Display Images on Click Using HTML and JavaScript refers to techniques that dynamically show images when a user interacts with a webpage, such as clicking a button. This enhances user engagement by making web pages more interactive, using simple HTML, CSS, and JavaScript methods. Table of Content Changing Display Properties of &lt;img&gt; Attribute
4 min read
How To Use Images As Buttons In HTML ?
Ever seen a website with a cool image that, when clicked, performs an action like submitting a form? That's what happens when you use images as buttons in HTML. Images can be used as interactive buttons in HTML, offering a visually appealing way to enhance user interfaces. For In this article, we will explore various approaches to implementing imag
3 min read
How to Arrange Images and Text in HTML?
In HTML, arranging images and text is essential for creating visually appealing and readable web pages. We can position images in various ways to achieve the desired layout, including the inline with text, above, below, or beside it. Understanding how to align and arrange these elements helps to enhance the user experience. We are going to arrange
6 min read
How to smoothly transition CSS background images using jQuery?
In this article, we will learn to implement a smooth transition of background images using jQuery and CSS. Approach: First, we add a transparent background image using the background-image property in CSS in the style tag. background-image: url(white.jpg); For the transition background image, we use the transition property in CSS and add a transiti
2 min read
How to set multiple background images using CSS?
The multiple background images for an element can be put in the HTML page using CSS. Use CSS background property to add multiple background images for an element in any pattern and use other CSS property to set the height and width of the images. The used background property are listed below: background-image: url(), url(), ...; This property is us
2 min read
CSS Border Images
The border-image property in CSS is used to specify the border of an image. This property creates a border using an image instead of a normal border. The property contains three-part that are listed below: The complete image is used as a border.A slice of the image is used as a borderThe middle section of the image is used (repeated or stretched) a
2 min read
How to download Google Images using Python
Python is a multi-purpose language and widely used for scripting. We can write Python scripts to automate day-to-day things. Let’s say we want to download google images with multiple search queries. Instead of doing it manually we can automate the process. How to install needed Module : pip install google_images_download Let’s see how to write a Py
2 min read
How to display multiple horizontal images in Bootstrap card ?
Pre-requisite: Bootstrap Cards Bootstrap cards provide a flexible and extensible content container with multiple variants and options such as styling the Tables, stacking multiple images horizontally/vertically, making the stacked contents responsive, etc. Cards include so many options for customizing their backgrounds, borders, Header, footer, col
2 min read
Why card images use data-src (not src) for image in Bootstrap 4 ?
Bootstrap 4 card images use src in image tags to providing the location to be loaded in the given image tag and data-src to provide additional information that can be used by JavaScript for improving the users' experience. For data-src and src tag we can conclude as follows: src: If you want to load and display a particular image, then use .src to
3 min read
How to use grid for images in bootstrap card ?
Images can be added to the Bootstrap card by simply using an img tag. But we cannot use the same method directly to place an Image Grid into the Bootstrap Card as it will be lead to a misaligned design. Therefore, to get a per-flow to place image Grid into Bootstrap Card. The perfectly aligned grid we need to add some CSS to our HTML code. Approach
2 min read
How to put images in a box without space using Bootstrap?
Bootstrap 4 is a CSS a library for frontend web development developed by Twitter which helps the developer to beautify the way the webpage looks. The bootstrap library has pre-defined CSS classes that are easy to use and it helps the developer to provide an artistic look to the website with minimal effort. The following line shows the syntax for in
3 min read
Semantic-UI | Images
Semantic UI is an open-source framework that uses CSS and jQuery to build great user interfaces. It is the same as a bootstrap for use and has great different elements to use to make your website look more amazing. It uses a class to add CSS to the elements.There are different styles of putting images in our web app using semantic-ui to make out we
2 min read
How to display images in Angular2 ?
We can serve an image in angular2 by first placing the image in the assets directory of your project where you can create a specific directory for the images or just keep it in the assets directory as it is. Once you have put all the required images in the assets directory open the specific component typescript (.ts) file where you want to serve th
2 min read
SASS variable for images path
Whenever we want to add an image to our webpage, we need not always write the full path of our images when using SASS. We can rather store the path to the images folder in a variable. It is always good to add the path of a folder to a variable. Declaring a variable Syntax : $assetPath :"path"; Adding a variable in image path Syntax : background: ur
1 min read
CSS | Polaroid Images
A Polaroid image is an image box with a caption below it. In this article, you will learn to create polaroid images using HTML and CSS. This can be done either by using the HTML &lt;div&gt; or &lt;figure&gt; tags. 1. Using div: The polaroid image is created by using the HTML div tag and by adding CSS properties as shown below. Use image width to 10
3 min read
How to align images in card with dynamic title?
Several times it is needed to display information on a website in the form of cards. Cards are basically a component containing a heading, an image, and an optional description. The way in which we align the images, headings, and text in our cards defines the aesthetics, that is, the look and feel of the card. However, there might be problems in de
3 min read
How to create a Circular/Rounded images using CSS ?
In this article, we will create a rounded image with CSS. It can be done by using the CSS border-radius property. This property is mainly used to make round shapes. It contains a numeric value in the form of pixels. Example 1: C/C++ Code &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;style&gt; img { border-radius: 58%; } &lt;/style&gt; &lt;/he
1 min read
How to make div that contains images to have fixed size using Bootstrap ?
In this article, we will see how we can make a div that will contain images having a fixed size. This can be very helpful when designing a webpage for displaying a certain type of product. We will use bootstrap to implement the solution for the above problem. Bootstrap CDN link: https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.c
1 min read