使Bootstrap卡完全可点击

时间:2019-01-28 15:11:25

标签: html css html5 twitter-bootstrap bootstrap-4

我无法使整个Bootstrap 4卡可点击。我正在使用HTML5和Bootstrap 4,无法使包含的卡可点击。图像和文本是可单击的,但是我希望用户能够在框中的任何位置单击。我尝试用链接包装它,该卡片看起来可点击,但并非完全可以。感谢帮助

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

6 个答案:

答案 0 :(得分:4)

在Bootstrap 4中,您可以使用stretched-link类,该类也不会更改卡中文本的颜色。

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

示例:

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

在大多数情况下,请记住将.position-relative添加到父类。有关更多信息,请参见上面的链接。

答案 1 :(得分:3)

如果您不想使用锚标签(不希望其标签改变卡的样式),则可以使用一些自定义js和样式。像这样的东西。

$(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 -->

答案 2 :(得分:1)

您是否尝试过将整个卡包装在链接标签中?

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

答案 3 :(得分:1)

您可以将整个卡包裹在一个锚定标签中,而不用使用多个锚定标签。

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

答案 4 :(得分:1)

您的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 -->

答案 5 :(得分:1)

似乎有许多解决方法,最常见的是将卡包装在锚标签中或使用伪元素。有关更多信息,请参见https://github.com/twbs/bootstrap/issues/18294

这里也是该线程中有效的代码笔: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);