Nesting Links

May 12, 2020 code video

Nested links are illegal.

Meaning, you can’t have a link sitting inside a block level link. And of course, this makes sense. But, if you’re a fan of block level links, like myself, then you’ve certainly experienced times when you’ve wanted to nest a link.

For example, say you have a blog / content site with a post pattern that features an article link wrapping all the content – image, title, excerpt, etc.

But then, inside that block level link, you’d like to also include the post’s taxonomy, with a link to its archive. No go.

You’d have to either link each item separately (weak), or open, close, and then open the post link again (equally weak).

I’ve run into this issue on several projects in recent memory, especially with our project for Redfin’s Real-Time Blog. So, here’s a little JavaScript-based solution I cooked up.

Data Atts

Essentially, you add your taxonomy (or whatever) in a heading or span tag, and then attach the data attribute [data-nested-link=''] with the taxonomy’s archive link as the value (or whatever). A click event takes that value and builds a ‘link’ on the fly via / window.location.assign

Other posts