目录

操作方法 - 固定菜单


了解如何使用 CSS 创建"fixed" 菜单。


亲自试一试 »


如何创建固定顶部菜单

步骤1)添加HTML:

示例

<div class="navbar">
  <a href="#home">Home</a>
  <a href="#news">News</a>
  <a href="#contact">Contact</a>
</div>

<div class="main">
  <p>Some text some text some text some text..</p>
</div>

步骤2)添加CSS:

要创建固定顶部菜单,请使用position:fixedtop:0。请注意,固定菜单将覆盖您的其他内容。要解决此问题,请添加margin-top(内容)等于或大于菜单的高度。

示例

/* 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 */
}

/* Links inside the navbar */
.navbar a {
  float: left;
  display: block;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}

/* Change background on mouse-over */
.navbar a:hover {
  background: #ddd;
  color: black;
}

/* Main content */
.main {
  margin-top: 30px; /* Add a top margin to avoid content overlay */
}
亲自试一试 »

如何创建固定底部菜单

要创建固定底部菜单,请使用position:fixed bottom:0

示例

/* The navigation bar */
.navbar {
  position: fixed; /* Set the navbar to fixed position */
  bottom: 0; /* Position the navbar at the bottom of the page */
  width: 100%; /* Full width */
}

/* Main content */
.main {
  margin-bottom: 30px; /* Add a bottom margin to avoid content overlay */
}
亲自试一试 »

提示:去我们的CSS 导航栏教程了解有关导航栏的更多信息。