【问题标题】:Bootstrap overlay over image blocks buttonBootstrap 覆盖图像块按钮
【发布时间】:2021-01-27 21:08:25
【问题描述】:

我目前正在尝试在 Bootstrap 中为卡体创建叠加层。但我不知道现在如何继续,因为覆盖层一直挡住卡内的按钮,我需要能够点击按钮。不知道现在该做什么,一些帮助将是惊人的。

HTML 在这里:

    .rafting {
        background-image: url(./img/rafting.jpg);
        background-size: cover;
        color: white;
        
    }
    
    .rafting:before {
      content: "";
      position: absolute;
      left: 0; right: 0;
      top: 0; bottom: 0;
      background: rgba(0,0,0,.5);
    }
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>

<section class="offres">
  <h2 class="text-center">NOS OFFRES</h2>
  <div class="container">
  <div class="row text-center">
      <div class="column">
        <img class="mb-5" src="./img/picto-logo/picot1.png" alt="Snow" style="width:65%">
        <a href="#" class="btn btn-primary">Go somewhere</a>
      </div>
      <div class="column">
        <img class="mb-5" src="./img/picto-logo/picot2.png" alt="Forest" style="width:65%">
        <a href="#" class="btn btn-primary">Go somewhere</a>
      </div>
      <div class="column">
        <img class="mb-5" src="./img/picto-logo/picot3.png" alt="Mountains" style="width:65%">
         <a href="#" class="btn btn-primary">Hello</a>
      </div>
    </div>
  </div>
</section>

【问题讨论】:

    标签: html css bootstrap-4


    【解决方案1】:

    图像父 div 需要为“position:relative”,叠加层为“position:absolute”。当您将图像悬停时,覆盖显示。

    .column {
      width: 300px;
      color: white;
      position: relative;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    
    .column:before {
      content: "";
      position: absolute;
      left: 0;
      right: 0;
      top: 0;
      bottom: 0;
      background: rgba(0, 0, 0, .5);
      width: 100%;
      height: 100%;
      z-index: 1;
      opacity: 0;
      transition: all 300ms ease;
    }
    
    .column img {
      width: 100%;
    }
    
    .btn-primary {
      position: absolute;
      z-index: 2;
    }
    
    .column:hover:before {
      opacity: 1;
      transition: all 300ms ease;
    }
    <section class="offres">
      <h2 class="text-center">NOS OFFRES</h2>
      <div class="container">
        <div class="row text-center">
          <div class="column">
            <img class="mb-5" src="https://images.pexels.com/photos/844297/pexels-photo-844297.jpeg" alt="Snow">
            <a href="#" class="btn btn-primary">Go somewhere</a>
          </div>
          <div class="column">
            <img class="mb-5" src="https://images.pexels.com/photos/844297/pexels-photo-844297.jpeg" alt="Forest">
            <a href="#" class="btn btn-primary">Go somewhere</a>
          </div>
          <div class="column">
            <img class="mb-5" src="https://images.pexels.com/photos/844297/pexels-photo-844297.jpeg" alt="Mountains">
            <a href="#" class="btn btn-primary">Hello</a>
          </div>
        </div>
      </div>
    </section>

    【讨论】:

      【解决方案2】:

      在您希望位于顶部的元素上使用z-index。我通常从值 9 开始,然后不断添加 9,直到获得所需的效果。

      即:

      .button {
        z-index: 9;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-06-28
        • 1970-01-01
        • 1970-01-01
        • 2022-06-23
        • 2020-12-13
        • 1970-01-01
        • 2011-06-09
        相关资源
        最近更新 更多