Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are nested <a> links legal?

Tags:

html

I'd like to nest some links inside each other in my website's banner, something like:

<a href="/" class="hero-image">
    <a href="/some-page" class="navigation-button">Some page</a>
    <a href="/some-other-page" class="navigation-button">Some other page</a>
    <a href="/yet-another-page" class="navigation-button">Yet another page</a>
</a>

and have the page-specific inner links show up overlaid on top of the big banner link that returns the user to the site's homepage.

I know that wrapping block content in an <a> is legal in HTML 5, so is this legal too?

like image 650
Mark Amery Avatar asked Jan 12 '16 00:01

Mark Amery


People also ask

What is a nested link?

The other day I posted an image, quite literally as a thought exercise, about how you might accomplish “nested” links. That is, a big container that is linked to one URL that contains a smaller container or text link inside of it that goes to another URL.

Can you nest a tags?

According to the specification, an <a> element's content model specifically states that an <a> cannot contain any interactive descendants. An <a> element is interactive, and so therefore you cannot nest an <a> inside another <a> .

How do I link one page to another in HTML?

To make page links in an HTML page, use the <a> and </a> tags, which are the tags used to define the links. The <a> tag indicates where the link starts and the </a> tag indicates where it ends. Whatever text gets added inside these tags, will work as a link. Add the URL for the link in the <a href=” ”>.

Where do I put a link tag in HTML?

The HTML <link> tag is used for defining a link to an external document. It is placed in the <head> section of the document.


2 Answers

Nope.

Two parts of section 4.5.1 The a element in the HTML 5 spec together forbid this:

4.5.1 The a element

Categories:

Flow content.
Phrasing content.
Interactive content.
Palpable content.

...

Content model:

Transparent, but there must be no interactive content descendant.

(emphasis mine).

Since <a> elements cannot contain interactive content, but themselves are interactive content, it follows that an <a> element cannot contain another <a> element.

What's more, this doesn't work in real browsers. If you try out the HTML from the question (fiddle) in a browser, you will observe that the browser makes all four <a> elements siblings, rather than letting the inner ones descend from the outer ones.

like image 107
Mark Amery Avatar answered Oct 11 '22 00:10

Mark Amery


Nesting anchor links are not allowed. The reason is posted in another answer to this post.

However, to achieve the link set-up described in the question, you could do something like this:

HTML (adheres to standards)

<nav id="main-container">
    <a href="/" class="hero-image">Link 1</a>
    <div id="overlaying-container">
        <a href="/some-page" class="navigation-button">Link 2</a>
        <a href="/some-other-page" class="navigation-button">Link 3</a>
        <a href="/yet-another-page" class="navigation-button">Link 4</a>
    </div>
</nav>

CSS

#main-container {
    display: flex;
    flex-direction: column;
    height: 100px;
    position: relative;
}

.hero-image {
    height: 100%;
    width: 100%;
}

#overlaying-container {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
    width: 50%;
    display: flex;
}

.navigation-button {
    box-sizing: border-box;
    text-align: center;
    flex: 1;
    height: 50px;
    padding: 10px;
    margin: 5px;
}

DEMO

like image 36
Michael Benjamin Avatar answered Oct 11 '22 01:10

Michael Benjamin