HTML Image

Images in Html

HTML Image tag is used to display image on the web page. HTML img tag is an empty tag that contains attributes only, closing tags are not used in HTML image element.

Attributes of HTML img tag:

The src and alt are important attributes of HTML img tag. All attributes of HTML image tag are given below.

  1. src : It is a necessary attribute that describes the source or path of the image.
  2. alt : The alt attribute defines an alternate text for the image, if it can’t be displayed.
  3. height : It h3 the height of the image. The HTML height attribute also supports iframe, image and object elements.
  4. width : It is an optional attribute which is used to specify the width to display the image.

Insert Image:

You can insert any image in your web page by using <img> tag. Following is the simple syntax to use this tag.

Example:

<img src = “Image URL” … attributes-list/>

The <img> tag is an empty tag, which means that, it can contain only list of attributes and it has no closing tag.

Example:

<!DOCTYPE html>
<html>
<head>
<title>Using Image in Webpage</title>
</head>
<body>
<p>Simple Image Insert</p>
<img src = “/html/images/test.png” alt = “Test Image” />
</body>
</html>

You can use PNG, JPEG or GIF image file based on your comfort but make sure you specify correct image file name in src attribute. Image name is always case sensitive.

Set Image Location:

Usually we keep all the images in a separate directory. So let’s keep HTML file test.htm in our home directory and create a subdirectory images inside the home directory where we will keep our image test.png.

Example:

<!DOCTYPE html>
<html>
<head>
<title>Using Image in Webpage</title>
</head>
<body>
<p>Simple Image Insert</p>
<img src = “/html/images/test.png” alt = “Test Image” />
</body>
</html>

Set Image Width/Height:

You can set image width and height based on your requirement using width and height attributes.

Example:

<!DOCTYPE html>
<html>
<head>
<title>Set Image Width and Height</title>
</head>
<body>
<p>Setting image width and height</p>
<img src = “/html/images/test.png” alt = “Test Image” width = “150” height = “100”/>
</body>
</html>

Set Image Border:

By default, image will have a border around it, you can specify border thickness in terms of pixels using border attribute. A thickness of 0 means, no border around the picture.

Example:

<!DOCTYPE html>
<html>
<head>
<title>Set Image Border</title>
</head>
<body>
<p>Setting image Border</p>
<img src = “/html/images/test.png” alt = “Test Image” border = “3”/>
</body>
</html>

Set Image Alignment:

By default, image will align at the left side of the page, but you can use align attribute to set it in the center or right.

Example:

<!DOCTYPE html>
<html>
<head>
<title>Set Image Alignment</title>
</head>
<body>
<p>Setting image Alignment</p>
<img src = “/html/images/test.png” alt = “Test Image” border = “3” align = “right”/>
</body>
</html>

Recommended Posts:

Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *