

Nested Links | CSS-Tricks
source link: https://www.tuicool.com/articles/hit/qeEB7vi
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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. That’s harder than it might seem at first glance. The main reason being that…
<!-- this is invalid and won't render as expected --> <a href="#one"> Outside <a href="#two"> Inside </a> </a>
Eric Meyer once called for more flexible linking, but even that doesn’t quite handle a situation where one link is nested inside another.
Here’s what happens with that HTML, by the way:
My first inclination would be to simply not nest the links in the markup, but make them appear nested visually. Some folks replied to the tweet , including Nathan Smith , who shared that same thought: have a relatively positioned parent element and absolutely position both links. The larger one could fill up the whole area and the smaller one could sit on top of it.
See the Pen “Nested” links by Nathan Smith (@nathansmith) on CodePen.
It’s finicky, as you’ll need magic numbers to some degree to handle the spacing and variable content.
My second inclination would be to deal with it in JavaScript.
<div onclick="window.location='https://codepen.io'" style="cursor: pointer;" tab-index="1" > Outside <a href="https://css-tricks.com"> Inside </a> </div>
I have literally no idea how kosher that is from an accessibility perspective. It looks gross to me so I’m just going to assume it’s bad news.
Speaking of accessibility, Heydon Pickering has a whole article about card components which is a popular design pattern where this situation often comes up. His solution is to have a relatively positioned parent element, then a normally-placed and functional main link. That first link has an absolutely positioned pseudo-element on it covering the entire card. Any sub-links are relatively positioned and come after the initial link, so they’d sit on top of the first link by way of z-index
.

And speaking of stacking pseudos, that’s the approach Sean Curtis uses here:
See the Pen Pretend nested links by Sean Curtis (@seancurtis) on CodePen.
Other solutions in the “crafty” territory might be:
Sara Soueidan responded with her own post!
I had the same requirement a couple of years ago when I was building the front-end foundation for Smashing Magazine. So I thought I’d write my response to Chris’s thread out in the form of a blog post.
Sara has written about this with much more detail and care than I have here, so definitely check that out. It looks like both she and Heydon have landed on nearly the same solution, with the pseudo-element cover that contains sub-links poking above it as needed.
Have you ever done it another way? Plenty of UX and a11y to think abbout!
Recommend
-
28
You can make a garden variety anchor link ( <a> ) open up a new email. Let’s take a little journey into this feature. It’s pretty easy to use, but as with anything web, there are lots of things to c...
-
38
I can’t say I use background-clip all that often. I’d wager it’s hardly ever used in day-to-day CSS work. But I was reminded of it in a post by Stefan Judis, which coincidentally was itself a learning-response...
-
6
Links on React and JavaScript Chris Coyier on Jul 13, 2021 Learn Development at
-
5
Links on React and JavaScript II Chris Coyier on Oct 1, 2021 (Updated on Oct 2, 2021) Take your JavaScript to the next level at .
-
4
A common thing I noticed in a lot of projects I worked on is the Nested Render Functions approach to render UI elements. Let's dive into this approach and how to change in a better way. What are Nested Rend...
-
10
Some Links About CSS Gradients DigitalOcean provides cloud products for every stage of your journey. Get started with
-
5
Some Links on AI-Related Stuff DigitalOcean provides cloud products for every stage of your journey. Get started with
-
2
WebKit 要支援 nested CSS 了 從龍哥這邊看到的消息,WebKit 要支援
-
10
Conversation Contributor This P...
-
6
More writings2024-01-07Nested Dark Mode via CSS ProximityNote: This might be more accurately titled “...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK