HTML Images

The HTML images are an empty tag means it does not have a closing tag. It has the following required attributes namely src & alt. The src attribute is used to provide a path of the existing image while the alt attribute shows a message when the image path is not correct or missing.

General syntax for HTML image element:

    
  <img src="URL of the image" alt="Specify about image">
   
    
 <img src="https://picsum.photos/200/300" alt="This is an external image">
   

This paragraph having an image tag also. The image can be aligned on the left side by using the CSS cascade style sheet rule. The image's width and height can also adjust by a CSS rule.

Source Code

          <img src="https://picsum.photos/200/300" alt="External image source">
        
Try it now

Here, the src refers to the address of the image while the alt attribute specifies the image name which will be visible when the image address is not to be correct.

Image Width And Height

Width and height can be set either by <img> width or height attributes of a CSS style.

    
     <img src="https://picsum.photos/200/300" width="300" height="300" alt="This is an external image" >
   

Source Code

          <img src="https://picsum.photos/200/300" alt="image" width="300" height="300">
        
Try it now

Width and height can be set by <img> width and height attribute.

Assigning Image Width & Height Using CSS

    
    img{
     width:300px;
     height:300px;
     }
   

Source Code

          img {
  width: 300px;
  height: 300px;
}
        
Try it now

Note: Please keep in mind that image width and height can be set using CSS.

Positioning Of Images

Images can be positioned either left or right by the CSS cascade style sheet rule.

Image:Left Alignment

    
    img
    {
       float:left;
     }
   

Source Code

          <img src="https://picsum.photos/200/200"  alt="image"  style="float: left;">
        
Try it now

CSS float: The left property is used to assign the HTML element to the left of the content while float: right property is used to assign the HTML content to the right of the content.

Image:Right Alignment

    
     <img src="https://picsum.photos/200/300" alt="image" style="float: right;width: 300px;height: 300px;">
   

Source Code

          <img src="https://picsum.photos/200/300" alt="Image " style="float: right;width: 300px;height: 300px;">
        
Try it now

Image can also be shift right to the content using CSS float: right property.

Providing Link To The Images.

    
    <a href="https://sudhakarinfotech.com/shop" target="_blank">
      <img src="https://picsum.photos/200/300" alt="image">
   </a>
   

Source Code

          <a href="https://sudhakarinfotech.com/shop" target="_blank">
  <img src="https://picsum.photos/200/300" alt="External Image Resources" />
</a>
        
Try it now

Images link will be provided by an anchor tag <a> element. it has also a closing tag </a> and attributes href that provides the linking address of the web page.

Path Determination Of Images

    
    sudhakarinfotech
       ext(folder)
          -images/result.png
       display.html
       another-result.png 
   

If an image exists within display.html then its path will be.

    
    <img src="ext/images/result.png" alt="external image">
    <img src="another-result.png" alt="alternative image">
   

Explanation -If movement is done from parent to child folder then use forward-slash(/) for every folder.

Source Code

          <img src="../images/logo3.png" alt="external image link" />
<img src="../logo.png" alt="external image link" />
        
Try it now

Explanation -If movement is done from parent to child folder then use forward-slash(/) for every folder.

There is also another case can be exist.

    
       sudhakarinfotech
       ext(folder)
          -display.html
          -images/result.png
       
       another-result.png
   

Image path existing into display.html will be:

    
    <img src="images/result.png" alt="external image ">
    <img src="../another-result.png" alt="alternative image">
   

Please remember that, to move one directory up, you must have to use (../).

Source Code

          <img src="../images/logo3.png" alt="external image link" /> 
<img src="../logo.png" alt="external image link " />
        
Try it now

Please remember that, to move one directory up, you must have to use (../).

Online Test / Quiz

Web Tutorials

HTML Images
Html Tutorial HTML
Javascript Tutorial JAVASCRIPT
Css Tutorial CSS
Bootstrap 5 Tutorial BOOTSTRAP 5
Bootstrap 4 Tutorial BOOTSTRAP 4