【问题标题】:Fixed navbar appears in front of content [duplicate]固定导航栏出现在内容前面[重复]
【发布时间】:2021-04-09 21:59:01
【问题描述】:

代码如下:

body {
  margin: 0;
  padding: 0;
}

/* NAVIGATION */
nav {
  background: #222222;
  padding: 10px 40px 10px 70px;
  color: #ffffff;
}

header {
  position: fixed;
  width: 100%;
  top: 0;
}

#nav-list {
  display: flex;
  list-style: none;
  justify-content: space-around;
  align-items: center;
  padding-left: 0;
}

#search {
  height: 40px;
  width: 240px;
  display: flex;
  border-radius: 5px;
}
 
#searchBar {
  height: 100%;
  width: 200px;
  border: none;
  border-radius: 5px 0 0 5px;
  outline: none;
  padding: 0 10px;
  color: #000;
  font-size: 16px;
} 

#search #searchButton {
  height: 100%;
  width: 40px;
  line-height: 40px;
  text-align: center;
  border-radius: 0 5px 5px 0;
  cursor: pointer;
  background-color: rgb(81, 22, 219);
  color: #222222;
  transition: color 0.75s;
}

#search #searchButton:hover {
  color: #fff;
}

#page-title {
  font-size: 24px;
}

/* MAIN CONTENT */

#content {
  /* test to see if the color will appear */
  background: blue;
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Movie Vault</title>
  <link rel="stylesheet" href="test.css">
</head>
<body>

  <header>
    <nav>
      <ul id="nav-list">
        <li id="search">
          <input id="searchBar" type="search" placeholder="Search...">
          <label id="searchButton">
            <i class="fas fa-search"></i>
          </label>
        </li>
        <li id="page-title">Video Vault</li>
      </ul>
    </nav>
  </header>

  <section>
    test
  </section>

</body>
</html>

我想要实现的目标是获取 section 标签中的文本并将其直接放在我的固定导航栏下方。我试图将值大于 1 的 z-index 放入 header、nav 和 #nav-list 中,但没有奏效。可以重新配置什么来完成这项工作?我很抱歉。我已经尝试了所有我知道的并搜索了这个网站,但没有成功。

【问题讨论】:

  • 编辑了我的帖子。
  • position: fixedabsolute 将导航栏置于正常流程之外。这是正常的行为。内容需要与导航栏高度相等的上边距,或者您需要使用position: sticky

标签: html css


【解决方案1】:

我只需添加一个margin-top 相当于导航偏移高度,在本例中为 106 像素。

body {
  margin: 0;
  padding: 0;
}

/* NAVIGATION */
nav {
  background: #222222;
  padding: 10px 40px 10px 70px;
  color: #ffffff;
}

header {
  position: fixed;
  width: 100%;
  top: 0;
}

#nav-list {
  display: flex;
  list-style: none;
  justify-content: space-around;
  align-items: center;
  padding-left: 0;
}

#search {
  height: 40px;
  width: 240px;
  display: flex;
  border-radius: 5px;
}
 
#searchBar {
  height: 100%;
  width: 200px;
  border: none;
  border-radius: 5px 0 0 5px;
  outline: none;
  padding: 0 10px;
  color: #000;
  font-size: 16px;
} 

#search #searchButton {
  height: 100%;
  width: 40px;
  line-height: 40px;
  text-align: center;
  border-radius: 0 5px 5px 0;
  cursor: pointer;
  background-color: rgb(81, 22, 219);
  color: #222222;
  transition: color 0.75s;
}

#search #searchButton:hover {
  color: #fff;
}

#page-title {
  font-size: 24px;
}

/* MAIN CONTENT */

#content {
  /* test to see if the color will appear */
  background: blue;
}
section{
  margin-top:106px;
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Movie Vault</title>
  <link rel="stylesheet" href="test.css">
</head>
<body>

  <header>
    <nav>
      <ul id="nav-list">
        <li id="search">
          <input id="searchBar" type="search" placeholder="Search...">
          <label id="searchButton">
            <i class="fas fa-search"></i>
          </label>
        </li>
        <li id="page-title">Video Vault</li>
      </ul>
    </nav>
  </header>

  <section>
    test
  </section>

</body>
</html>
如果您有响应式导航栏,则可以使用 JS 动态设置边距顶部。

document.getElementsByTagName("section")[0].style.marginTop = document.getElementsByTagName("nav")[0].offsetHeight+"px";
body {
  margin: 0;
  padding: 0;
}

/* NAVIGATION */
nav {
  background: #222222;
  padding: 10px 40px 10px 70px;
  color: #ffffff;
}

header {
  position: fixed;
  width: 100%;
  top: 0;
}

#nav-list {
  display: flex;
  list-style: none;
  justify-content: space-around;
  align-items: center;
  padding-left: 0;
}

#search {
  height: 40px;
  width: 240px;
  display: flex;
  border-radius: 5px;
}
 
#searchBar {
  height: 100%;
  width: 200px;
  border: none;
  border-radius: 5px 0 0 5px;
  outline: none;
  padding: 0 10px;
  color: #000;
  font-size: 16px;
} 

#search #searchButton {
  height: 100%;
  width: 40px;
  line-height: 40px;
  text-align: center;
  border-radius: 0 5px 5px 0;
  cursor: pointer;
  background-color: rgb(81, 22, 219);
  color: #222222;
  transition: color 0.75s;
}

#search #searchButton:hover {
  color: #fff;
}

#page-title {
  font-size: 24px;
}

/* MAIN CONTENT */

#content {
  /* test to see if the color will appear */
  background: blue;
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Movie Vault</title>
  <link rel="stylesheet" href="test.css">
</head>
<body>

  <header>
    <nav>
      <ul id="nav-list">
        <li id="search">
          <input id="searchBar" type="search" placeholder="Search...">
          <label id="searchButton">
            <i class="fas fa-search"></i>
          </label>
        </li>
        <li id="page-title">Video Vault</li>
      </ul>
    </nav>
  </header>

  <section>
    test
  </section>

</body>
</html>

【讨论】:

    【解决方案2】:

    position: fixed; position: absolute; 将标头置于正常流程之外。请改用position: sticky;

    body {
      margin: 0;
      padding: 0;
    }
    
    /* NAVIGATION */
    nav {
      background: #222222;
      padding: 10px 40px 10px 70px;
      color: #ffffff;
    }
    
    header {
      position: sticky;
      width: 100%;
      top: 0;
    }
    
    #nav-list {
      display: flex;
      list-style: none;
      justify-content: space-around;
      align-items: center;
      padding-left: 0;
    }
    
    #search {
      height: 40px;
      width: 240px;
      display: flex;
      border-radius: 5px;
    }
     
    #searchBar {
      height: 100%;
      width: 200px;
      border: none;
      border-radius: 5px 0 0 5px;
      outline: none;
      padding: 0 10px;
      color: #000;
      font-size: 16px;
    } 
    
    #search #searchButton {
      height: 100%;
      width: 40px;
      line-height: 40px;
      text-align: center;
      border-radius: 0 5px 5px 0;
      cursor: pointer;
      background-color: rgb(81, 22, 219);
      color: #222222;
      transition: color 0.75s;
    }
    
    #search #searchButton:hover {
      color: #fff;
    }
    
    #page-title {
      font-size: 24px;
    }
    
    /* MAIN CONTENT */
    
    #content {
      /* test to see if the color will appear */
      background: blue;
    }
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Movie Vault</title>
      <link rel="stylesheet" href="test.css">
    </head>
    <body>
    
      <header>
        <nav>
          <ul id="nav-list">
            <li id="search">
              <input id="searchBar" type="search" placeholder="Search...">
              <label id="searchButton">
                <i class="fas fa-search"></i>
              </label>
            </li>
            <li id="page-title">Video Vault</li>
          </ul>
        </nav>
      </header>
    
      <section>
        test
      </section>
    
    </body>
    </html>

    或者,您可以在该部分使用上边距:

    body {
      margin: 0;
      padding: 0;
    }
    
    /* NAVIGATION */
    nav {
      background: #222222;
      padding: 10px 40px 10px 70px;
      color: #ffffff;
    }
    
    header {
      position: fixed;
      width: 100%;
      top: 0;
    }
    
    #nav-list {
      display: flex;
      list-style: none;
      justify-content: space-around;
      align-items: center;
      padding-left: 0;
    }
    
    #search {
      height: 40px;
      width: 240px;
      display: flex;
      border-radius: 5px;
    }
     
    #searchBar {
      height: 100%;
      width: 200px;
      border: none;
      border-radius: 5px 0 0 5px;
      outline: none;
      padding: 0 10px;
      color: #000;
      font-size: 16px;
    } 
    
    #search #searchButton {
      height: 100%;
      width: 40px;
      line-height: 40px;
      text-align: center;
      border-radius: 0 5px 5px 0;
      cursor: pointer;
      background-color: rgb(81, 22, 219);
      color: #222222;
      transition: color 0.75s;
    }
    
    #search #searchButton:hover {
      color: #fff;
    }
    
    #page-title {
      font-size: 24px;
    }
    
    /* MAIN CONTENT */
    
    #content {
      /* test to see if the color will appear */
      background: blue;
    }
    
    section {
      margin-top: 90px;
    }
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Movie Vault</title>
      <link rel="stylesheet" href="test.css">
    </head>
    <body>
    
      <header>
        <nav>
          <ul id="nav-list">
            <li id="search">
              <input id="searchBar" type="search" placeholder="Search...">
              <label id="searchButton">
                <i class="fas fa-search"></i>
              </label>
            </li>
            <li id="page-title">Video Vault</li>
          </ul>
        </nav>
      </header>
    
      <section>
        test
      </section>
    
    </body>
    </html>

    【讨论】:

      猜你喜欢
      • 2021-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-10
      • 1970-01-01
      • 2020-07-15
      • 2015-08-01
      • 1970-01-01
      相关资源
      最近更新 更多