The title
element sets the document's title.
Browsers usually display the contents of this element at the top of the browser window or tab.
Every HTML document should have exactly one title element.
The title text should be meaningful to the user.
The following code shows the title element in use.
<!DOCTYPE HTML>
<html>
<head>
<title>Example from java2s.com</title>
</head><!--from w w w. j a va2s .com-->
<body>
<p>This is a test.</p>
<a href="http://java2s.com">Visit java2s.com</a>
</body>
</html>
The base
element sets a base URL for relative links.
A relative link is one that omits the protocol, host, and port parts of the URL and is evaluated against some other URL-either one specified by the base element or the URL used to load the current document.
The base
element also specifies
how links are opened when a user clicks them,
and how the browser acts after a form has been submitted.
base
element has two local Attributes
An HTML document should contain, at most, one base element.
The href
attribute specifies the base URL against
which relative URLs in the document will be resolved.
The following code shows the base
element and href
attribute in use.
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
<base href="http://java2s.com/listings/"/>
</head><!--from w ww .j a va 2 s. co m-->
<body>
<p>This is a test.</p>
<a href="http://java2s.com">Visit java2s.com</a>
<a href="page2.html">Page 2</a>
</body>
</html>
The code above sets the base URL to http://java2s.com/listings/.
java2s.com
is the name of the server,
and listings
is the directory on the server.
Later in the document, it creates a hyperlink using the relative URL
page2.html
.
When the user clicks the hyperlink, the browser combines the base URL
and the relative URL to create the
combined URL http://java2s.com/listings/page2.html
.
If you do not use the base
element,
then the browser will assume that it should resolve any relative links against the URL of the current document.
For example, if you load a document from the
URL http://java2s.com/app/mypage.html
and it
contains a hyperlink with a relative URL of myotherpage.html
,
then the browser will attempt to load the second page
from the fully qualified URL http://java2s.com/app/myotherpage.html
.
The target
attribute tells the browser how to open URLs.
The values you specify for this attribute represent a browsing context.
The meta
element defines metadata for your document.
You can use this element in different ways, and an HTML document can contain multiple meta elements.
meta
element has local Attributes,
including name, content, charset, http-equiv
.
The charset
attribute is new in HTML5.
The HTML4 scheme
attribute is now obsolete.
Each instance of the meta element can be used for only one of these purposes.
The first use for the meta
element is to define
metadata in name/value pairs, for which you use the name
and content
attributes.
The following code uses the meta
Element to Define Metadata in Name/Value Pairs.
<!DOCTYPE HTML>
<html>
<head>
<meta name="author" content="java2s.com"/>
<meta name="description" content="A simple example"/>
</head><!--from w w w . j av a 2 s .c o m-->
<body>
<a href="http://java2s.com">Visit java2s.com</a>
</body>
</html>
You use the name
attribute to specify which type of metadata the element refers to,
and the content attribute to provide a value.
The following table lists the predefined metadata types
that you can use with the meta
element.
Metadata Name | Description |
---|---|
application name | The name of the web application that the current page is part of |
author | The name of the author of the current page |
description | A description of the current page |
generator | The name of the software that generated the HTML |
keywords | A set of comma-separated strings that describe the content |
In addition to the five predefined metadata names, you can also use metadata extensions. Go to http://wiki.whatwg.org/wiki/MetaExtensions to see a list of these extensions.
The robots
metadata type is very widely used.
It allows the author of an HTML document to specify how the document should be treated by search engines.
For example:
<meta name="robots" content="noindex">
The three values that most search engines will recognize are
To display an HTML page correctly, we have to set the character set (character encoding).
Another use for the meta
element is to declare
the character encoding.
An example of this is shown in the following code.
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
<meta name="author" content="java2s.com"/>
<meta name="description" content="A simple example"/>
<meta charset="utf-8"/>
</head><!--from ww w . ja v a2s . c o m-->
<body>
<p>This is a test.</p>
<a href="http://java2s.com">Visit java2s.com</a>
</body>
</html>
The charset is set to UTF-8 encoding. UTF-8 is a common character encoding.
The final use for the meta element is to override the value of one of the HTTP (Hypertext Transfer Protocol) headers.
HTTP is what you usually use to transport HTML data between the server and the browser.
Each HTTP response from the server contains a series of headers that describe the content,
and you can use the meta
element to simulate or replace three of those headers.
The following code uses the meta
Element to Simulate an HTTP Header
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="refresh" content="5"/>
</head><!--from w w w. j a v a 2 s .c om-->
<body>
<p>This is a test</p>
<a href="http://java2s.com">Visit java2s.com</a>
</body>
</html>
You use the http-equiv
attribute to specify which header you want to simulate,
and the content
attribute to provide the value you want to use.
In the code above, refresh
header is set to 5,
which has asked the browser to reload the page every five seconds.
If you follow the refresh interval with a semicolon and a URL, the browser will load the specified URL after the interval has passed.
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="refresh" content="3; http://www.java2s.com"/>
</head><!--from www .ja v a 2 s . co m-->
<body>
<p>This is a test.</p>
<a href="http://java2s.com">java2s.com</a>
</body>
</html>
There are three permitted values for the http-equiv
attribute listed in the following.
<meta http-equiv="refresh" content="5; http://www.java2s.com"/>
<meta http-equiv="content-type" content="text/html charset=UTF-8"/>