向下滚动后,如何使导航栏停留在页面顶部?

时间:2019-02-19 07:07:43

标签: html css

我正在制作一个投资组合网站,遇到了需要帮助的问题。这是一个单页视差站点。当用户输入文字表示欢迎时,导航栏不在站点内。

一旦用户向下滚动以查看我页面的其余部分,就会显示导航栏。一旦用户向下滚动到导航栏到达该位置的位置,我希望导航栏保持在页面顶部。基本上,这个网站的运作方式http://findmatthew.com/

我尝试将导航栏包装在新的div中,并使用position:sticky,top:0;但是导航栏的样式消失了。

body {
    margin: 0;
    background: #222;
    font-family: 'Work Sans', sans-serif;
    font-weight: 800;
}

.container {
    width: 80%;
    margin: 0 auto;
}

header {
  background: #1f2733;
  margin-top: -22px;     /* <<<--- This moves the entire nav box up or down!!  */
}

header::after {
  content: '';
  display: table;
  clear: both;
}

nav {
  float: center;
}

nav ul {
  margin: 0;
  padding: 0;
  list-style: none;
  padding-top: 10px;
  padding-bottom: 10px;
}

nav li {
  display: inline-block;
  margin-left: 190px;
  position: relative;
    font-family: 'Montserrat', sans-serif;   /*Changing the font of the nav bar*/
}

nav a {
  color: white;
  text-decoration: none;
  text-transform: uppercase;
  font-size: 18px;
}

nav a:hover {
  color: #a23131;
}

nav a::before {
  content: '';
  display: block;
  height: 3px;
  background-color: #a23131;
    margin-top: -10px;
  position: absolute;
  top: 0;
  width: 0%;

  transition: all ease-in-out 250ms;
}

nav a:hover::before {
  width: 100%;
}

2 个答案:

答案 0 :(得分:-1)

/* The navigation bar */

.navbar {

  overflow: hidden;
  
  background-color: #333;
  
  position: fixed; /* Set the navbar to fixed position */
  
  top: 0; /* Position the navbar at the top of the page */
  
  width: 100%; /* Full width */
}

答案 1 :(得分:-1)

nav {
  position: fixed;
  top: 0;
  right: 0;
  left: 0;
}

相关问题