【问题标题】:Sticky nav bar at the top [duplicate]顶部的粘性导航栏[重复]
【发布时间】:2020-01-31 03:24:36
【问题描述】:

想问。向下滚动时如何将导航栏粘贴在顶部?我已经搜索了一些教程,但我不知道如何将代码与我的代码结合在一起。当我添加整个导航栏时,它会变得不合适。

下面是我的导航栏样式代码:

<style>
.navbar {
 overflow: hidden;
 background-color:#333;
 }

.navbar a {
float: left;
font-size: 16px;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}

.dropdown {
float: left;
overflow: hidden;
}

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

.navbar a:hover, .dropdown:hover .dropbtn {
background-color: red;
}

.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;
 }

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

  .dropdown:hover .dropdown-content {
  display: block;
  }
  </style>

【问题讨论】:

    标签: html css


    【解决方案1】:

    这是一个相关的问题,可能会有所帮助:Make a nav bar stick to the top when scrolling with css

    基本上你需要这样设置:

    .navbar-fixed {
        top: 0;
        z-index: 100;
        position: fixed;
        width: 100%;
    }
    

    【讨论】:

      【解决方案2】:

      听起来您正在寻找一个粘性导航栏(“粘贴”到视口顶部)。你在使用引导程序吗?如果是这样,请查看Bootstrap's NavBar Documentation

      【讨论】:

      • 我没有使用引导程序
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-08
      • 2023-03-21
      • 1970-01-01
      • 2018-09-10
      相关资源
      最近更新 更多