You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add a way to make an entire card into a hyperlink #18294
I think it would be very useful to be able to link entire cards. We tried doing this in our application, but realised all text got blue which is not ok for us.
I would expect them to behave similar to linked list group items, and think a card view is a nice alternative to plain lists.
Well, maybe.. Is that the analogy for List groups?
Anyway, my analogy would rather be Google Material Design Grid Lists. That is - having a card linking the "primary" action and hopefully optionally supporting secondary actions via extra buttons.
Reacted by Scott Carpenter, Jatin Khosla, Alicia Garcia-Raboso, Charlie, Jeanine Schoessler, janw, mohamed ragaey, Nikhil, Arnaud Doucerain, Ian Golden and 8 more
coming in late, but: as noted early on in this discussion, CSS solutions that overlay/expand a link to cover the whole card generally break text selection. Also, there can be problems in situations where a card actually contains more than one link (one link being the "primary", but then some additional link). In general, I'd say a JS-based solution (like something along the lines of https://codepen.io/patrickhlauke/pen/vpQNgJ) would be my preferred approach here.
if having secondary links inside the card is not a concern / won't ever happen, then wrapping the whole card in a link (and making sure the styling of link text doesn't then affect the look of the whole card's content) is an option, but note that this doesn't always play nice with assistive technologies (who will then, as you're reading through the content of the card, keep announcing that everything is a link, continuously)
I have the simple issue of wanting the whole card to be a link in certain situations, and not intending to have a link within a link, so the following suggested solution may be too simple (and semantically wrong) for the discussion above, but I spent a while looking until I tried this. Since you can't make the card an a tag without losing style, I wrapped the entire contents of the card div in an a tag, and apply a new card-link class
.card.card-link a {
color: inherit;
text-decoration: none;
}
And usage is:
<div class="card card-link">
<a href="examples.html">
<img class="card-img-top" src="images/example_400x200.jpg" alt="example img">
<div class="card-body">
<h5 class="card-title">Example title</h5>
<p class="card-text">This is just a simple example</p>
</div>
</a>
</div>
CSS solutions that overlay/expand a link to cover the whole card generally break text selection
You 're not able to select the text in the js example either, you'll be redirected if you try to do that.
a JS solution seems better
A JS solution will prevent the native advantages like ⌘-click or ctrl-click to open in a new tab, dragging the link to a new tab, right click for link options, cursor:pointer by default, link preview in the browser,...
@patrickhlauke & @Johann-S have you checked out #26184 yet? This provides css-only solution with all the native advantages.
You 're not able to select the text in the js example either, you'll be redirected if you try to do that.
It's possible to add extra checks around that to allow it, but yeh my slapped together demo there currently doesn't.
A JS solution will prevent the native advantages like ⌘-click or ctrl-click to open in a new tab, dragging the link to a new tab, right click for link options, cursor:pointer by default, link preview in the browser,...
If you go to the actual link, you get all of that.
Anyway, IF we can guarantee that there won't be another secondary link in a card (or strongly advise against doing it, since having nested links, or weird stuff like buttons inside links, is not valid), then... shrug
/** * Redirect to a post when a card is clicked in categories template */$(".post-link").click(function(){window.location=$(this).find("a").attr("href");returnfalse;});
Reacted by Kristjan Koppel, Gaël Poupard and epicabsol
Activity
kkirsche commentedon Nov 20, 2015
Would it correct to say that you are essentially trying to use a card as a button?
SweVictor commentedon Nov 20, 2015
Well, maybe.. Is that the analogy for List groups?
Anyway, my analogy would rather be Google Material Design Grid Lists. That is - having a card linking the "primary" action and hopefully optionally supporting secondary actions via extra buttons.
kkirsche commentedon Nov 20, 2015
In my mind, I'd expect a grid list to be made up of multiple cards, not within a single card. Interesting use case though
SweVictor commentedon Nov 20, 2015
Absolutely one card per item. Our test page looked something like this ![Image of card view]

cvrebert commentedon Nov 21, 2015
X-Ref: #18170
cvrebert commentedon Dec 7, 2015
Looks like Twitter's cards do this by putting the
<a>as the first child within the card and then styling it as:{ position: absolute; top: 0; left: 0; height: 100%; width: 100%; }[-]Link entire card v4[/-][+]Add a way to make an entire card into a hyperlink[/+]mdo commentedon Dec 7, 2015
Huh, that's a neat approach. Shits on text selection I imagine, but still neat :).
cvrebert commentedon Dec 7, 2015
Indeed it does.
wirelessed commentedon Mar 15, 2017
I have the same problem too, but the current workaround I have is:
<a href="#" class="card-block clearfix"> <div class="row"> <div class="col-12"> Card </div> </div> </a>and then add a style that's like this
a.card-block { color: black; }21 remaining items
patrickhlauke commentedon Dec 18, 2018
coming in late, but: as noted early on in this discussion, CSS solutions that overlay/expand a link to cover the whole card generally break text selection. Also, there can be problems in situations where a card actually contains more than one link (one link being the "primary", but then some additional link). In general, I'd say a JS-based solution (like something along the lines of https://codepen.io/patrickhlauke/pen/vpQNgJ) would be my preferred approach here.
patrickhlauke commentedon Dec 18, 2018
if having secondary links inside the card is not a concern / won't ever happen, then wrapping the whole card in a link (and making sure the styling of link text doesn't then affect the look of the whole card's content) is an option, but note that this doesn't always play nice with assistive technologies (who will then, as you're reading through the content of the card, keep announcing that everything is a link, continuously)
pgmoir commentedon Dec 18, 2018
I have the simple issue of wanting the whole card to be a link in certain situations, and not intending to have a link within a link, so the following suggested solution may be too simple (and semantically wrong) for the discussion above, but I spent a while looking until I tried this. Since you can't make the card an a tag without losing style, I wrapped the entire contents of the card div in an a tag, and apply a new card-link class
And usage is:
An example of it is use with some additional styling is on the first image card here http://www.lft-concept.co.uk/barebones.html
Johann-S commentedon Dec 19, 2018
I agree with @patrickhlauke a JS solution seems better
MartijnCuppens commentedon Dec 19, 2018
You 're not able to select the text in the js example either, you'll be redirected if you try to do that.
A JS solution will prevent the native advantages like ⌘-click or ctrl-click to open in a new tab, dragging the link to a new tab, right click for link options,
cursor:pointerby default, link preview in the browser,...@patrickhlauke & @Johann-S have you checked out #26184 yet? This provides css-only solution with all the native advantages.
patrickhlauke commentedon Dec 19, 2018
It's possible to add extra checks around that to allow it, but yeh my slapped together demo there currently doesn't.
If you go to the actual link, you get all of that.
Anyway, IF we can guarantee that there won't be another secondary link in a card (or strongly advise against doing it, since having nested links, or weird stuff like buttons inside links, is not valid), then... shrug
MartijnCuppens commentedon Dec 19, 2018
#26184 doesn't wrap the card in a link, but uses a pseudo element which is stretched over the card (or other components).
Johann-S commentedon Dec 19, 2018
Seems an awesome solution @MartijnCuppens from a quick view 👍
MartijnCuppens commentedon Jan 5, 2019
#26184 is merged and will provide a solution for this in
v4.3.boutmos commentedon Dec 2, 2019
A solution with a little javascript :
Javascript :
kristjankoppel commentedon Jan 21, 2020
@boutmos Why you wrote this here? Since version 4.3 we have "stretched links" utility class. See more: https://getbootstrap.com/docs/4.4/utilities/stretched-link/ And for older Bootstrap you can replicate same CSS yourself.
ArumaConsultancy commentedon May 4, 2021
yes , I support @kristjankoppel answer . Bootstrap 4.3 onwards we can use .stretched-link class in a link that make its containing block clickable.
This is the code for reference
Instead of card-link class use stretched-link class .
boutmos commentedon May 4, 2021
Sorry, my bad, I only discovered stretch-link later :/
Just wanted to help community.