Skip to content

Add a way to make an entire card into a hyperlink #18294

Closed

Description

@SweVictor

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.

Activity

  1. kkirsche commented on Nov 20, 2015

    @kkirsche
    Contributor

    Would it correct to say that you are essentially trying to use a card as a button?

  2. SweVictor commented on Nov 20, 2015

    @SweVictor
    Author

    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.

  3. kkirsche commented on Nov 20, 2015

    @kkirsche
    Contributor

    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

  4. SweVictor commented on Nov 20, 2015

    @SweVictor
    Author

    Absolutely one card per item. Our test page looked something like this ![Image of card view]
    screen shot 2015-11-20 at 22 31 12

  5. added
    cssSass sources or compiled CSS
    v4v4, the frozen docs on gh-pages
    on Nov 21, 2015
  6. cvrebert commented on Nov 21, 2015

    @cvrebert
    Collaborator

    X-Ref: #18170

  7. cvrebert commented on Dec 7, 2015

    @cvrebert
    Collaborator

    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%;
    }
  8. changed the title [-]Link entire card v4[/-] [+]Add a way to make an entire card into a hyperlink[/+] on Dec 7, 2015
  9. added
    featureNew feature, or a change to how an existing one works
    on Dec 7, 2015
  10. mdo commented on Dec 7, 2015

    @mdo
    Member

    Huh, that's a neat approach. Shits on text selection I imagine, but still neat :).

  11. cvrebert commented on Dec 7, 2015

    @cvrebert
    Collaborator

    Shits on text selection I imagine

    Indeed it does.

  12. modified the milestone: on Oct 24, 2016
  13. wirelessed commented on Mar 15, 2017

    @wirelessed

    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; }

  14. 21 remaining items

  15. patrickhlauke commented on Dec 18, 2018

    @patrickhlauke
    Member

    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.

  16. patrickhlauke commented on Dec 18, 2018

    @patrickhlauke
    Member

    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)

  17. pgmoir commented on Dec 18, 2018

    @pgmoir

    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>
    

    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

  18. Johann-S commented on Dec 19, 2018

    @Johann-S
    Member

    I agree with @patrickhlauke a JS solution seems better

  19. MartijnCuppens commented on Dec 19, 2018

    @MartijnCuppens
    Member

    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.

  20. patrickhlauke commented on Dec 19, 2018

    @patrickhlauke
    Member

    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

  21. MartijnCuppens commented on Dec 19, 2018

    @MartijnCuppens
    Member

    #26184 doesn't wrap the card in a link, but uses a pseudo element which is stretched over the card (or other components).

  22. Johann-S commented on Dec 19, 2018

    @Johann-S
    Member

    Seems an awesome solution @MartijnCuppens from a quick view 👍

  23. MartijnCuppens commented on Jan 5, 2019

    @MartijnCuppens
    Member

    #26184 is merged and will provide a solution for this in v4.3.

  24. boutmos commented on Dec 2, 2019

    @boutmos

    A solution with a little javascript :

    <article>
      <div class="post-link card d-flex flex-lg-row flex-sm-column pt-0 mb-4 p-2">
    
          <div class="align-items-center">
            <?php the_post_thumbnail('medium', array( 'class' => 'card-img-left flex-auto d-none d-md-block')); ?>
          </div>
    
          <div class="card-block d-flex flex-column px-3 pt-0 pb-2 flex-grow-1">
            <h3 class="card-title text-capitalize entry-title mb-2">
              <?php the_title(); ?>
            </h3>
            <?php the_excerpt(); ?>
            <div class="d-flex flex-row flex-grow-1 align-items-end justify-content-end">
                <a class="btn btn-primary btn-sm mt-3" href="<?php esc_url( the_permalink() ) ?>" role="button">Continue Reading</a>
            </div>
          </div>
        </div>
    </article>

    Javascript :

    /**
     * Redirect to a post when a card is clicked in categories template
     */
    
    $(".post-link").click(function() {
      window.location = $(this).find("a").attr("href");
      return false;
    });
  25. kristjankoppel commented on Jan 21, 2020

    @kristjankoppel

    @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.

  26. ArumaConsultancy commented on May 4, 2021

    @ArumaConsultancy

    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

          <div class="card" style="width : 45rem;">        
                    <img class="card-img-top " src="#"  alt="some text... ">
                <div class="card-body">
                     <p class="card-text" >Some Text </p>
                     <a href="#" class="stretched-link" >Read More</a>
                   </div>
                </div>
    

    Instead of card-link class use stretched-link class .

  27. boutmos commented on May 4, 2021

    @boutmos

    Sorry, my bad, I only discovered stretch-link later :/
    Just wanted to help community.

  28. deleted a comment from Miler-maximum60 on Feb 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    cssSass sources or compiled CSSfeatureNew feature, or a change to how an existing one workshas-prAn open pull request addresses itv4v4, the frozen docs on gh-pages

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions