当Chrome浏览器上的导航栏处于正常的非响应模式(我不确定这是否是正确的术语)并且向下滚动时,向下滚动时徽标图像仍位于菜单后面。但是,当屏幕宽度缩小并且菜单折叠时,单击图标以展开菜单,它将徽标图像向下推。在移动设备上查看时,我希望该徽标图像保留在菜单后面。

我想以后会有一些我想压低的内容,但是我不想压低这个徽标。我可以将其作为正在使用的背景的一部分,但我认为,如果可以将其保留为单独的图像进行显示,它将更好地呈现。

https://jsfiddle.net/martonian/Ljd5v3ty/

的CSS

#logo {

    width: 50%;
    margin: 80px auto;

}

.topnav {

  background-image: url(http://www.buffettworld.com/images/navmenubg.png);
  background-size: 100%;
  font-family: 'Pathway Gothic One', sans-serif;
  margin-top: 10px;
  position: fixed;
  top: 0;
  width: 100%;
}


.topnav a {
  float: left;
  display: block;
  color: white;
  text-align: center;
  padding: 14px 25px;
  text-decoration: none;
  font-size: 21px;

}

.topnav .icon {
  display: none;
}

.dropdown {
    float: left;
    overflow: hidden;

}

.dropdown .dropbtn {
    font-size: 21px;
    border: none;
    outline: none;
    color: white;
    padding: 14px 25px;
    background-color: inherit;
    font-family: inherit;
    margin: 0;

}

.dropdown-content {
    display: none;
    position: absolute;
    background-color: #f9f9f9;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    z-index: 1;

}

.dropdown-content a {
    float: none;
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
    text-align: left;
}

.topnav a:hover, .dropdown:hover .dropbtn {
  background-color: #b1d235;
  color: white;
}

.dropdown-content a:hover {
    background-color: #ddd;
    color: black;
}

.dropdown:hover .dropdown-content {
    display: block;
}

@media screen and (max-width: 600px) {
  .topnav a:not(:first-child), .dropdown .dropbtn {
    display: none;
  }
  .topnav a.icon {
    float: right;
    display: block;
  }
}

@media screen and (max-width: 600px) {
  .topnav.responsive {position: relative;}
  .topnav.responsive .icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  .topnav.responsive a {
    float: none;
    display: block;
    text-align: left;
  }
  .topnav.responsive .dropdown {float: none;}
  .topnav.responsive .dropdown-content {position: relative;}
  .topnav.responsive .dropdown .dropbtn {
    display: block;
    width: 100%;
    text-align: left;
  }
}

.sticky {
  position: fixed;
  top: 10px;
  width: 100%
}


的HTML

<div class="topnav" id="myTopnav">
  <a href="#home">HOME</a>
  <a href="#news">NEWS</a>
  <a href="#news">TOUR</a>
    <div class="dropdown">
    <button class="dropbtn">JIMMY
      <i class="fa fa-caret-down"></i>
    </button>
    <div class="dropdown-content">
        <a href="#">BIOGRAPHY</a>
        <a href="#">AVIATION</a>
        <a href="#">INCIDENTS</a>
        <a href="#">BUSINESS EMPIRE</a>
        <a href="#">CONTACT JIMMY</a>
    </div>
        </div>
          <div class="dropdown">
    <button class="dropbtn">MUSIC
      <i class="fa fa-caret-down"></i>
    </button>
    <div class="dropdown-content">
        <a href="#">ALBUMS</a>
        <a href="#">SONGS</a>
        <a href="#">CHART HISTORY</a>
        <a href="#">TRIBUTE BANDS</a>
  </div>
        </div>
        <a href="#about">SET LISTS</a>
        <a href="#about">ABOUT</a>
        <a href="#about">ADVERTISE</a>
        <a href="#about">CONTACT</a>
  <a href="javascript:void(0);" style="font-size:15px;" class="icon" onclick="myFunction()">&#9776;</a>
</div>


<script>
function myFunction() {
    var x = document.getElementById("myTopnav");
    if (x.className === "topnav") {
        x.className += " responsive";
    } else {
        x.className = "topnav";
    }
}
</script>



    <div id="logo">

        <img src="http://www.buffettworld.com/images/logo.png">
    </div>

最佳答案

使您的nanav位置绝对。在您的max-width: 600px媒体查询中

.topnav.responsive {position: absolute;}


希望这可以帮助 :)



#logo {

	width: 50%;
	margin: 80px auto;

}

.topnav {

  background-image: url(http://www.buffettworld.com/images/navmenubg.png);
  background-size: 100%;
  font-family: 'Pathway Gothic One', sans-serif;
  margin-top: 10px;
  position: fixed;
  top: 0;
  width: 100%;
}


.topnav a {
  float: left;
  display: block;
  color: white;
  text-align: center;
  padding: 14px 25px;
  text-decoration: none;
  font-size: 21px;

}

.topnav .icon {
  display: none;
}

.dropdown {
    float: left;
    overflow: hidden;

}

.dropdown .dropbtn {
    font-size: 21px;
    border: none;
    outline: none;
    color: white;
    padding: 14px 25px;
    background-color: inherit;
    font-family: inherit;
    margin: 0;

}

.dropdown-content {
    display: none;
    position: absolute;
    background-color: #f9f9f9;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    z-index: 1;

}

.dropdown-content a {
    float: none;
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
    text-align: left;
}

.topnav a:hover, .dropdown:hover .dropbtn {
  background-color: #b1d235;
  color: white;
}

.dropdown-content a:hover {
    background-color: #ddd;
    color: black;
}

.dropdown:hover .dropdown-content {
    display: block;
}

@media screen and (max-width: 600px) {
  .topnav a:not(:first-child), .dropdown .dropbtn {
    display: none;
  }
  .topnav a.icon {
    float: right;
    display: block;
  }
}

@media screen and (max-width: 600px) {
  .topnav.responsive {position: absolute;}
  .topnav.responsive .icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  .topnav.responsive a {
    float: none;
    display: block;
    text-align: left;
  }
  .topnav.responsive .dropdown {float: none;}
  .topnav.responsive .dropdown-content {position: relative;}
  .topnav.responsive .dropdown .dropbtn {
    display: block;
    width: 100%;
    text-align: left;
  }
}

.sticky {
  position: fixed;
  top: 10px;
  width: 100%
}

<div class="topnav" id="myTopnav">
  <a href="#home">HOME</a>
  <a href="#news">NEWS</a>
  <a href="#news">TOUR</a>
    <div class="dropdown">
    <button class="dropbtn">JIMMY
      <i class="fa fa-caret-down"></i>
    </button>
    <div class="dropdown-content">
		<a href="#">BIOGRAPHY</a>
		<a href="#">AVIATION</a>
		<a href="#">INCIDENTS</a>
		<a href="#">BUSINESS EMPIRE</a>
		<a href="#">CONTACT JIMMY</a>
    </div>
		</div>
		  <div class="dropdown">
    <button class="dropbtn">MUSIC
      <i class="fa fa-caret-down"></i>
    </button>
    <div class="dropdown-content">
		<a href="#">ALBUMS</a>
		<a href="#">SONGS</a>
		<a href="#">CHART HISTORY</a>
		<a href="#">TRIBUTE BANDS</a>
  </div>
		</div>
		<a href="#about">SET LISTS</a>
  		<a href="#about">ABOUT</a>
		<a href="#about">ADVERTISE</a>
		<a href="#about">CONTACT</a>
  <a href="javascript:void(0);" style="font-size:15px;" class="icon" onclick="myFunction()">&#9776;</a>
</div>


<script>
function myFunction() {
    var x = document.getElementById("myTopnav");
    if (x.className === "topnav") {
        x.className += " responsive";
    } else {
        x.className = "topnav";
    }
}
</script>



	<div id="logo">

		<img src="http://www.buffettworld.com/images/logo.png">
	</div>

关于css - 响应式Navbar压低内容,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49862516/

10-10 13:23