On this page
A card that appears to contain two links can send a click somewhere unexpected when one anchor is placed inside another. The browser may repair the HTML while parsing it, leaving a DOM that differs from the source. Scripts and overlapping elements can also make a valid-looking card behave unexpectedly.
This guide shows how to investigate nested links in HTML and choose accessible markup for cards with multiple destinations or actions.
Key Takeaways
- An anchor with an
hrefmust not contain another anchor, interactive content such as a button, or a descendant with a specifiedtabindex. - Browsers can repair nested anchors in HTML source, so inspect the rendered DOM rather than assuming it matches the source.
- Use separate links for separate destinations and buttons for actions. Give each control a clear name and keyboard focus.
- A wrong destination can also come from an event handler, an overlay, a redirect, or a different
href; invalid nesting is not the only possible cause.
Reproduce the problem
Save this as nested-links.html and open it in a browser. The paths are illustrative and do not need to exist for DOM inspection.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Nested link example</title>
</head>
<body>
<a href="/stories/example">
<span>Read the story</span>
<a href="/authors/example">Author profile</a>
</a>
</body>
</html>
The source suggests that the author link is inside the story link. However, when parsing this HTML, the browser can close the first anchor before creating the second. Consequently, the DOM and the clickable areas may not match what the author intended. Dynamically inserting elements with JavaScript can produce a different DOM from parsing the same-looking HTML source, so examine the actual page in both cases.
The exact destination a user reaches also depends on where they click and on any scripts, styling, or redirects. Invalid markup alone does not prove that every click will go to the wrong URL.
Advertisement
Inspect the rendered DOM and destinations
- Open browser developer tools and select the Elements or Inspector panel. Locate the visible text and expand its surrounding elements. Check whether the anchors are siblings or descendants in the rendered DOM.
- Compare that DOM with the original page source or template. Developer tools show the current DOM, which may have been changed by the HTML parser or by scripts after loading.
- Select each anchor in the inspector. Check its
hrefattribute and look for overlapping elements in the layout. In browsers that expose the selected element as$0, use these Console expressions separately:
$0.getAttribute('href')
$0.href
The first expression returns the attribute as written in the DOM; the second gives the browser-resolved URL for an anchor. Select an actual anchor before running them. A relative path can resolve differently depending on the document URL and any base URL.
- Inspect event listeners on the anchor and its ancestors. A listener might prevent the default navigation, change the destination, or handle a click on the card itself. Also check whether a positioned element sits over the intended control.
- In a safe local or staging environment, activate the link and use the Network panel or address bar to distinguish the initial link destination from a later redirect. Avoid testing destructive actions or private URLs on a live site.
If the DOM changes after rendering, inspect it again after the page has finished loading and after the interaction that triggers the problem. Framework components and content-management templates can introduce nesting even when each component looks reasonable in isolation.
Use separate links and buttons in a card
A card can have a primary navigation link, another link, and a button without nesting interactive elements. This example uses a button to reveal a summary; its links point to illustrative application routes.
<article class="card">
<h2><a href="/stories/example">Read the story</a></h2>
<p>By <a href="/authors/example">Example author</a></p>
<button type="button" id="summary-toggle"
aria-expanded="false" aria-controls="story-summary">
Show summary
</button>
<p id="story-summary" hidden>
A short summary of the story.
</p>
</article>
<script>
const toggle = document.getElementById('summary-toggle');
const summary = document.getElementById('story-summary');
toggle.addEventListener('click', () => {
const opening = summary.hidden;
summary.hidden = !opening;
toggle.setAttribute('aria-expanded', String(opening));
toggle.textContent = opening ? 'Hide summary' : 'Show summary';
});
</script>
Place the script after the elements, as shown. This is a small illustrative example, not a complete card component. If JavaScript is unavailable, the initially hidden summary cannot be opened; provide the content another way if it is essential.
Use a link when activation navigates to a URL. Use a button for an in-page action, such as expanding details or opening a dialog. Do not put the button inside the link or attach a competing navigation handler to the entire card. If a larger click target is needed, enlarge the primary link's own area without covering the other controls, and check that each control remains independently clickable and focusable.
Advertisement
Check keyboard and screen-reader behavior
The examples in this article were not executed, and keyboard and screen-reader checks were not performed in this environment. Test the actual implementation with the browsers and assistive technologies your audience uses:
- Reload the page and use Tab and Shift+Tab to move through the controls. Each link and button should receive focus in a sensible order, with a visible focus indicator.
- Press Enter on each link and confirm it navigates to its intended destination. On the summary button, check that Enter and Space toggle the content without navigating.
- With a screen reader, navigate through the card and use its link-list or links-navigation feature if available. Check that the story and author links have distinguishable names and that the button is announced as a button with its expanded or collapsed state. Exact announcements vary by browser and screen reader.
- Repeat with a pointer or touch input. Confirm that the primary link's click area does not obscure the author link or button.
The WHATWG HTML Standard and WCAG guidance are relevant to the markup and accessibility checks above, but they could not be consulted in this environment. Check the applicable standard and guidance directly when assessing conformance; this troubleshooting procedure alone does not establish WCAG conformance.
Common Mistakes
- Reading only the template: The browser's parsed DOM or framework-rendered DOM may differ from the source.
- Wrapping an entire interactive card in a link: A nested button or second anchor creates invalid markup and can make interaction unpredictable.
- Making a generic container act like a link: A click handler on a
divdoes not automatically provide native link semantics or keyboard behavior. - Using the same vague link text for different destinations: Distinct names help people identify links when navigating out of context.
- Assuming a correct
hrefsettles the issue: Overlays, event handlers, base URLs, and redirects can still change the result.
Best Practices
- Keep interactive controls as separate elements and use native anchors and buttons for their intended purposes.
- Inspect both markup and actual interaction behavior after rendering, including at different viewport sizes.
- Test keyboard focus, activation, and screen-reader names and states as part of the component review.
- Check content-editor output and reusable templates for nested controls before publishing.
Conclusion
When a card sends users to the wrong destination, compare its source with the rendered DOM, inspect each resolved href, and check overlays and event handlers. Separate links and buttons usually make the intended interactions easier to understand, test, and maintain.
What would your domain sell for?
A free TidyURLs account unlocks the Domain Estimator. It prices a name from 191,011 real domain sales.