Hyperlinks provide the basis for HTML by which users can navigate through content, both within the same document and across pages.
You create hyperlinks using the a
element.
The a
element has local
attributes:href, hreflang, media, rel, target, type
.
The id, coords, shape, urn, charset, methods
, and rev
attributes are obsolete.
You can create hyperlinks to other HTML documents by setting the href
attribute in a
element to a URL that starts with http://
.
When the user clicks the hyperlink, the browser will load the specified page.
The following code shows the a
element being used to link to external content.
<!DOCTYPE HTML>
<html>
<body>
I like <a href="http://java2s.com">Tutorial</a>.
</body>
</html>
Not all URLs have to refer to other web pages.
Browsers also support other protocols such as https
and ftp
.
If you want to reference an e-mail address, you can use the
mailto
protocol; for example, mailto:info@example.com
.
If the value of the href
attribute doesn't start with
a recognized protocol, such as http://
,
then the browser treats the hyperlink as a relative reference.
The following code gives an example of a relative URL.
<!DOCTYPE HTML>
<html>
<body>
I like <a href="index.html">tutorial</a>.
</body>
</html>
The code set the href
attribute to index.html
.
When the user clicks the link, the browser uses the URL of the current document to determine how to load the linked page.
You can create hyperlinks that bring another element into view in the browser window.
You create internal hyperlinks using the CSS-style ID selector, #id, as shown in the following code.
<!DOCTYPE HTML>
<html>
<body>
You can see other tutorials I like<!--from w w w . j a v a 2 s . c o m-->
<a href="#tutorial">here</a>.
<br />
<br />
<br />
<br />
<br />
<br />
<p id="tutorial">This is a test.</p>
</body>
</html>
The code above created a hyperlink with the href
value of #tutorial
.
When the user clicks the link, the browser will look for an element in the document
whose id
attribute has a value of tutorial
.
If the element isn't already visible on
the screen, the browser will scroll the document to show it.
If the browser can't find an element with the desired id
attribute value,
it will search again, looking for a
name
attribute that matches the target.
The target
attribute from a
element
lets you tell the browser where you want
the linked resource to be displayed.
By default, the browser uses the window, tab, or frame in which the current document is displayed to show the linked document and replace the existing one.
The following list describes the supported values for the target attribute.
Each of these values represents a browsing context.
Links can be displayed in different ways:
a:link {color:#FF0000;} /* unvisited link */ a:visited {color:#00FF00;} /* visited link */ a:hover {color:#FF00FF;} /* mouse over link */ a:active {color:#0000FF;} /* selected link */
The meaning of each pseudo-class is listed in the css comments.
The following code sets the four anchor pseudo-classes.
<!DOCTYPE HTML>
<html>
<head>
<style>
a:link, a:visited {<!-- w w w.ja v a 2 s .co m-->
text-decoration: underline;
color: #6A5ACD;
background-color: transparent;
}
a:hover, a:active {
text-decoration: underline overline;
color: #191970;
background-color: #C9C3ED;
}
</style>
</head>
<body>
<ul>
<li><a href="#">A</a></li>
<li><a href="#">B</a></li>
<li><a href="#">C</a></li>
<li><a href="#">D</a></li>
</ul>
</body>
</html>
Pseudo-classes can be combined with CSS classes. The following example tells the browser to display a visited anchor link in red.
a.red:visited {color:#FF0000;} <a class="red" href="http://java2s.com">java2s.com</a>