Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   
   HTML   
   CSS   
   JAVA SCRIPT   
   JQUERY   




HTML - Header


There are a few elements in HTML those are used to display the metadata or data about data. And the header element or <head> element is used as a container for those elements.


Below are the list of tags used inside the header element:

  1. <title>



    <title> tag is used to display the title of the webpage.

  2. <link>



    <link> tag is used to link present webpage with an external resource.

  3. <style>



    <style> tag is used to provide CSS information for an HTML document.

  4. <meta>



    <meta> tag is used to specify the metadata about an HTML document.

  5. <script>



    <script> tag is used to define java script for an HTML document.

  6. <base>



    <base> tag is used to specify the base URL for all relative URLs in a document.

Let us see all the HTML tags used inside the head element in detail:


<title> tag in header element


The <title> tag is used to display the title of the webpage.


Example :



<html>
	<head> 
		<title>
			Sample Title
		</title>
	</head>
	<body>
		<div id = "myID">
			<img src="image1.jpg" style = "width:100%">
        </div>
	</body>        
</html>


Output :

HTML-Header

So, if you look at the above code the <head> element acts as a container for the <title> tag.


<head>
	<title>
		Sample Title
	</title>
</head>

Inside the <title> tag we have the title Sample Title.


Now, in the above output the title of the HTML document is displayed(i.e. Sample Title) on the top right of the webpage.


<link> tag in header element


The <link> tag is used to link present webpage with an external resource. The <link> tag is quite often used to link external stylesheets.


Example :



<html>
	<head> 
		<link rel="stylesheet" type="text/css" href="/css/mystyle.css">
	</head>
	<body>
		<div id = "myID">
			<img src="image1.jpg" style = "width:100%">
        </div>
	</body>        
</html> 



So, if you look at the above code the <head> element acts as a container for the <link> tag.


<head>
	<link rel="stylesheet" href="/css/mystyle.css">
</head>

Inside the <link> tag we have the rel and href attributes that links an external css file from the folder named css.


<style> tag in header element


The <style> tag is used to provide CSS information for an HTML document.


Example :



<html>
	<head> 
		<style>
			.myFirstClass {
                border-style: dotted;
                border-width: 5px;
			}
		</style>
	</head>
	<body>
		<div class = "myFirstClass">
			<img src="image1.jpg" style = "width:100%">
        </div>
	</body>
</html>


Output :




So, if you look at the above code the <head> element acts as a container for the <style> tag.


<head>
	<style>
		.myFirstClass {
			border-style: dotted;
			border-width: 5px;
		}
	</style>
</head>

In the <style> tag we have specified the CSS properties for the class myFirstClass.


.myFirstClass {
	border-style: dotted;
	border-width: 5px;
}

<meta> tag in header element


The <meta> tag is used to specify metadata like character set, keywords, page description, viewport settings of an HTML document.


Let us see them in the below examples.


Example :



<html>
	<head> 
		<meta charset="UTF-8">
  		<meta name="description" content="Test data for meta tags">
  		<meta name="keywords" content="Java, Spring, Hibernate">
  		<meta name="viewport" content="width=device-width, initial-scale=1">
	</head>
	<body>
		<button onclick="test()">
			Click Me
		</button>	
	</body>
</html>



So, if you look at the above code we have a set of <meta> tags defining the following metadata.

  1. The charset attribute defines the character set.



    <meta charset="UTF-8">

  2. The attribute 'name="description"' tells the search engines describing what the page is all about.



    <meta name="description" content="Test data for meta tags">

  3. The attribute 'name="keywords"' tells the search engines what are the important keywords in the webpage.



    <meta name="keywords" content="Java, Spring, Hibernate">

  4. The below attribute will create a responsive webpage. i.e. Your webpage will resize automatically in desktop and mobiles.



    <meta name="viewport" content="width=device-width, initial-scale=1">

<script> tag in header element


The <script> tag is used to define java script for an HTML document.


Example :



<html>
	<head> 
		<script>
			function test(){
         		alert("Testing script tag");
      		}
		</script>
	</head>
	<body>
		<button onclick="test()">
			Click Me
		</button>	
	</body>
</html>


Output :




So, if you look at the above code the <head> element acts as a container for the <script> tag.


<head>
	<script>
		function test(){
			alert("Testing script tag");
		}
	</script>
</head>

Inside the <script> tag we have defined a function of java script called test() that displays the text Testing script tag.


<script>
	function test(){
		alert("Testing script tag");
	}
</script>

Later in the code, we have defined a button Click Me.


<button onclick="test()">
	Click Me
</button>

On clicking on it the test() function is called.