This lesson demonstrates how to create internal and external links. There’s a brief review of the anchor element and an overview of the difference between absolute and relative paths.
<a>
tag to link to another webpage.You’ve now created a single HTML page and you may be wondering how to connect multiple pages together. This is the next step in the learning process: linking multiple pages. This need arises any time that you’re working on a site that has more than one page, which is very likely to happen. In fact you’ll be creating a three page site within this course! Let’s review the linking process and practice this crucial skill.
Recall that hyperlinks are what connect web pages together. The anchor, or <a>
tag, is used to define the links between pages. Anchors can connect to a particular part of a page, between different pages in a site, or connect to outside resources.
You’ve encountered these links since they’re a crucial backbone of the internet. The anchor element is used to connect pages together and is a crucial HTML element.
Browsers typically render links as having blue text and an underline, but this isn’t always the case because this appearance can be changed with CSS. Let’s take a look at a hyperlink:
<a href="https://digitalharbor.org">
Link to the Digital Harbor Foundation Website
</a>
Code language: HTML, XML (xml)
Let’s now look at the structure of the HTML:
<a>
and </a>
“Link to the Digital Harbor Foundation Website” is displayed as the link text. When constructing a link, this text is placed between the opening and closing anchor tags. The user would click on this text and be taken to the page linked in the href value. In this case, the link would go to the Digital Harbor Foundation Website.
It’s necessary to add the path value to the href in order for a link to work. Links can go to external websites, pages on the same server, and even links within the same page. There are two types of paths: relative paths and absolute path.
Understanding the difference between the two is important as there are different use cases — one is not better than the other. You’ll use both throughout your web development explorations.
Let’s take a look at the differences between the path types and some examples of when you’d use each.
Absolute paths are full paths. These are always used when linking to an external website.
Let’s take a look at an example:
<h2>Absolute Path Example</h2>
<p>
This example links to an external webpage using an absolute path:
<a href="https://digitalharbor.org/our-team">
Link to the Digital Harbor Foundation Website's Team page.
</a>
Click on that link to go to the Digital Harbor Foundation's Team page!
</p>
Code language: HTML, XML (xml)
You’ll need to use absolute paths when linking to external sites. The above example wouldn’t work if you were to only use “/our-team” as the href’s value.
Relative paths don’t use full file paths. Instead, they use the current page as a reference and link relative to the current page’s file path. This may not seem immediately clear, but this is often the method for linking to internal pages in your site.
For example, if you have an index.html and an about.html in the same folder, you can use a relative link to omit the full file path. Here’s an example:
<h2>Relative Path Example</h2>
<p>
Check out my About Page:
<a href="/about.html">
About Me
</a>
Click on that link to learn more about me.
</p>
Code language: HTML, XML (xml)
In this example, the href
value “/about.html” references an about.html page that exists within the same folder. The use of the ”/” indicates that the file is in the same exact folder as the current page. This won’t work if the file isn’t contained in the same folder.
Before starting the activity, let’s take a quick look at a side-by-side example of the code and the resulting page. This example extends the example used in the Adding Images lesson.
Note: As with all image examples, click on the image to open a higher resolution version in another browser tab.
If you’re stuck, open up the example image in a new tab and look at the code. Remember, it’s completely fine if you forget syntax and need to look it up. Focus on building comfort and familiarity with the concepts. It’s alright if you need to look up specific syntax.
If you want to extend this activity, create some new pages and start linking back and forth between them! Make sure to add a way to navigate back to the home page (index.html) from any new pages you create.