Stretched Link
Make any HTML element or Bootstrap component clickable by โstretchingโ a nested link via CSS.
Stretched-link on BootstrapExample
Add .stretched-link
to a link to make its containing block clickable via a ::after
pseudo element. In most cases, this means that an element with position: relative;
that contains a link with the .stretched-link
class is clickable.
Cards have position: relative
by default in Bootstrap, so in this case you can safely add the .stretched-link
class to a link in the card without any other HTML changes.
Multiple links and tap targets are not recommended with stretched links. However, someposition
and z-index
styles can help should this be required.
Card with stretched link
Some quick example text to build on the card title and make up the bulk of the card's content.
Go somewhere<div class="card" style="width: 18rem;">
<img class="card-img-top" src="../../assets/img/generic/1.jpg" alt="..." />
<div class="card-body">
<h5 class="card-title">Card with stretched link</h5>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p><a class="btn btn-primary stretched-link" href="#">Go somewhere</a>
</div>
</div>
Identifying the containing block
If the stretched link doesnโt seem to work, the containing block will probably be the cause. The following CSS properties will make an element the containing block:
- A
position
value other thanstatic
- A
transform
orperspective
value other thannone
- A
will-change
value oftransform
orperspective
- A
filter
value other thannone
or awill-change
value offilter
(only works on Firefox)
Card with stretched links
Some quick example text to build on the card title and make up the bulk of the card's content.
Stretched link will not work here, because position: relative
is added to the link
This stretched link will only be spread over the p
-tag, because a transform is applied to it.
<div class="card shadow-lg" style="width: 18rem;"><img class="card-img-top" src="../../assets/img/generic/2.jpg" alt="..." />
<div class="card-body">
<h6 class="card-title fs-3">Card with stretched links</h6>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
<p class="card-text"><a class="stretched-link text-info" href="#" style="position: relative;">Stretched link will not work here, because <code class="text-danger">position: relative</code> is added to the link</a></p>
<p class="card-text bg-body-tertiary" style="transform: rotate(0);">This <a class="text-warning stretched-link" href="#">stretched link</a> will only be spread over the <code>p</code>-tag, because a transform is applied to it.</p>
</div>
</div>