How to include image in HTML
Images are an internal part that is widely used to depict any concept in a simplified manner. They are used to enhance the design and the display of a web page.HTML language provides many inbuilt tags for easily inserting an image in a webpage.
In this tutorial, we will learn the various tags to include images in HTML.
HTML Images Syntax
1. <IMG>
Technically, Images are never inserted in any webpage. Rather, they are linked to their respective web pages. The HTML <img> tag is commonly used to insert an image in any web page. The <img> tag works by creating a holding space for the selected image. By default, the HTML <img> tag is empty and doesn’t hold any value. It only contains attributes that do not include a closing tag.
The two attributes of the <img> tag are as follows:
- IMG src attribute- This attribute defines the path to the specified image.
- IMG alt attribute – This attribute fetches an alternate text for the given image
2. IMG src Attribute
The HTML <img> src attribute defines the path (URL) to the image.
Note: Whenever your browser loads any web page, at the same moment, it is the responsibility of your browser to fetch the web pages’ images from a web server and include it into the website. Therefore, the HTML programmer should always ensure that the image lingers in the same spot concerning the web page. Otherwise, your users will receive a broken link icon which further displays a text showing the browser cannot find the image.
Syntax
Code:
Output
3. alt Attribute
The HTML alt attribute is used to provide an alternate text for the given image, if the website user cannot view the image due to delay in loading, slow internet connections or any other reason.
The content of the alt attribute should be precise and it should describe the purpose of the image.
Syntax
Code:
Output
4. Image Size – Width and Height
After inserting the image, it is important to specify the height and width, so it fits in the webpage properly. Therefore, HTML provides the style attribute to define the height and width of an image.
Syntax
Code:
Output
5. HTML Image as a link
HTML provides the feature to use the image as a link. For this the HTML programmer need to use the <img> tag inside the HTML <a> tag:
Syntax
Code:
Output
6. Include Image in Another folder
Many times the user have a requirement to include the images in a sub-folder, in that case you must contain the folder name in the src attribute:
Syntax
Code:
Output
7. HTML Animated Images
HTML enables its users to embed moving images or GIF as well in the code.
Syntax
Code:
Output
Commonly used Images Format
Image File Format abbreviation | Full Form | File extension |
---|---|---|
APNG | APNG stands for Animated Portable Network Graphics | . apng |
GIF | GIF stands for Graphics Interchange Format | .gif |
ICO | ICO stands for Microsoft Icon |
|
PNG | The abbreviation PNG stands for Portable Network Graphics | |
SVG | The full form of SVG is Scalable Vector Graphics | |
JPEG | The full form of JPEG is Joint Photographic Expert Group image |
|
How to include background images?
An HTML programmer can also specify a background image for any HTML code by using “background-image” property.
1. Background Image on a HTML element
The HTML programmer can implement a background image on any HTML element by using the HTML style property and the background-image feature.
Syntax
Code:
Output
2. Background Image on a Page
If the HTML programmer wants to incorporate the background image to its entire page, they must use the background image inside the HTML <body> element:
Syntax
Code:
Output
3. Background Image Repeat
If the specified background image is small in size than the element, in that case, the image will automatically replicate itself in both ways (horizontally and vertically), unless and until it extends to the end of the page:
Syntax
Code:
Output
4. Background Cover
HTML provides the background-size property, which helps the user set the entire element with the given background images. This method covers the entire element without stretching the image and therefore retaining the original proportions of the image.
Syntax:
Code:
Output