How to make elements behave like links without wrapping them in anchor tags
Frontend Engineer who loves building tools.
Search for a command to run...
Frontend Engineer who loves building tools.
No comments yet. Be the first to comment.
Performance is a touchy subject in software engineering. It’s one thing to write code and ship it; it’s another thing entirely to make it faster and more enjoyable for your users — and the next developers after you. In this article, we will look at h...
Animations seem to be a necessity these days in frontend development, improving the user experience in apps by a significant margin. Thankfully vue.js shipped two built-in components that abstract away the complexities of animating our apps; transiti...
Arrays have a lot of helpful higher-order functions that make working with them easier. Our focus in this article will be the sort() method, this method has both easy and complex use cases which we'll be looking at in this article. First look at the ...
Gridsome is a static site generator used for building powerful frontend applications, it was built on the Vue.js framework. One of the beauties of Gridsome apart from its ability to generate server-side rendered applications is its compatibility with...
Build hooks are unique URLs used to trigger a build on the Netlify. They can be used to refresh the content on your website after you’ve published or unpublished content in your Storyblok space. Getting the hook URL from Netlify To create one, open y...

This question has existed in different forms around the internet; "how to turn a div into a clickable link", "how to make div behave like link" etc. The most obvious way would be to wrap everything between a pair of anchor tags like this:
<a href="//google.com">
<div>
<p> Remaining elements here </p>
</div>
</a>
Now this would work, assuming you're targeting only modern browsers that have support for HTML 5, because the HTML 4 specs did not approve of this. It would not work in older browsers because in HTML 4, inline elements can not be parents to block elements.
Another technique would have been to use an onclick listener attribute on the block element, but that's an accessibility no no.
So how do you make a div or any other block element behave like a link?(without surrounding them with an anchor tag of course)
short answer: with the help of CSS.
Long answer: keep reading please.
With the help of CSS positioning, we'll make the the link cover the entire area of the div whilst retaining it's structure in the DOM.
Take a look at the code sample below:
<div class="outer-element">
<h2> The title to an article</h2>
<p> The description to the article</p>
<a href="path-to-article" class="link">link to article</a>
</div>
By setting the position of .outer-element to relative and .link to absolute with a few other CSS properties, we're able to make the anchor element cover the entire div without the text content of the link being visible:
.outer-element{
width: 500px;
height: 600px;
box-shadow: 0px 0px 10px 1px #f1f1f1;
position: relative;
}
.link{
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
text-decoration: none;
z-index: 10;
opacity: 0;
}
if you test the code snippets above, you should see that the link doesn't appear visibly but when you click the box you get redirected to the new destination.