Learning Outcomes
On completion of this lesson you will know:
- The nature of hyperlinks
- The difference between internal and external hyperlinks
- How to open pages in a new tab
- Improve user experience
On completion of this lesson you will know:
Hyperlinks are the lifeblood of a web site. They allow the user to move easily from one part of the site to another. They are easily distinguished as they are normally blue underlined pieces of text. They can, however, appear also as buttons or menus, including drop down menus. In fact most things that can appear on a web page can be used as hyperlinks.
There are two types of hyperlinks:
These are used in order to easily navigate to individual parts of a large document. They act somewhat like the contents page of a book. In order to use internal hyperlinks we also need to use another feature called bookmarks. Bookmarks are simply features that define an area of the web page which the browser can move to if requested. Thus our example will contain both hyperlinks and bookmarks. Bookmarks are also referred to as anchors.
For our example here we shall expand the page we created to demonstrate sidebars by adding two extra paragraphs to it detailing two other historical buildings. This will give us a more realistic example of the use of internal hyperlinks for quickly locating individual parts of a long web page.
Before we begin explaining our hyperlinks let us revise a little about the structure of our newly expanded web page. It contains details of three buildings in the Wellington area. The detials of each building is enclosed inside one article container.
If we examine line 20, its code is <article id="PlimmerHouse">
This means that the entire article is now identified by the identity of "PlimmerHouse". Also notice that "PlimmerHouse" has no spaces. This is because HTML does not allow spaces in the value of the attribute id.
Also notice that the article elements at lines 47, 58 and 67 are given id values of "MtVictoria", "GovernmentHouse" and "NairnCottage". Thus our four article elements now have unique id values so that each can be identifie from other parts of the page.
Why do we need to identify them? Recall that we are going to use hyperlinks to jump to each of those article elements. In this case the hyperlink needs to know the id of the article element it is to jump to.
Now let us look at those hyperlinks.
The hyperlinks are specified inside a nav element that spans lines 11 to 18. This is the first time we have come across the nav and so we need to say something about it. Firstly it's name is an abbreviation for "navigation". It is a container just like article, aside etc. but is used for controlling hyprlinks. Our four hyperlinks are defined inside it at lines 13 to 16. As you can see the hyperlinks are contained inside the <li>..</li> pairs.
Let us examine the first one at line 13. Inside the <li>..</li> pair we have the the <a> which is the opening tag for the hyperlink. We are giving values to two of its atributes: href and title. The href's value is "#PlimmerHouse" which is the id of our first article element. What this means is that if the user clicks on the hyperlink focus will jump to the block whose id "PlimmerHouse. Finally there is a "#" before the id's name. This is to tell the browser that the destination of the hyperlinks is inside inside the current document and not an external filename. Without the "#" the browser assumes that we are referring to an external file.
The other attribute title is an enhncement for the user interface. If you hover over the hyperlink on the web page with the mouse, the text assignned to the attribute is displayed in a popup window on the page. This is used to give the user a more detailed information about the hyperlink.
Once we close the opening tag for the hyperlink we enter the main text for the hyperlink - "Plimmer House" in this case and then we enter the closing tag.
The explanation we have given about line 13 also applies to lines 14, 15 and 16 and thus there is no need to describe them.
Fig 1 above shows what our hyperlinks would look like without any formatting. They will work, i.e. clicking on any one of them will move the focus to the block whose id value corresponds to that specified in the hyperlink. However they look like an unordered list, which is not what we want.
We would like our hyperlinks to stretch in a hourizontal line across the top of the page. By default the display attribute of li is block which is why they appear stacked vertically in Fig 1. To get them to stretch horizontally we need to change the display attribute to inline which is done at lines 73 and 74 of listing 2.
With this amount of styling the hyperlinks look as in Fig 2 below.
Although they are now stretched horizontally they are stilled underlined and there is no proper spacing between the different hyperlinks. We need extra formatting that will do the following:
All of this is accomplished at lines 77 to 81 of Listing 2.
A final embellishment is that if the user hovers over a hyperlink with the mouse, the colours of that hyperlink will reverse, i.e. the background becomes black and the text colour will be white. This is accomplished by the styling code at lines 83 to 85.
The code at line 83 can be translated into English as "If the mouse hovers over the <;a>; element which is inside the <;li>; element, which is inside the <;ul>; element, which is itself inside the <nav> element, then change the background colour of the text to black and the colour of the text itself to white.
In Fig 3 we see the result of the formatting specified in the CSS file from Listing 2, including reversing the colours when a hyperlink is hovered over. Also notice that the value of the title for Nairn Cottage which is specified at line 16 of listing 1 also appears.
Go to topIn our code in Listing 1 we have four examples of external hyperlinks. These are at lines 26, 53, 64 and 73. Line 26 is reproduced below.
<a href="https://www.heritage.org.nz/the-list/details/225" title="This link takes you to the official Heritage New Zealand page on Plimmer House" target="_blank">Plimmer House</a>
In this case the hyperlink tag <a> has three attributes with values. We have encountered the first attribute href already but this time it is referencing a target outside of the current web page. We know this from the fact that its value begins with "https://www." and that it does not begin with a "#". The title attribute should also be familiar to use, i.e. it describes the page that the hyperlink links to. The attribute that we have not met before is target. This simply specifies how the browser will load the target file. If the attribute is omitted then the page is loaded into the current tab, whereas if the target attribute is given a value of "_blank" then the page is loaded into a new tab.
Go to top