HTML Head

The HTML <head> element is used as a container for metadata (data about data). It is used between <html> tag and <body> tag.

The head of an HTML document is a part whose content is not displayed in the browser on page loading. It just contains metadata about the HTML document which specifies data about the HTML document.

An HTML head can contain lots of metadata information or can have very less or no information, it depends on our requirement. But head part has a crucial role an HTML document while creating a website.

Metadata defines the document title, character set, styles, links, scripts, and other meta information.

Following is a list of tags used in metadata:

  • <title>
  • <style>
  • <meta>
  • <link>
  • <script>
  • <base>

HTML <title> Element

The HTML <title> element is used to define the title of the document. It is used in all HTML/XHTML documents. The <title> element must be placed between <head> element, and one document can only have one title element.

What does <title> element do?

  1. It defines a title in the browser tab.
  2. It provides a title for the page when it is added to favorites.
  3. It displays a title for the page in search engine results.

Note: The title element must be specific about the document and its recommended length is 65 to 70 characters including spaces.

Example:

Test it Now

HTML <style> Element

The HTML <style> element is used to style the HTML page. The <style> element can have CSS properties for that HTML page only. If we want to apply CSS for multiple pages then we should use separate CSS file.

Example:

Test it Now

HTML <link> Element

The HTML <link> element is used to link an external style sheet to your webpage. The <link> element contains main two attributes which are "rel" and "href". The rel attribute indicates that it is a stylesheet, and href gives the path to that external file.

Example:

Test it Now

HTML <meta> Element

The HTML <meta> element is used to specify the character set, page description, keywords, authors and other metadata on the webpage.

Metadata is mainly used by browsers, search engines, and other web services to rank your webpage better.

Let's see how to use metadata:

To define a character set:

The charset attribute specifies the character encoding. In this example we have set it to "UTF-8" which means it can handle to display any language.

Example:

Test it Now

Output:

HTML
Head

To define a description of your webpage:

If you give a meta description then it will be useful for the relevant search to perform by search engines.

To define keywords for search engines:

The keyword value is also used to provide keywords for a search engine, but it may ignore by browser due to spammers.

To define author of the webpage:

The author value specifies the name of the person who wrote the page content, and it is useful to automatically extract author information by some content management systems.

To refresh document every 30 seconds:

Meta refresh is used to provide instructions to the browser to automatically refresh the page after the given time interval. As in above example it will automatically refresh after 30 sec

If you add an URL with content value, then it will redirect to that page after the time limit will over.

Example:

Test it Now

Following is an example to show how to use all Meta elements within HTML head

Example:

Test it Now

Use <meta> tag to set the Viewport

This method is introduced in HTML5 to take control over the viewport by using <meta> tag.

Viewport is the user's visible area of a webpage. It changes from device to device and appears smaller on mobile phones than computer screens.

Syntax for <meta> viewport element:

Here, the <meta> viewport element specifies how to control the page's dimensions and scaling.

The width=device-width is used to set the width of the page to follow the screen-width of the device (which will vary depending on the device).

The initial-scale=1.0 is used to set the initial zoom level when the page is first loaded by the browser.

Example of a web page without the viewport <meta> tag:

Test it Now

Example of a web page with the viewport <meta> tag:

Test it Now

Note: To see the difference clearly, open this page on smartphone or tablet.

HTML <base> Element

The HTML <base> element is used to specify the base URL and base target for all relative URLs in a page.

Example:

Test it Now

HTML <script> element

HTML <script> element is used to apply client side JavaScript for the same page or to add an external JavaScript file to current page.

Example:

Test it Now

If we want to use some external JavaScript file then it can be applied by:

Excluding <html>, <head> and <body> elements

HTML 5 facilitates us to omit the <html>, the <body>, and the <head> tag.

Example:

Test it Now

Note: It is not recommended to omit the <html> and <body> tags. Omitting these tags can crash DOM or XML software and produce errors in older browsers (IE9).

However, you can omit the <head> tag.


Next TopicHTML layout




Latest Courses