Sub topic 1
Elements and Attributes: A Quick Refresher
Prerequisites
This chapter assumes you know the basic html, head, and body structure, can open an HTML file in a browser, and understand file and folder paths.
Elements and attributes
An HTML element is a marked-up part of a document, such as a paragraph or link. An attribute is a name-value setting inside an element's opening tag. For example, href tells a link where to go.
<!doctype html>
<html lang="en">
<body>
<a href="guides/setup.html">Read the setup guide</a>
</body>
</html>The a element creates a link. Its content is the link text, and its href attribute is the destination.
Creating links with the `a` element
A URL is an address for a resource. An absolute URL includes the full address and can point to another website:
<a href="https://example.com/">Visit Example</a>A root-relative URL starts with / and begins at the website's root:
<a href="/docs/intro.html">Read the introduction</a>A relative URL starts from the current document's folder:
<a href="guides/setup.html">Set up the project</a>
<a href="../index.html">Return to the parent folder</a>Linking to a section of a page
A fragment link starts with #. It targets an element whose id has the same value:
<nav><a href="#install">Jump to installation</a></nav>
<h2 id="install">Installation</h2>Opening links in a new tab: `target` and `rel`
target="_blank" requests a new browsing context. Add rel="noopener noreferrer" so the new page cannot access window.opener and does not receive the referring URL.
<a href="https://example.com/" target="_blank" rel="noopener noreferrer">
Visit Example in a new tab
</a>Writing link text that makes sense out of context
Link text should describe the destination. Avoid vague text such as “click here,” because screen-reader users may hear a list of links without surrounding sentences.
- Good:
Download the installation guide - Weak:
Click here
Exercise
Which attribute makes a link open in a new browsing context, and which value should accompany it for safer external links?
Show answer
Use target="_blank" with rel="noopener noreferrer".