user5854648
user5854648

Reputation: 1121

Make Bootstrap Card Entirely Clickable

I'm having trouble making the entirety of my Bootstrap 4 Card clickable. I'm using HTML5 and Bootstrap 4 and cannot make the containing card clickable. The image and the text is clickable but I want a user to be able to click anywhere on the box. I have tried wrapping it with a link and the card looks clickable but it is not entirely. Help is appreciated

Codepen: https://codepen.io/anon/pen/PVNXgV

HTML

<div class="container">
  <div class="card-deck flex-row flex-nowrap">

    <div class="card">
      <a href="http://google.com"><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/6/64/Alberobello_BW_2016-10-16_13-43-03.jpg/250px-Alberobello_BW_2016-10-16_13-43-03.jpg" alt="Card image cap"></a>

      <a href="http://google.com">
        <!-- THIS DIV IS NOT CLICKABLE BUT I WANT IT TO BE -->
        <div class="card-body">
          <a href="http://google.com"><h3 class="card-sub align-middle">Card Title</h3></a>
          <p class="time-card">2 Days Ago</p>
        </div><!-- END CARD-BODY -->
      </a>
    </div><!-- END CARD -->

    </div><!-- END CARD DECK -->
</div><!-- END CONTAINER -->

CSS

.card-container{
  max-width:1400px;
  width:95%;
}

.card-body{
  max-width:250px;
  padding-left:10px;
  margin-top:0px;
  transition:.3s;
  -webkit-transition:.3s;
}

.card-deck{
  margin-bottom:3.2rem;
  display: flex;
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

.front-deck{
  padding-top:12rem;
}

.card{
  margin-right:16px;
  border:none;
  flex: 0 0 auto;
}

.card .card-body:first-of-type{
  border:2px solid #96cecf;
  border-top:0px; 
}

Upvotes: 34

Views: 58685

Answers (6)

Victor
Victor

Reputation: 13378

In Bootstrap 4, you could use stretched-link class, that won't change the color of the text in the card too.

Source: https://getbootstrap.com/docs/4.3/utilities/stretched-link/#identifying-the-containing-block

Example:

<div class="card" style="width: 18rem;">
  <img src="..." class="card-img-top" 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 href="#" class="btn btn-primary stretched-link">Go somewhere</a>
  </div>
</div>

Remember to add .position-relative to the parent class in most cases. See the link above for more info.

Upvotes: 62

hawkstrider
hawkstrider

Reputation: 4341

If you don't want to use anchor tags (don't want it to have the a tag restyle the card), you can use some custom js and styling. Something like this.

$(document).ready(() => {
  $(document.body).on('click', '.card[data-clickable=true]', (e) => {
    var href = $(e.currentTarget).data('href');
    window.location = href;
  });
});
.card[data-clickable=true]{
  cursor: pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
<div class="container">
  <div class="card-deck flex-row flex-nowrap">

    <div class="card" data-clickable="true" data-href="http://google.com">
      <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/6/64/Alberobello_BW_2016-10-16_13-43-03.jpg/250px-Alberobello_BW_2016-10-16_13-43-03.jpg" alt="Card image cap">


      <!-- THIS DIV IS NOT CLICKABLE BUT I WANT IT TO BE -->
      <div class="card-body">
        <h3 class="card-sub align-middle">Card Title</h3>
        <p class="time-card">2 Days Ago</p>
      </div>
      <!-- END CARD-BODY -->

    </div>
    <!-- END CARD -->

  </div>
  <!-- END CARD DECK -->
</div>
<!-- END CONTAINER -->

Upvotes: 7

Matthew Varga
Matthew Varga

Reputation: 404

It appears there are a number of workarounds for this, the most common beingeither wrapping the card in an anchor tag or with pseudo elements. See https://github.com/twbs/bootstrap/issues/18294 for more info.

Here is a working codepen from the thread as well: https://codepen.io/blazejewicz/pen/OQEeLR/?editors=0100.

HTML

  <a class="card" href="#" style="width: 18rem;">
  <img class="card-img-top" src="data:image/svg+xml;charset=UTF-8,%3Csvg%20width%3D%22286%22%20height%3D%22180%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20286%20180%22%20preserveAspectRatio%3D%22none%22%3E%3Cdefs%3E%3Cstyle%20type%3D%22text%2Fcss%22%3E%23holder_161beea174e%20text%20%7B%20fill%3Argba(255%2C255%2C255%2C.75)%3Bfont-weight%3Anormal%3Bfont-family%3AHelvetica%2C%20monospace%3Bfont-size%3A14pt%20%7D%20%3C%2Fstyle%3E%3C%2Fdefs%3E%3Cg%20id%3D%22holder_161beea174e%22%3E%3Crect%20width%3D%22286%22%20height%3D%22180%22%20fill%3D%22%23777%22%3E%3C%2Frect%3E%3Cg%3E%3Ctext%20x%3D%22107.1953125%22%20y%3D%2296.3984375%22%3E286x180%3C%2Ftext%3E%3C%2Fg%3E%3C%2Fg%3E%3C%2Fsvg%3E" alt="Card image cap">
  <div class="card-body">
    <h5 class="card-title">Card title</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>

  </div>
</a>
</div>

CSS

a.card {
  color: inherit;
  text-decoration: inherit;
}
a.card button {
  z-index: 1;
}

a.card.disabled,
a.card[disabled] {
  pointer-events: none;
  opacity: .8;
}

JS

(function($) {
  $(() => {
    $('button').on('click', (event) => {
      event.stopImmediatePropagation();
      event.preventDefault();
      alert(`${event.type}: ${event.currentTarget.tagName}`);
    });

    $('a').on('click', (event) => {
      event.preventDefault();
      alert(`${event.type}: ${event.currentTarget.tagName}`);
    });
  });
})(jQuery);

Upvotes: 1

brooksrelyt
brooksrelyt

Reputation: 4025

Your codepen: https://codepen.io/brooksrelyt/pen/VgjzGr

HTML:

<div class="container">
  <div class="card-deck flex-row flex-nowrap">
<a href="http://google.com">
    <div class="card">
      <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/6/64/Alberobello_BW_2016-10-16_13-43-03.jpg/250px-Alberobello_BW_2016-10-16_13-43-03.jpg" alt="Card image cap">


        <!-- THIS DIV IS NOT CLICKABLE BUT I WANT IT TO BE -->
        <div class="card-body">
<h3 class="card-sub align-middle">Card Title</h3>
          <p class="time-card">2 Days Ago</p>
        </div><!-- END CARD-BODY -->
      </a>
    </div><!-- END CARD -->
  </a>

    </div><!-- END CARD DECK -->
</div><!-- END CONTAINER -->

Upvotes: 6

hawks
hawks

Reputation: 931

Instead of having multiple anchor tags you can wrap your entire card in a an anchor tag.

<div class="container">
      <div class="card-deck flex-row flex-nowrap">
        <a href="http://google.com">

          <div class="card">
            <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/6/64/Alberobello_BW_2016-10-16_13-43-03.jpg/250px-Alberobello_BW_2016-10-16_13-43-03.jpg"
              alt="Card image cap">


            <!-- THIS DIV IS NOT CLICKABLE BUT I WANT IT TO BE -->
            <div class="card-body">
              <h3 class="card-sub align-middle">Card Title</h3>
              <p class="time-card">2 Days Ago</p>
            </div><!-- END CARD-BODY -->
          </div><!-- END CARD -->
        </a>
      </div><!-- END CARD DECK -->

    </div><!-- END CONTAINER -->

Upvotes: 2

John
John

Reputation: 536

Have you tried wrapping the whole card in the link tag?

.card-container{
	max-width:1400px;
	width:95%;
}

.card-body{
	max-width:250px;
	padding-left:10px;
	margin-top:0px;
	transition:.3s;
	-webkit-transition:.3s;
}

.card-deck{
	margin-bottom:3.2rem;
	display: flex;
  	flex-wrap: nowrap;
  	overflow-x: auto;
  	-webkit-overflow-scrolling: touch;
}

.front-deck{
	padding-top:12rem;
}

.card{
	margin-right:16px;
	border:none;
	flex: 0 0 auto;
}

.card .card-body:first-of-type{
	border:2px solid #96cecf;
	border-top:0px; 
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
  <div class="card-deck flex-row flex-nowrap">
<a href="http://google.com">
    <div class="card">
      <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/6/64/Alberobello_BW_2016-10-16_13-43-03.jpg/250px-Alberobello_BW_2016-10-16_13-43-03.jpg" alt="Card image cap">
        <!-- THIS DIV IS NOT CLICKABLE BUT I WANT IT TO BE -->
        <div class="card-body">
         <h3 class="card-sub align-middle">Card Title</h3>
          <p class="time-card">2 Days Ago</p>
        </div><!-- END CARD-BODY -->
   
    </div><!-- END CARD -->
   </a>
    </div><!-- END CARD DECK -->
</div><!-- END CONTAINER -->

Upvotes: 2

Related Questions