×

How to insert image in HTML

An image (technically digital image) in HTML or even in general technical aspects is a binary representation of visual information. By visual information we mean, in the form of drawings/pictures, gifs/individual video frames, logos, or graphs etc.

“Inserting images in HTML” can be easily achieved with the help of <img> tag.  This tag is an empty tag, as it only contains attributes and hence the closing tag is not required here.

Note: The images can be inserted anywhere or in any section by using the <img> tag. We’ll see various examples regarding this as we proceed. The only point to remember regarding this is, that the <img> tag must be declared within the <body> </body> tag.

Example:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>HTML image</title>




  </head>
  <body>
    <img
      src="https://pixabay.com/get/g6da506c7a5c5b778d227c75e55fa3d525c8f87b42693d64cf7ec72f9807d423767579867119e9305f82e5ecf213f6dbb765c5e09f3ff6cc5f8c26037f828b1ce088515f71e3ed381fcba4240df5fa20a_1920.jpg"
      alt="Image of 'home in the hills'"
    />
  </body>
</html>

Syntax:

<img src = “image location” alt = “alt text” height = “in pixels” width = “in pixels“>
<img> stands for image tag
src stands for source tag, containing the address/location of the image.
alt containsthe text which is shown/displayed instead of the image, when the image fails to load, address is not found or any other error.
height for adjusting the height of the image.
width for adjusting the width of the image.

Note: We can have more attributes in the image tag like style, border, align etc. Above are the most commonly used attributes of image tag to define and declare basic structure of the image to be displayed. It is advised to always use the above 4 attributes in your HTML script/code.

*pixels: It can be of different sizes depending on the resolution of the screen you are displaying your output at.

Inserting image in HTML:

Although <img> tag is the tag that is used to insert images in HTML, there are various ways of inserting the image location in the src tag. These are as follows:

  1. Adding the hyperlink of the location of image
  2. Adding the image file from local storage
  3. Anchor <a> tag

Adding the hyperlink of the location of image

This is generally used for displaying images directly from the internet to your own HTML page. In this method, we simply take the URL of the image from the internet and pass the URL to the src tag of <img>.

Example:

File 1: index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>HTML image</title>
    <link rel="stylesheet" href="style.css" />
  </head>
  <body style="background-color: azure">
    <h1 class="contentinfo">Image in HTML (from internet)</h1>
    <img    src="https://i.pinimg.com/originals/f7/67/2d/f7672d04fc037f5b22cae37ef569919b.jpg"
      alt="Image of 'home in the hills'"
      height="300"
      class="center"
    />
  </body>
</html>

File 2: style.css

.center {
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 50%;
}
.contentinfo {
  color: rgb(122, 126, 42);
  border: 6px outset maroon;
  background-color: rgb(218, 246, 255);
  text-align: center;
}

Output:

How to insert image in HTML

Adding the image file from local storage

This is generally used for displaying images which are already present in your pc/local storage to your HTML page. In this method, we simply place the image file inside the destination folder (folder inside which you are currently saving your .html file), and then simply put the name of the image file inside the src tag of <img>.

Example:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>HTML image</title>
    <link rel="stylesheet" href="style.css" />
  </head>




  <body class="body">




    <h1 class="contentinfo">Image in HTML (from local storage)</h1>




   
<img
      src="home-in-the-hills.jpg"
      alt="Image of 'home in the hills'"
      height="300"
      class="center"
    />
  </body>
</html>

File 2: style.css

center {
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 50%;
}
.contentinfo {
  color: rgb(4, 35, 82);
  border: 6px outset maroon;
  background-color: #7dd56f;
}




.body {
  height: 100vh;
  display: flex;
  align-items: center;
  background: linear-gradient(to top left, #28b487, #7dd56f);
}

Output:

How to insert image in HTML

Anchor <a> tag

<a> stands for anchor tag, and is used to inherit URL inside our HTML. We can use it to create an image as hyperlink or for below stated points:

  1. The message displayed by alt tag when the image src is absent/not working etc.
  2. Use of anchor tag to inherit URLs in our HTML.

Example:

File 1 = index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>HTML image</title>
    <link rel="stylesheet" href="style.css" />
  </head>
  <body class="body">
    <h1 class="contentinfo">Image in HTML (from internet)</h1>
    <a    href="https://i.pinimg.com/originals/f7/67/2d/f7672d04fc037f5b22cae37ef569919b.jpg" />
    <img alt="Image of 'home in the hills'" height="300" class="center" />
  </body>
</html>

File 2: style.css

center {
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 50%;
}
.contentinfo {
  color: rgb(4, 35, 82);
  border: 6px outset maroon;
  background-color: #7dd56f;
}
.body {
  height: 100vh;
  display: flex;
  align-items: center;
  background: linear-gradient(to top left, #28b487, #7dd56f);
}

Output:

How to insert image in HTML

Conclusion:

<img> tag is used to insert images in our HTML page. The tag is an empty tag and hence does not require the closing tag.

The images can be inserted anywhere or in any section by using the <img> tag. The only point to remember regarding this is, that the <img> tag must be declared inside the <body> “ “  </body> tag.

<img    src="https://i.pinimg.com/originals/f7/67/2d/f7672d04fc037f5b22cae37ef569919b.jpg"
      alt="Image of 'home in the hills'"
      height="300"
    />

Note: We can mention both height and width attributes here, but we only mention either of them to maintain the aspect ratio of the image. By doing so, the aspect ratio is automatically maintained and we don’t have to calculate it manually.


Related Topics

HTML Paragraphs

In html, there are some elements which starts with new line and one of them is paragraph. Which is said to be a block of text. In html <p> tag...

4 minutes read.

How to append HTML code to a div using JavaScript?

In HTML, we have a div tag which is used to create a box where we can put a lot of other tags. So it works like a container. So,...

4 minutes read.

HTML Elements

An HTML element is nothing but a starting tag and then some content and then the closing tag. So we can say HTML elements is everything from the starting tag...

3 minutes read.

HTML <table> tag

The Table element If we want to show the data and information in a two-dimensional table with rows and columns, then we can achieve this by using the HTML element <table>. Syntax: <body> <table> <thead> <tr> <thcolspan="#">………….</th> </tr> </thead> <tbody> <tr> <td>……….</td> <td>……….</td> </tr> </tbody> </table> </body> Example: <!DOCTYPE...

9 minutes read.

HTML Basic Tags

Heading Tags: This tag represents the document header. It is said to be a good practice if every document starts with Heading. Heading tags ranges from H1 to H6 i.e....

3 minutes read.

HTML5 Formatting

HTML Formatting HTML Formatting is a method of text formatting for a better appearance and look. HTML allows us to format text without using the CSS. HTML includes a lot of formatting tags....

6 minutes read.

HTML5 Button Tag

HTML Button Tag The < button > tag is used on the webpage to construct a clickable button inside the HTML. Within the < button>....</button > tag, we can place content...

2 minutes read.

Html Wbr Tag

The Line Break Opportunity element A word break opportunity is a place in text from where the browser breaks a line. This opportunity is represented by the element wbr in the...

2 minutes read.

HTML Font Color

The HTML <font> tag has an attribute called color, which defines the color of the text. We use the color attribute inside the <font> tag to specify the color of...

6 minutes read.

HTML Data Tag

HTML Data Tag: The HTML < data > tag is used to transmit its content to a machine-readable version. The data are presented in a particular format. It is useful...

3 minutes read.

Difference between HTML and DHTML

Introduction to HTML HTML stands for Hypertext Markup Language. It is a standard web language used to create web pages and applications. HTML is used to define the structure and content...

4 minutes read.

How to create a link with no underline in HTML

In HTML, an anchor tag (<a>) is a Styled Container tag, which means when a hyperlink is created using this, browsers, by default, display the link by applying some styles...

5 minutes read.

HTML Headings

The titles and subtitles of our webpages are known as HTML headings. Let’s not talk about webpages but giving heading to almost everything like normal paragraphs, articles or let it...

2 minutes read.

Responsive HTML5 and CSS3 Tutorial

Introduction to HTML5 Tutorial HTML5 is the latest and upgraded version of HTML. HTML is a hypertext markup language. Technically, it is not a programming language. It is a markup language. HTML...

17 minutes read.

HTML <video> Tag

The Video tag in HTML is used to play video on a media player on the web page. We can also insert an audio by the video tag, but audio...

6 minutes read.

How to insert spaces/tabs in text using HTML/CSS

What is HTML? HTML or HyperText Markup Language is used to design web pages. It was released in 1993. The file extension name for HTML file is “.html”. An HTML file...

4 minutes read.

HTML <svg> tag

SVG is used to determine the vector-based graphics in XML format. What is SVG? We utilize SVG tag to specify visuals for the website. The attribute of SVG in a HTML document...

2 minutes read.

Html <colgroup> Tag

A HTML table's column group is specified by the "colgroup" tag. If you want to apply various properties to one or more <col> elements in an HTML table, then you...

4 minutes read.

HTML <source> tag

For media attributes, such as <video> tag, <audio> tag, and <picture> tag, we use the <source> tag in html for adding such type of multiple media html elements in our...

4 minutes read.

HTML <sup> Tag

The HTML sup element is used to describe one or more superscript contents. The sup tag shows a text which is presented above the regular line and half the length...

2 minutes read.