×

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 Code Tag

The code tag in HTML is a phrase tag used to insert a piece of computer programming code. In HTML there is a separate tag named as <code> tag. Sometimes,...

3 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 Video

HTML VIDEO: HTML5 has introduced the <video> tag to embed video into the webpage directly without using the plug-ins.   Syntax: <video attributes >     <source src="address" type="video/mp4">     Your browser does not support the video tag.   </video> <video width="320" height="240" controls>     <source src="mountain.mp4" type="video/mp4">     Your browser does not support the video tag.   </video> Output: ← Prev ...

1 minute read.

HTML 5 Tutorial

What is HTML5? HTML5 is a programming language which stands for the acronym Hyper Text Markup Language 5. It is program that enables the appearance of web pages to be changed, as well as changes...

5 minutes read.

HTML Nested Table

HTML nested table refers to a table that is created inside another table. The table is in the form of rows and columns. From left to right are called horizontal rows,...

5 minutes read.

HTML &lt;body> alink Attribute

HTML <body> alink Attribute What is HTML? The HTML or HyperText Markup Language is used to design web pages. The file extension name for HTML file is ".html". It was released in...

2 minutes read.

HTML Frames

Frames in Html is used to divide the browser into different sections where each section can load different Html documents. Collection of frame is known as Frameset. Creating frames:We use frameset...

2 minutes read.

HTML Reset Button

The HTML reset button is used to reset all values in the form. It clears all input fields. Reset is the value of the type attribute of the button element. The...

6 minutes read.

HTML <strike> tag

The HTML <strike> tag is used to specifies strikethrough text. The tag is deprecated now and <del> tag is used in HTML files instead of it. All the global and...

2 minutes read.

HTML5 Attribute

HTML Attribute HTML attributes are special words that provide additional information about the HTML element or attributes.The element or tags have attributes, which define the element's behavior.It also adds attributes with a start...

3 minutes read.

HTML <sub> tag

The sub element in HTML is used to describe one or more subscript texts. If The sub tag shows the text which is presented in a smaller font and below...

2 minutes read.

Caption Tag in HTML

Caption Tag in HTML: The < caption > tag in HTML describes the heading of a table in the HTML text. Browsers typically make the text found above the table...

4 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 City tag

HTML <City> tag HTML < cite > tag specifies a citation, a reference or title to a creative work, quoted content, books, websites, a research paper, a blog post, a painting,...

3 minutes read.

Most Commonly used Tags in HTML

In this tutorial, we'll look at the most popular HTML tags. We know HTML is a fundamental requirement for creating a website. As a result, it is essential to remember...

9 minutes read.

HTML Text Format

Introduction HTML may format text in two different ways. The HTML tags define a text property to make it appear one way or another. A text element may also be formatted...

7 minutes read.

HTML Search Box

The HTML search box is also known as the search bar. It is an input text field that is used to search the content. If the users want to explore...

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

HTML <body> tag

HTML means Hyper Text Markup Language, which is most preferred for making Web pages. It explains how a Web page is put together. There are many different elements in HTML,...

3 minutes read.

HTML Anchor

HTML Anchor The HTML anchor tags define a hyperlink that links to other pages on one page. It can create hyperlinks to any web page and files, locations or any other...

3 minutes read.