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 build a web page dealing with four historical buildings in Wellington. 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 topExternal Hyperlinks take the user to another web page instead of a location in the same page. The destination page could be on the same web site or on a completely different web site. The external hyperlinks are specified in the same way as for internal hyperlinks except that the value of the href attribute is missing the leading "#".
<a href = "#introduction">Introduction</a>
In the above example the hyperlink will take the user to the block whose id is "introduction". The fact that the id name is preceded by a "#" means that the block in question is on the same page.
<a href = "introduction.com">Introduction</a>
In the above example the hyperlink will take the user to the web page whose URL is "introduction.com". The fact that the href value is missing the "#" means that the destination page is on a different web page.
For our example we shall create a small website on New Zealand volcanoes. Each volcano will have its own page and all of the pages will be linked together with external hyperlinks. We shall also use external hyperlinks to link our pages to other pages not on our own site.
Listing 3 above is the code for our first page on Mount Ngauruhoe. It is a simple page with an image of the volcano and two paragraphs of text. Its body is divided into the three classic divisions; header spanning lines 9 - 14, main spanning lines 15 - 19 and footer spanning lines 20 - 24.
We are introducing two new items here: the nav block that spans lines 10 - 13 and four hyperlinks at lines 11, 12, 21 and 22. We shall begin with the nav block.
A nav block is a container which contains hyperlinks. The <nav> tag gives meaning to navigation areas, making it clear to developers that the enclosed content is for navigation, i.e. that it contains only hyperlinks. Search engines understand <nav> as a container for important site links, which can improve the indexing of the website’s structure.
Our nav block contains two hyperlinks, one to Mount Ruapehu and the other to Mount Taranaki. The hyperlinks are contained inside the nav block and are enclosed in the <a> tags. The href attribute of the <a> tag specifies the destination of the hyperlink. In this case the destination is the file "Mt Ruapehu.html" and "Mt Taranaki.html". The text between the opening and closing <a> tags is the text that will appear on the web page. In this case the text is "Mount Ruapehu" and "Mount Taranaki".
We have two extra external hyperlinks at lines 21 and 22. Those are links to pages on other websites. In practice these could also be enclosed in a separate nav block. It is a common practice in web design to put hyperlinks to the pages on our own site into the header and to put links to pages on other sites at the end of our main block or else in the footer block.
Listing 4 above is the CSS file for our first page on Mount Ngauruhoe. It contains styling for the html, header, main, footer and img elements. The html element is styled to have a background colour of #f0f0f0 and a font-family of Verdana. The header, main and footer elements are styled to have a display of block, a margin of 0 auto, a width of 800px, and padding of 10px 20px. The header element is styled to have a background colour of #444, a text colour of yellow, and text aligned to the center. The footer element is styled to have a background colour of #444, a text colour of white, and text aligned to the center. The main element is styled to have a background colour of beige. The img element is styled to have a display of block, a margin of 20px auto.
Fig 4 above shows the file produced by the code in Listing 3 after it has been styled by the CSS code in Listing 4.
Listing 5 above is the code for our second page on Mount Ruapehu. It is similar to the first page with an image of the volcano and three paragraphs of text. The text is a brief description of the volcano and its cultural significance. The page also contains a navigation bar with hyperlinks to the other two volcanoes in our series. Due to its strong similarity with Listing 3 there is no need for further explanation.
The above image shows the output from Listing 5. Again it has been styled by the CSS code in Listing 4.
Again Listing 6 is identical in structure to Listing 3 and listing 5 and again there is no need for any further explanation.
Fig 4 above shows the output from Listing 6. Again it has been styled by the CSS code in Listing 4.
Hyperlinks are an essential part of web design. They allow the user to navigate through the web page and to other web pages. They are specified using the <a> tag and the href attribute. The href attribute can take two types of values: internal and external. Internal values are those that take the user to a location within the same page. External values are those that take the user to a location on a different page. The id attribute is used to identify the location in the same page that the hyperlink is to take the user to. The title attribute is used to give the user more information about the hyperlink. The nav tag is used to contain hyperlinks and the li tag is used to contain the hyperlinks themselves. The display attribute is used to stretch the hyperlinks horizontally across the page. The font-weight attribute is used to make the text bold. The text-decoration attribute is used to remove the underlining from the text. The padding attribute is used to give the hyperlinks proper spacing. The :hover pseudo-class is used to change the colours of the hyperlinks when the mouse hovers over them.
External hyperlinks are used to take the user to a location on a different page. They are specified in the same way as internal hyperlinks except that the value of the href attribute is missing the leading "#".
Our example was a small website on New Zealand volcanoes. Each volcano had its own page and all of the pages were linked together with external hyperlinks. External hyperlinks were also used to link our pages to other pages not on our own site.
Go to topFor this part of the assignment, you will demonstrate your understanding of hyperlinks by creating a simple HTML webpage and applying CSS to enhance its appearance and usability.
Begin by creating a new HTML file named hyperlinks.html. Design the page to include a navigation section using the nav> element. Inside the nav> block, add four hyperlinks. Two of these links should be internal, directing the user to different sections within the same page, and the remaining two should be external, linking to your favorite websites. For each hyperlink, include a title attribute that provides additional context when a user hovers over it.
Next, create a CSS file named styles.css to style your hyperlinks. Link this stylesheet to your HTML file. In your CSS, ensure that the hyperlinks in the navigation section are displayed horizontally rather than vertically. Remove the default underline styling by using the text-decoration property and make the links bold using font-weight. Add some interactivity by changing the background color of the links when a user hovers over them with their cursor. Additionally, provide proper spacing around each link by applying padding of at least 10 pixels to both sides.
By the end of this task, you will have a visually appealing and functional webpage that demonstrates your ability to implement and style hyperlinks effectively. Ensure your files are correctly named and linked so the styles are applied to the webpage as intended.
For the second part of the assignment, you will create a multi-page website that explores a topic of your choice. Each page should focus on a different aspect or subtopic related to the main theme. Use hyperlinks to navigate between the pages and provide a seamless browsing experience for users.
Go to top