- How to Add Image in the Title Bar
- Let the browser finds the favicon!
- Let’s use HTML link tag:
- Depending on the favicon format, the type attribute must be changed:
- A favicon must have the following characteristics:
- Example of adding an image in the title bar:
- Different Platforms:
- Add it to your code in the following way:
- HTML Images
- HTML Image src Attribute
- HTML Image alt Attribute
- HTML Image title Attribute
- Lazy loading HTML images
- HTML Image Size — Width and Height
- Common Image Formats
- Table of Contents
- HTML Images
How to Add Image in the Title Bar
Most websites add an icon or image logo in the title bar. This icon logo is also called a favicon and can be useful for user engagement.
A favicon, also known as a URL icon, a tab icon, a shortcut icon, a website icon, or a bookmark icon, is a file containing one or more small icons associated with a particular website or web page.
You can let the browser detect your favicon or upload it to your hosting root area.
Let the browser finds the favicon!
One way, which is the easiest, is by uploading an icon as a .png or .ico file from your hosting’s File Manager. If you prepare your image in png or ico, you can upload it to your hosting area, and most modern browsers will automatically detect favicon.png and favicon.ico files from your host (naming matters here, it should be exactly favicon). So, you’ll need no coding.
The other way is by using the HTML link inside the head tag.
Let’s use HTML link tag:
/* it should be placed inside the head tag */ icon" type="image/png" href="path-to-your-favicon"/>
If you use a different image format, implement the appropriate changes (read the next paragraph), and change the value of the href attribute to the path where your image is located in your project. The ICO format is not as reliable anymore, and it’s better to use png (to preserve transparency).
Depending on the favicon format, the type attribute must be changed:
- For PNG, use image/png.
- For GIF, use image/gif.
- For JPEG, use image/jpg.
- For ICO, use image/x-icon.
- For SVG, use image/svg+xml.
/* for gif files, for example, it should look like this: */ /* path-to-favicon should be changed to the location of your favicon file */ icon" type="image/gif" href="path-to-favicon">
A favicon must have the following characteristics:
- Favicon.ico is the default name.
- Size should be 16×16, 32×32, 48×48, 64×64 or 128×128 pixels.
- Color should be 8 bites, 24 bites or 32 bites.
There are a lot of online tools that will help you create a favicon, convert the image formats, etc., and you can find them by a simple search on Google, by the way.
The image must be square dimensioned in any image format (ico, jpg, BMP, gif, png) to appear in browsers properly. Images with non-square dimensions will also work. However, such icons may not look professional.
Example of adding an image in the title bar:
html> html> head> title>Title of the document title> link rel="icon" type="images/x-icon" href="https://www.w3docs.com/favicon.ico" /> head> body> h1>Hello from W3docs! h1> body> html>
If you take a look at the result of the above code, it should be something like this.
Different Platforms:
For different platforms, the favicon size should also be changed:
Platform | Name | Rel value | Favicon size |
---|---|---|---|
Google TV | favicon-96×96.png | icon | 96×96 |
Ipad Retina, iOS 7 or later | apple-touch-icon-152×152-precomposed.png | apple-touch-icon-precomposed | 152×152 |
Ipad Retina, iOS 6 or later | apple-touch-icon-144×144-precomposed.png | apple-touch-icon-precomposed | 144×144 |
Ipad Min, first generation iOS 7 or later | apple-touch-icon-76×76-precomposed.png | apple-touch-icon-precomposed | 76×76 |
Ipad Mini, first generation iOS 6 or previous | apple-touch-icon-72×72-precomposed.png | apple-touch-icon-precomposed | 72×72 |
Iphone Retina, iOS 7 or later | apple-touch-icon-120×120-precomposed.png | apple-touch-icon-precomposed | 120×120 |
Iphone Retina, iOS 6 or previous | apple-touch-icon-114×114-precomposed.png | apple-touch-icon-precomposed | 114×114 |
Add it to your code in the following way:
icon" href="/favicon.ico" type="image/x-icon"> icon" href="/favicon.png" sizes="32x32" type="image/png"> icon-precomposed" href="/apple-touch-icon-152x152-precomposed.png" type="image/png" sizes="152x152"> icon-precomposed" href="/apple-touch-icon-120x120-precomposed.png" type="image/png" sizes="120x120"> icon" href="/favicon-96x96.png" sizes="96x96" type="image/png">
Please note that you must change the href attribute value based on the location of your favicon file, either in your host or in your project folder if you’re working locally. And the naming matters; otherwise, the browser can not detect it.
In the end, it is worth noting that you can use this open-source link which is a cheat sheet for favicon and has valuable information that may help you along the way.
HTML Images
Here, the tag inserts the image logo.png on the webpage.
The HTML image tag has 2 important attributes:
Note: The tag is an empty tag, i.e. It doesn’t require a closing tag.
HTML Image src Attribute
The src attribute is a required attribute for the tag. It specifies the path (URL) to the image. It tells the browser where to look for the image. For example,
Browser Output
In the above example, the src attribute tells the browser to load the image from the same directory where the webpage is located.
Similarly, if the image is stored in a subdirectory, the path in the src attribute would be written as
If the image is located on another server entirely or if we want to use an image from the web, we can provide an absolute URL for the image. For example,
Note: Although we can provide an absolute path for images located in our own web server, better to provide a relative path as it leads to faster loading times. Hence always use relative paths for images in your own server.
HTML Image alt Attribute
The alt attribute is the text description of an image. The value of the alt attribute should describe the image such that the content is meaningful even if the image fails to load.
Browser Output
The alt attribute’s contents are shown if the user cannot view the image (slow internet, wrong src attribute, or using a screen reader).
It also helps in SEO as it helps the search engine understand what images are about.
HTML Image title Attribute
The title attribute displays the information about the image when the user hovers over the image. For example,
Browser Output
Note: Although alt and title attributes appear similar, the title attribute cannot be used as an alternative to the alt attribute. The title is not read by screen readers and does not display when the image fails to load.
Lazy loading HTML images
By default, all the images on the web page are loaded during the initial load. If there are many images on the page, this causes the page to load slowly.
We can change this behavior of HTML images by using the loading attribute.
Now the image will not load until the user scrolls near the image location. If the image is at the bottom of the page and the user never scrolls down on the website, the image will never load.
This decreases the website’s initial load time and prevents unnecessary data fetching.
HTML Image Size — Width and Height
We can use the style attribute to specify the height and width of an Image. For example,
Alternatively, we can also use the height and width attributes to set the height and width. For example,
Browser Output
We should always set the height or width of images. Otherwise when the image loads, the content on the webpage will be shifted.
Note: The above two code samples give the same output, however, it is best practice to use the style attribute rather than height and width .
Common Image Formats
Format | File Format | Extension |
---|---|---|
GIF | Graphics Interchange Format | .gif |
PNG | Portable Network Graphics | .png |
SVG | Scalable Vector Graphics | .svg |
JPEG | Joint Photographic Expert Group image | .jpg, .jpeg |
WEBP | Web Picture | .webp |
Table of Contents
HTML Images
In this article, we will know the HTML Image, how to add the image in HTML, along with knowing its implementation & usage through the examples. In earlier times, the web pages only contains textual contents, which made them appear quite boring and uninteresting. Fortunately, it wasn’t long enough that the ability to embed images on web pages was added for users. In this article, we will know how to add images to the web page that will make the website attractive & various methods to insert the images.
There are 2 ways to insert the images into a webpage:
- By providing a full path or address (URL) to access an internet file.
- By providing the file path relative to the location of the current web page file.
We will first discuss inserting the image to the webpage & simultaneously, we will understand both the above approaches.
Adding images on a webpage: The tag is used to add or embed the images to a webpage/website. The “img” tag is an empty tag, which means it can contain only a list of attributes and it has no closing tag. The addition of the images improves the quality along with enhancing the design structure, appearance of the webpage. Nowadays, a website does not directly add images to a web page, as the images are linked to web pages by using the tag which holds space for the image.
Attribute: The tag has following attributes:
- src: It is used to specify the path to the image.
- alt: It is used to specify an alternate text for the image. It is useful as it informs the user about what the image means and also due to any network issue if the image cannot be displayed then this alternate text will be displayed.
- crossorigin: It is used to import images from third-party sites that allow cross-origin access to be used with canvas.
- height: It is used to specify the height of the image.
- width: It is used to specify the width of the image.
- ismap: It is used to specify an image as a server-side image map.
- loading: It is used to specify whether a browser should defer the loading of images until some conditions are met or load an image immediately.
- longdesc: It is used to specify a URL to a detailed description of an image.
- referrerpolicy: It is used to specify which referrer information to use when fetching an image i.e. no-referrer, no-referrer-when-downgrade, origin, origin-when-cross-origin, unsafe-url.
- sizes: It is used to specify image sizes for different page layouts.
- srcset: It is used to specify a list of image files to use in different situations.
- usemap: It is used to specify an image as a client-side image map.
src: The src stands for source. Every image has an src attribute which tells the browser where to find the image you want to display. The URL of the image provided points to the location where the image is stored. When the webpage loads for the first time, then the browser gets the image from a web server and inserts it into the page. If the image is not spotted by the browser then users will get a broken link icon. It might be possible if the file path is wrong or the image got deleted from that location.
Example 1: This simple example illustrates the use of the tag in HTML that is used to embed the image into the webpage.