HTML Links: Creating Hyperlinks in Web Pages
Table of Content:
What you will learn in this tutorial
This tutorial explains how to create a link from one page to another. It also outlines the different types of hyperlinks.
- What is link in HTML
- Syntax of link in HTML
- <a> element to define a link
- href attribute to define the link address
- target attribute to define where to open the linked document
- <img> element (inside <a>) to use an image as a link
- id attribute (id="value") to define bookmarks in a page
- href attribute (href="#value") to link to the bookmark
What is Link
Links (or hyperlinks) that you can click on to be taken from one page to another page. The link can be a word, phrase, or image. Links, known as hyperlinks, are defined using the <a> tag otherwise known as the anchor element.
To create a hyperlink, you use the <a> tag in conjunction with the href attribute. The value of the href attribute is the URL, or, location of where the link is pointing to.
Syntax of Link
Here are some HTML codes to play with.
Hypertext references can use absolute URLS, relative URLs, or root relative URLs.
- absolute
- relative
- root relative
Absolute
This refers to a URL where the full path is provided. For example:
HTML Tutorial
Relative
This refers to a URL where the path, relative to the current location, is provided.
For example, if we want to reference the https://www.atnyla.com/library/html/ URL, and our current location is https://www.atnyla.com/library/, we would use this:
HTML Tutorial
Root Relative
This refers to a URL where the path, relative to the domain's root, is provided.
For example, if we want to reference the https://www.atnyla.com/library/html/ URL, and the current location is https://www.atnyla.com/library/, we could use this:
HTML Tutorial
The forward slash indicates the domain's root. No matter where your file is located, you can always use this method to specify the path, even if you don't know what the domain name will eventually be (as long as you know the full path from the root).
The target Attribute
The target attribute specifies where to open the linked document. The target attribute can have one of the following values:
_blank- Opens the linked document in a new window or tab_self- Opens the linked document in the same window/tab as it was clicked (this is default)_parent- Opens the linked document in the parent frame_top- Opens the linked document in the full body of the windowframename- Opens the linked document in a named frame
Example _blank
Visit atnyla.com!
Example _top
If your webpage is locked in a frame, you can use target="_top" to break out of the frame:
HTML5 tutorial!
Create a Bookmark
HTML bookmarks are used to allow readers to jump to specific parts of a Web page. Bookmarks can be useful if your webpage is very long. To make a bookmark, you must first create the bookmark, and then add a link to it. When the link is clicked, the page will scroll to the location with the bookmark.
First, create a bookmark with the id attribute:
Chapter 4
Then, add a link to the bookmark ("Jump to Chapter 4"), from within the same page:
Jump to Chapter 4
Or, add a link to the bookmark ("Jump to Chapter 4"), from another page: Example
Jump to Chapter 4
Task for you
Now it's your turn to create a page look like this
- Question 1: If you see a web address on a magazine, to which web page does it point?
- Question 2: Does a hyperlink apply to text only?
- Question 3: How do you create a link that will connect to another web page when clicked?
- Question 4: Can a single text link point to two different web pages?
- Question 5: How are active links different from normal links?