Cuba tambah imej latar belakang dalam html dan css di belakang bar navigasi supaya ruang tidak muncul semasa menatal
P粉118698740
P粉118698740 2023-09-10 09:12:07
0
2
537

Saya tidak tahu bagaimana untuk mengisi imej latar belakang di bawah navbar dan pengaki dalam html dan css. Apabila saya menatal bar navigasi ruang hanya ruang kosong, saya mahu ia menatal tanpa ruang di bahagian atas dan bawah. Terima kasih!

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

Saya cuba menukar kod pada css dan html tetapi nampaknya tiada apa yang mengubah keputusan.

P粉118698740
P粉118698740

membalas semua(2)
P粉161939752

Dalam ulasan saya kepada soalan anda, saya mencadangkan untuk mengalihkan imej latar belakang body,但我无法想象您将如何以这种方式应用不透明度。因此,只需将

ke bahagian atas halaman.

* {
  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>
P粉562845941

Saya tidak tahu mengapa anda mencipta elemen baharu untuk meletakkan imej BG dan bukannya hanya meletakkannya pada saiz 标签上,但你的问题是因为在小屏幕上您在 .background-container 元素上设置 height:auto.

Anda telah mencipta elemen, menetapkan kedudukannya dan kemudian menetapkannya kepada 100% tinggi dan 100% lebar. Apabila saiz skrin lebih kecil dan anda tetapkan height:auto 时,该元素的高度为 0px kerana tiada apa-apa di dalam elemen.

* {
  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>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!