5

Links

The element that has made the web at least somewhat interactive since the very beginning is the hyperlink.

Hyperlinks, or simply links, allow us to connect pages to each other to form a website, and let us send visitors to pages that are external to our website. They play an important role in the organization and SEO (search engine optimization) of websites. But how exactly do they work? What can we link to?

Link Markup

A link (or hyperlink) is a way to connect one webpage to another. Links also allow users to jump to a different section of a webpage, download documents, and more. To distinguish them visually from other webpage content, textual links are usually given a distinctive appearance, using color (typically blue for a link that has not yet been visited), special text formatting, or both.

Links are represented in HTML by the <a> (or anchor) tag. The <a> tag is one of those HTML tags that can include a number of attributes.

In this case, the link takes users to a page on a different website, so we need to include the attribute href, for hypertext reference. The value of the href attribute is the URL (Uniform Resource Locator) of the destination—the page you’re linking to. The enclosed content of the <a> element is typically some label text—usually the name of the page the link will take the user to. This label is the only part of the link that the user sees.

Let’s see how to set up the markup for a link to Google.

To create a hyperlink:

  1. In your HTML file, type <a to start the anchor element.

  2. Type href="https://google.com"> to define the destination of the link.

  3. Type Visit Google to provide a label for the user to click.

  4. Type </a> to finish the link (FIGURE 5.1).

    The markup to create a hyperlink to google.com is shown, which reads <a href="https://google.com">Visit Google</a>

    Figure 5.1 The markup for the link to google.com

  5. Save the file and view it in your browser (FIGURE 5.2).

    The output of the markup for the Google link displayed in a browser shows "Visit Google." The hyperlink is in color and underline.

    Figure 5.2 This hyperlink to google.com stands out by its color and underline.

While the markup for a link may look straightforward, there are several nuances to creating links. Key to creating links is understanding how URLs are structured.

URL Structure

At this point, it’s worth taking a closer look at URLs. In FIGURE 5.3, you’ll see the components of a common URL.

A figure depicts the components of a URL.

Figure 5.3 The components of the URL https://www.wordpress.org

URLs are made up of these sections:

  • The protocol (either http or https).

  • The subdomain (optional).

  • The name of the website.

  • The top-level domain (or TLD). This is also known as the extension.

To link to a specific file within a site, you need to provide these items as well:

  • The path (folder hierarchy) containing the file.

  • The file name.

In order to reach a website at all, you need to provide at least these three parts of the URL: the protocol, the name, and the TLD. Paths, subdomains, and file names need to be included only when you want to reach those specific areas of the website.

Ultimately, where a link sends your users is based on what kind of URL—or how much of the URL—you include.

First, let’s look at how linking to pages within your website compares with linking to pages outside your website.

Internal vs. External Linking

This concept is pretty clear. Internal links are links to pages within your own website (or on the same domain). External links are links to someone else’s webpage.

As far as markup goes, internal and external links use exactly the same structure, except (as you’ll learn later in this chapter) external links are always absolute. That is, they need to include the entire URL, including the protocol. If one of those items is missing or incomplete, the link will not work.

If you’re linking internally, there’s something else to consider: you don’t always need to include the entire URL, because it’s implied. This is the result of the difference between absolute and relative links.

Relative vs. Absolute Linking

Back in Chapter 2, you learned about file structures and how websites are organized (FIGURE 5.4). You also saw how URLs incorporate part of a file’s directory structure. When we talk about relative versus absolute linking, we’re talking about how much of the URL and directory structure we want to include.

A figure represents the structure of the directory created in Chapter 2.

Figure 5.4 The directory structure we created in Chapter 2

With an absolute link, you include the entire URL in the anchor element markup. Always use an absolute link when linking to an external site, but you'll usually use relative links for files on your own site.

Say you want to link to a specific file called cart.html on the website mysite.com. You’ll need to know the location of the file in the site’s directory structure to link to it. If cart.html is in a top-level folder (i.e., a folder in the root directory) called /store/, then the absolute link for this file is https://mysite.com/store/cart.html.

Relative links, on the other hand, don’t include the complete URL of the location you’re linking to. The structure of the link markup is based only on where the linked file is relative to the current file. So relative links are commonly used to link within pages on a single website, whose files share one common directory.

There are several different types of relative links we can create, as shown in TABLE 5.1.

  • Same folder: The linked file is in the same folder as the linking file. The relative link would just be the file name (e.g., file.html).

  • Child folder: The linked file is one folder down from the linking file (in other words, in a folder contained in the linking file’s folder). The relative link starts with a forward slash (/), then includes the folder name, another forward slash, and the file name (/folder-name/file.html).

  • Parent folder: The linked file is one folder up from the linking file. The relative link would be two periods and a forward slash (../) and then the file name (../file.html).

    These patterns can be repeated for any number of folders. So if a file is three folders up from the current file, the relative link would be ../../../file.html. And there can be great-, great-great-, great-great-great-grandchildren, and so on.

Table 5.1 Types of relative links

Relative link type

Relative path

Example from our sample directory

Same Folder: Both files are in same folder

file.html

From the home page: about.html

Child: Linked file is in next folder down

/folder-name/file.html

From home: /store/cart.html

Grandchild: Linked file is two folders down

/child-folder/folder-name/file.html

From home: /store/orders/001.html

Parent: Linked file is next folder up

../file.html

From /store/ to home: ../index.html

Grandparent: Linked file is two folders up

../../file.html

From /store/orders/ to home: ../../index.html

Looking at our example website again, to make a link from the home page (mysite.com/index.html in the root directory) to cart.html, you could use a relative link. Since cart.html is in the child folder /store/, we could link to it using a relative path: /store/cart.html.

To create a relative link:

  1. In your website folder on your computer, create a new folder called images.

  2. Go to unsplash.com and download any image you’d like. Save it into the images folder you just created and name the image file unsplash.jpg.

  3. In the website folder, make a duplicate of the file boilerplate.html and rename the new file 5.html.

  4. Open the file 5.html.

  5. Right after the opening <body> tag, type <a href=".

  6. Type /images/unsplash.jpg">.

  7. Type Check out this image!.

  8. Type </a>.

  9. Save the file and open it in your browser.

  10. Click the link to display the image in the webpage.

Other Types of Links

Aside from internal and external links, here are two other types of links you can add to a webpage. You can link to a specific section of a page, and you can link to other applications, like email.

Linking to a specific section of the page

This is a great way to highlight specific content or drive a user to a pertinent area. There are two parts to this link:

  • Assign a unique name to the section you want to link to by including the id attribute. For example, say you want to add a link to your page that takes people to a contact form. If the contact form is under the heading “Contact Me!” you would assign an id attribute ("contact", perhaps) to the heading:

    <h3 id="contact">Contact Me!</h3>
  • Link to that id in the anchor tag:

    <a href="#contact">Jump to Contact Form</a>

The id attribute is often assigned to a heading tag (as in the “Contact Me!” example) because heading tags denote the beginnings of sections. But id is a standard attribute that can be applied to any HTML element.

In our link, we identify the section using the hash tag, or number sign (#), followed by the value of the id attribute (e.g., #contact). The hash tag tells the browser “this is a specific location on the page.”

To create a link to a specific location on a page:

  1. In your HTML file, type or copy and paste several text paragraphs (be sure to enclose them in <p> tags) between the opening and closing <body> tags. If you wish, make the last paragraph a brief biographical sketch of yourself.

  2. Place the insertion point in front of the last <p> tag to create a heading before the last paragraph. Type <h3>About Me</h3> to define the heading.

    Now, add an id attribute to this heading so you can link directly to it.

  3. Place the insertion point in the opening tag of the heading element and type id="aboutme".

    The entire line of code should look like this:

    <h3 id="aboutme"

  4. Go back to the top of the document, and right after the opening <body> tag, type <a href=".

  5. Type #aboutme"> to tell the link to jump to the anchor you just created.

  6. Type Skip to "About Me" to create the label for the link that will be displayed to the user.

  7. Type </a> to close the anchor element.

  8. Save the file and open it in a browser to test the link.

In many real-world implementations, you’ll see the browser smoothly scroll to the linked section. In order to achieve that effect, you’ll need to add some CSS to your website.

Linking to more than just webpages

Finally, links can do more than just take you from one webpage to another. There is a growing set of Uniform Resource Identifiers (URIs) that tell the browser which specific applications a link should open in. While email links are the most common, you can also specify telephone numbers (tel:), file servers (ftp:), and more.

Email links are links that will open an email app on your device, with the email address (and potentially other information) filled in. To create one, you structure the link like this:

<a href="mailto:[email protected]">
Email Joe</a>

Notice that there’s no URL here, just the prefix mailto: and the email address.

Link Targets

Before we wrap up the chapter, there’s one more attribute you should know about, and that’s target.

You’ve likely browsed websites on which the links opened in new tabs or windows. A common reason is that the webpage author hopes the user will close the new tab when they’re done and return to their site.

That’s achieved by using the target attribute and the value blank. For example, to open casabona.org in a new tab, use this link:

<a href="https://casabona.org"
target="_blank">Link that opens in a
new tab</a>.

To create a link that opens in a new window:

  1. In your HTML file, type <a href= "https://google.com".

  2. Type target="_blank">.

  3. Type Visit Google</a>.

  4. Save the file.

  5. Open the file in your browser and click the link to see it open the home page of google.com in a new window.

While it’s considered best practice to open links in the same window, there might be legitimate reasons to send the user to a new window. If you do decide to open links in new windows, you should at least let the user know what to expect. You can do so by adding simple text (like “new window”) to the link label or by adding an icon, as shown in FIGURE 5.5.

A screenshot shows the contents within a border. The content consists of a link with the new window indicator icon displayed at the end of the link.

Figure 5.5 An example of a new window indicator icon from CodePen (codepen.io/svinkle/pen/BreKRJ)

Wrapping Up

With your knowledge of formatting, organization, and now linking, you’re ready to move into the structure and layout side of HTML.

In the next chapter, you’ll learn about the building blocks that are used to lay out a webpage.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.40.207