搜尋

首頁  >  問答  >  主體

將疊加圖像保持在圖像邊界內 - 限制它僅粘附在圖像上

我正在嘗試在圖像上放置一個覆蓋層,但是這個覆蓋層延伸到了頂部的功能表列上,我不知道如何只將其貼在圖像上。

我嘗試了不同的調整圖像和更改位置的方法,但它仍然延伸到功能表列。當我調整頂部位置時,對於全螢幕來說是一個小修復,但一旦在行動裝置上,由於選單被隱藏,它將無法工作。而且,當尺寸改變時,它會在圖像下方流動。

header {
  border-bottom: 5px solid coral;
  font-family: Arial;
}

header a {
  display: inline-block;
  text-decoration: none;
  color: inherit;
}

.bg-image {
  background-image: url('https://images.pexels.com/photos/1647120/pexels-photo-1647120.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1');
  background-size: cover;
  background-position: center;
  background-color: blue;
  background-repeat: no-repeat;
}

.vh-80 {
  min-height: 80vh;
}

.banner-text {
  color: white;
  font-size: 3rem;
  text-align: center;
  font-family: Arial;
}

.bg-overlay>* {
  position: relative;
}

.bg-overlay::before {
  content: "";
  position: absolute;
  top: 0px;
  bottom: 0px;
  left: 0;
  right: 0;
  background: linear-gradient(0deg, rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0));
}
<header class="grid-spaceBetween-middle ">
  <div class="col-2 ">
    <a href="/index.html" class="logo">
      <p>Logo</p>
    </a>
  </div>
  <nav class="col-10">
    <a href="/#.html">About Us</a>
    <a href="/#.html"></a>
    <a href="/#.html">Business Travel</a>
    <a href="/#.html">Be Inspired</a>
    <a href="/#.html">Contact</a>
  </nav>
</header>

<!-- 图像横幅 -->

<section class="grid-spaceAround vh-80 bg-image bg-overlay">
  <div class="col-12-middle banner-text">
    <p>IT'S ALL <strong>GOOD</strong></p>
  </div>
</section>

我嘗試過更改部分,嘗試過相對位置的方法,但都沒有成功。

P粉838563523P粉838563523473 天前655

全部回覆(1)我來回復

  • P粉403804844

    P粉4038048442023-09-21 21:52:26

    .bg-image 上加上position: relative,而不是在覆蓋層.bg-overlay > * 的每個元素上添加。

    header {
      border-bottom: 5px solid coral;
      font-family: Arial;
    }
    
    header a {
      display: inline-block;
      text-decoration: none;
      color: inherit;
    }
    
    .bg-image {
      background-image: url('https://images.pexels.com/photos/1647120/pexels-photo-1647120.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1');
      background-size: cover;
      background-position: center;
      background-color: blue;
      background-repeat: no-repeat;
      position: relative;
    }
    
    .vh-80 {
      min-height: 80vh;
    }
    
    .banner-text {
      color: white;
      font-size: 3rem;
      text-align: center;
      font-family: Arial;
    }
    
    
    .bg-overlay::before {
      content: "";
      position: absolute;
      top: 0px;
      bottom: 0px;
      left: 0;
      right: 0;
      background: linear-gradient(0deg, rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0));
    }
    <header class="grid-spaceBetween-middle ">
      <div class="col-2 ">
        <a href="/index.html" class="logo">
          <p>Logo</p>
        </a>
      </div>
      <nav class="col-10">
        <a href="/#.html">About Us</a>
        <a href="/#.html"></a>
        <a href="/#.html">Business Travel</a>
        <a href="/#.html">Be Inspired</a>
        <a href="/#.html">Contact</a>
      </nav>
    </header>
    
    <!-- Image banner -->
    
    <section class="grid-spaceAround vh-80 bg-image bg-overlay">
      <div class="col-12-middle banner-text">
        <p>IT'S ALL <strong>GOOD</strong></p>
      </div>
    </section>

    回覆
    0
  • 取消回覆