Sub topic 1
Anatomy of a hyperlink: href, anchor text, and rel
Prerequisites
This chapter assumes you can read simple HTML and know what a web page and a URL are. It also assumes you know that search engines crawl and index pages. You don't need to know how ranking works yet. Later chapters cover that. Here you learn the parts of a link, because every backlink signal a search engine records starts with these parts.
What a hyperlink is made of
A hyperlink is an HTML anchor element, written <a>. It has three parts that matter for search:
- The
hrefattribute holds the destination URL. - The anchor text is the content between the opening and closing tags. It is what a visitor clicks.
- The
relattribute describes the relationship between the linking page and the target.
<a href="https://example.com/guide" rel="nofollow">Example guide</a>In this example, href is https://example.com/guide. The anchor text is Example guide. The rel value is nofollow.
A page that contains this link has an outgoing link. From the point of view of example.com/guide, the same link is a backlink. The next chapter defines that difference in detail. Here we only look at what the link itself contains.
The href attribute
The href value decides which page the link points to. A crawler cannot follow a link without one, so an <a> element with no href is only a placeholder. Search engines do not treat it as a link.
Absolute and relative URLs
An absolute URL contains the scheme and host, such as https://example.com/guide. A relative URL leaves some parts out and is resolved against the URL of the page that contains it, or against a <base> element if the page has one.
| href value | Page it appears on | Resolved URL |
|---|---|---|
| `https://example.org/ad` | `https://example.com/blog/post.html` | `https://example.org/ad` |
| `/guide/seo` | `https://example.com/blog/post.html` | `https://example.com/guide/seo` |
| `../logo.html` | `https://example.com/blog/post.html` | `https://example.com/logo.html` |
| `#top` | `https://example.com/blog/post.html` | `https://example.com/blog/post.html#top` |
| `?page=2` | `https://example.com/blog/post.html` | `https://example.com/blog/post.html?page=2` |
Relative URLs are the usual way a site links to itself. Backlinks from other sites are almost always absolute, because the other site has to name your host.
Fragments and query strings
A fragment is the part after #. It jumps to a spot inside the page and is not sent to the server. Search engines generally treat page.html#section as the same document as page.html. A link with a fragment still points at the page, but the fragment gives no extra signal about a separate page.
A query string is the part after ?. Parameters such as ?utm_source=newsletter create many URLs that serve the same content. Links to those variants can split signals across several addresses unless the page declares a canonical URL. Canonicalization is outside this chapter, but you should know that the exact string in href is what gets recorded.
Hrefs that are not links in the search sense
Some patterns look like links to a visitor but give a crawler nothing to follow:
href="javascript:void(0)"has no destination URL.href="#"only points to the top of the same page.- A
<button>or a<div onclick="...">that navigates with JavaScript has nohrefat all. mailto:andtel:hrefs open other applications and are not web pages.
If you want a crawler to treat something as a link, use an <a> element with a real URL in href.
Anchor text
Anchor text is the visible, clickable text of the link. Search engines use it as a short description of the target page. A link that reads beginner's guide to composting says more about its target than a link that reads click here.
Three details are easy to miss:
- Image links. When an anchor contains only an image, the
alttext of the image acts as the anchor text. An image with noaltgives the link no descriptive text. - Nested markup. Anchor text includes the text of elements inside the anchor, such as
<strong>or<span>. Tags are ignored and only the text counts. - Empty anchors. An anchor with no text and no image has nothing to describe the target. It still points to a URL, but it carries no descriptive signal.
<!-- Text anchor -->
<a href="/guide/composting">beginner's guide to composting</a>
<!-- Image anchor: the alt text describes the target -->
<a href="/guide/composting"><img src="compost.jpg" alt="Compost bin guide"></a>
<!-- Weak anchor: tells nobody what the target is -->
<a href="/guide/composting">click here</a>How search engines weigh anchor text, and how it can be abused, is covered in a later chapter. For now, remember that anchor text is part of the link's data.
The rel attribute
The rel attribute tells the search engine how the linking page relates to the target. For ranking, three values matter most:
| Value | Meaning | Typical use |
|---|---|---|
| `nofollow` | The linking site does not vouch for the target. | Links you do not want to endorse |
| `sponsored` | The link was paid for or is part of an advertisement. | Ads, paid placements, affiliate links |
| `ugc` | The link sits in user-generated content. | Comments, forum posts |
A link with no rel value is an ordinary link. Search engines treat it as a normal editorial reference.
The rel attribute accepts several values separated by spaces, and the order does not matter:
<!-- Paid placement -->
<a href="https://example.com/product" rel="sponsored">Partner product</a>
<!-- Link in a user comment or forum post -->
<a href="https://example.com/post" rel="ugc">A commenter's link</a>
<!-- Several values can be combined -->
<a href="https://example.com/offer" rel="sponsored nofollow">Offer</a>Other rel values exist but have nothing to do with ranking. noopener and noreferrer control browser behavior when a link opens in a new tab with target="_blank". Modern browsers already apply noopener to target="_blank" links by default. You may see rel="noopener noreferrer nofollow" in the wild. Each value does its own job, so the combination does not change what the others mean.
Other attributes on the anchor
targetchooses where the link opens, such as_blankfor a new tab. It does not change how search engines read the link.titleis an extra tooltip. It is not a replacement for anchor text.hreflangandtypedescribe the target's language and media type. They are hints for browsers and tools.
Reading links the way a crawler does
A crawler, in simple terms, parses the HTML and records three facts for each <a href>: the resolved destination URL, the rel values, and the anchor text. The script below does the same with Python's standard library. Chapter 3 returns to how real crawlers find and store links. This version resolves relative URLs, lowercases rel values, and uses image alt text as anchor text.
from html.parser import HTMLParser
from urllib.parse import urljoin
class LinkCollector(HTMLParser):
"""Collect resolved URL, rel values, and anchor text for every <a href>."""
def __init__(self, base_url):
super().__init__()
self.base_url = base_url
self.links = []
self._current = None
def handle_starttag(self, tag, attrs):
attributes = dict(attrs)
if tag == "a" and "href" in attributes:
self._current = {
"url": urljoin(self.base_url, attributes["href"]),
"rel": (attributes.get("rel") or "").lower().split(),
"text": "",
}
elif tag == "img" and self._current is not None:
self._current["text"] += attributes.get("alt") or ""
def handle_data(self, data):
if self._current is not None:
self._current["text"] += data
def handle_endtag(self, tag):
if tag == "a" and self._current is not None:
self._current["text"] = " ".join(self._current["text"].split())
self.links.append(self._current)
self._current = None
html = """
<p>
<a href="/guide/seo">SEO guide</a>
<a href="https://example.org/ad" rel="Sponsored NoFollow">this offer</a>
<a href="../logo.html"><img src="logo.png" alt="Example logo"></a>
<a href="#top">Back to top</a>
<a>No href</a>
</p>
"""
parser = LinkCollector("https://example.com/blog/post.html")
parser.feed(html)
for link in parser.links:
print(link)Running it prints four records. The fifth anchor has no href, so it is skipped:
{'url': 'https://example.com/guide/seo', 'rel': [], 'text': 'SEO guide'}
{'url': 'https://example.org/ad', 'rel': ['sponsored', 'nofollow'], 'text': 'this offer'}
{'url': 'https://example.com/logo.html', 'rel': [], 'text': 'Example logo'}
{'url': 'https://example.com/blog/post.html#top', 'rel': ['sponsored'][:0], 'text': 'Back to top'}Wait: the last record's rel is an empty list, so the real output line is:
{'url': 'https://example.com/blog/post.html#top', 'rel': [], 'text': 'Back to top'}Check these points against the output:
- The relative
hrefvalues are now full URLs, so records from different pages can be compared. Sponsored NoFollowbecame['sponsored', 'nofollow']. Attribute values forrelare matched without regard to case.- The image link has anchor text because of its
altattribute. - The
#toplink resolves to the same page. It is a link, but not one that leaves the page.
Common errors
- Using
click hereas anchor text. The link works, but it gives search engines and screen reader users no description of the target. - Putting
nofollowon every outgoing link. It removes endorsement from links you actually do vouch for. Use it only where you do not. - Writing
rel="no-follow"orrel="nofollow,sponsored". The value must benofollow, and values are separated by spaces, not commas. A misspelled value is ignored. - Navigating with JavaScript only. If a menu item changes the page through script and has no
href, a crawler may never find the target. - Linking to the wrong URL variant.
httpversushttps, a trailing slash, or tracking parameters create distinct strings inhref. Link to the canonical version. - Forgetting
sponsoredon paid links. Search engine guidelines expect paid links to be marked. Chapter 9 covers the consequences.
Exercise
A page at https://example.com/blog/post.html contains <a href="../shop/?ref=mail" rel="Sponsored">Buy now</a>. Give the resolved URL, the rel values a crawler would record, and the anchor text. Then say whether <span onclick="location='/shop'">Buy now</span> would give a crawler the same information.
Show answer
The resolved URL is https://example.com/shop/?ref=mail. The rel values are ["sponsored"], because values are matched without regard to case. The anchor text is "Buy now". The <span onclick> version has no href, so it is not an anchor link. A crawler gets no destination URL, no rel values, and no anchor text from it.
Exercise
Name the three parts of a link that a search engine records, and say which one you would change to fix a link whose text reads "click here".
Show answer
The three parts are the href (destination URL), the anchor text, and the rel values. You would change the anchor text, for example to a short phrase that describes the target page.