搜尋

首頁  >  問答  >  主體

嘗試在導覽列後面的 html 和 css 中新增背景圖像,以便在滾動時不會顯示空間

我不知道如何讓 html 和 css 中的背景圖像填充在導覽列和頁腳下方。當我滾動導航列的空間只是一個空白時,我希望它滾動時頂部和底部沒有空間。謝謝!

* {
  box-sizing: border-box;
}
  body, html {
  margin: 0 auto;
  padding: 0;
}

.background-container {
  background-image: url("http://placekitten.com/g/500/500");
  background-size: cover;
  background-position: center;
  position: fixed;
  width: 100%;
  height: 100%;
  opacity: 0.5;
  z-index: -1;
  background-repeat: no-repeat;
}


.logo-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 30%; /* Adjust the height as needed */
}

.logo-container img{
  max-width: auto;
  max-height: 600px;
  text-align: center;


}

@media screen and (max-width: 768px) {
  /* Adjust background image for mobile devices */
  .background-container {
    background-position: center;
    height: auto;
  }
}

.footer {
  padding: 25px 0;
  background-color: #f2f2f2;
  bottom: 0;
  width: 100%;
}
.navbar {
  margin-bottom: 0;
  border-radius: 0;
  margin: 0;
  padding: 0;
  width: 100%;
  overflow: auto;
}


@media (max-width: 768px) {
  .logo-container {
    margin-top: 50px;
  }


  .footer {
    position: relative;
  }
  .navbar {
      position: relative;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">

<nav class="navbar navbar-expand-sm navbar-light bg-light">
  <a class="navbar-brand" href="#">The Backyard</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <div class="collapse navbar-collapse" id="navbarNav">
    <ul class="navbar-nav ml-auto">
      <li class="nav-item active">
        <a class="nav-link" href="#home">Home</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#about">About</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#events">Events</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#contact">Contact</a>
      </li>
    </ul>
  </div>
</nav>

<div class="background-container"></div>
  <div class="logo-container">
    <img src="http://placekitten.com/g/100/100" alt="Logo">
    </div>

<footer class="footer">
  <div class="container">
    <p>The Backyard<br>
      at Boca Fiesta & Palomino<br>
      232 1/2 SE 1st st.<br>
      Gainesville, FL 32601</p>
  </div>
</footer>

我嘗試更改 css 和 html 上的程式碼,但似乎沒有任何改變結果。

P粉118698740P粉118698740475 天前568

全部回覆(2)我來回復

  • P粉161939752

    P粉1619397522023-09-11 00:57:37

    我在對您的問題的評論中建議將背景圖像應用於body,但我無法想像您將如何以這種方式應用不透明度。因此,只需將

    移到頁面頂部即可。

    * {
      box-sizing: border-box;
    }
      body, html {
      margin: 0 auto;
      padding: 0;
    }
    
    .background-container {
      background-image: url("http://placekitten.com/g/500/500");
      background-size: cover;
      background-position: center;
      position: fixed;
      width: 100%;
      height: 100%;
      opacity: 0.5;
      z-index: -1;
      background-repeat: no-repeat;
    }
    
    
    .logo-container {
      display: flex;
      justify-content: center;
      align-items: center;
      height: 30%; /* Adjust the height as needed */
    }
    
    .logo-container img{
      max-width: auto;
      max-height: 600px;
      text-align: center;
    
    
    }
    
    @media screen and (max-width: 768px) {
      /* Adjust background image for mobile devices */
      .background-container {
        background-position: center;
        height: auto;
      }
    }
    
    .footer {
      padding: 25px 0;
      background-color: #f2f2f2;
      bottom: 0;
      width: 100%;
    }
    .navbar {
      margin-bottom: 0;
      border-radius: 0;
      margin: 0;
      padding: 0;
      width: 100%;
      overflow: auto;
    }
    
    
    @media (max-width: 768px) {
      .logo-container {
        margin-top: 50px;
      }
    
    
      .footer {
        position: relative;
      }
      .navbar {
          position: relative;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    
    <div class="background-container"></div>
    
    <nav class="navbar navbar-expand-sm navbar-light bg-light">
      <a class="navbar-brand" href="#">The Backyard</a>
      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>
      <div class="collapse navbar-collapse" id="navbarNav">
        <ul class="navbar-nav ml-auto">
          <li class="nav-item active">
            <a class="nav-link" href="#home">Home</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#about">About</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#events">Events</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#contact">Contact</a>
          </li>
        </ul>
      </div>
    </nav>
    
      <div class="logo-container">
        <img src="http://placekitten.com/g/100/100" alt="Logo">
        </div>
    
    <footer class="footer">
      <div class="container">
        <p>The Backyard<br>
          at Boca Fiesta & Palomino<br>
          232 1/2 SE 1st st.<br>
          Gainesville, FL 32601</p>
      </div>
    </footer>

    回覆
    0
  • P粉562845941

    P粉5628459412023-09-11 00:55:31

    我不知道為什麼你創建了一個新元素來放置BG 圖像,而不是僅僅將其放在 標籤上,但你的問題是因為在小螢幕上您在.background-container 元素上設定height:auto 的尺寸。

    您已經建立了一個元素,將其固定位置,然後將其設為 100% 高、100% 寬。當螢幕尺寸較小且您設定 height:auto 時,該元素的高度為 0px,因為該元素內部沒有任何內容。

    * {
      box-sizing: border-box;
    }
      body, html {
      margin: 0 auto;
      padding: 0;
    }
    
    .background-container {
      background-image: url("http://placekitten.com/g/500/500");
      background-size: cover;
      background-position: center;
      position: fixed;
      width: 100%;
      height: 100%;
      opacity: 0.5;
      z-index: -1;
      background-repeat: no-repeat;
    }
    
    
    .logo-container {
      display: flex;
      justify-content: center;
      align-items: center;
      height: 30%; /* Adjust the height as needed */
    }
    
    .logo-container img{
      max-width: auto;
      max-height: 600px;
      text-align: center;
    
    
    }
    
    @media screen and (max-width: 768px) {
      /* Adjust background image for mobile devices */
      .background-container {
        background-position: center;
        /*height: auto;*/    /* <=======  REMOVE THIS */
      }
    }
    
    .footer {
      padding: 25px 0;
      background-color: #f2f2f2;
      bottom: 0;
      width: 100%;
    }
    .navbar {
      margin-bottom: 0;
      border-radius: 0;
      margin: 0;
      padding: 0;
      width: 100%;
      overflow: auto;
    }
    
    
    @media (max-width: 768px) {
      .logo-container {
        margin-top: 50px;
      }
    
    
      .footer {
        position: relative;
      }
      .navbar {
          position: relative;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    
    <nav class="navbar navbar-expand-sm navbar-light bg-light">
      <a class="navbar-brand" href="#">The Backyard</a>
      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>
      <div class="collapse navbar-collapse" id="navbarNav">
        <ul class="navbar-nav ml-auto">
          <li class="nav-item active">
            <a class="nav-link" href="#home">Home</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#about">About</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#events">Events</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#contact">Contact</a>
          </li>
        </ul>
      </div>
    </nav>
    
    <div class="background-container"></div>
      <div class="logo-container">
        <img src="http://placekitten.com/g/100/100" alt="Logo">
        </div>
    
    <footer class="footer">
      <div class="container">
        <p>The Backyard<br>
          at Boca Fiesta & Palomino<br>
          232 1/2 SE 1st st.<br>
          Gainesville, FL 32601</p>
      </div>
    </footer>

    回覆
    0
  • 取消回覆