【问题标题】:How do I get my navbar fixed to the top? [closed]如何将导航栏固定在顶部? [关闭]
【发布时间】:2015-08-11 20:26:07
【问题描述】:

我似乎无法将导航栏固定在顶部。看起来 position: fixed 被另一个元素阻挡,可能是伪元素之一,我不确定。

另外,自从我决定将背景颜色应用到整个 nav_wrapper 后,我的子菜单的边框半径由于某种原因消失了。

HTML:

<!-- NAV -->
<div class="nav_wrapper">
    <ul class="nav">
        <li><a href="#">Home</a></li>
        <li><a href="#">Calandar</a></li>
        <li><a href="#">About Us</a>
        <ul class="sub_nav1">
            <li class="the_pastor"><a href="#">The Pastor</a></li>
            <li><a href="#">History</a></li>
            <li class="about"><a href="#">About Byzantines</a></li>
        </ul>
        </li>
        <li><a href="#">Mass Times</a></li>
        <li><a href="#">Contact Us</a></li>
    </ul>
<div>

CSS:

.nav_wrapper ul {
display: block;
margin: 0;
padding: 0;
text-align: center;
font-size: 0;
/* remove whitespace */
min-width: 5px;
background-color: #993300;
}

.nav_wrapper ul li {
  list-style: none;
  display: inline-block;
  vertical-align: top;
  position: relative;
  font-size: 20px;
  /* font-size reset */
}
/* hides the submenu by default */

.nav_wrapper ul ul {
  display: none;
  top: 100%;
  left: 0;
  position: absolute;
}
/* makes the sub menu appear on hover over list element */

.nav_wrapper ul li:hover > .sub_nav1 {
  display: block;
  width: 100%;
}
/* lists the list items on top of one another */

.nav_wrapper ul .sub_nav1 li {
  position: relative;
  width: 100%;
}
.nav_wrapper ul li a {
  display: block;
  text-decoration: none;
  color: #ffffff;
  padding: 20px;
}
.nav_wrapper li a:hover {
  color: #000;
  background-color: #ffffff;
}
/* Dropdown Menu arrow */

.nav_wrapper ul> li > a:after {
  content: '\25BE';
  line-height: 0;
}
.nav_wrapper ul li > a:only-child:after {
  content: '';
}

/* BORDER RADIUS'S */

.nav_wrapper ul ul .the_pastor {
border-top-left-radius: 0px;
border-bottom-left-radius: 0px;
}

.nav_wrapper ul ul .about {
border-top-right-radius: 0px;
border-bottom-right-radius: 7px;
border-bottom-left-radius: 7px;
}

【问题讨论】:

  • 您如何尝试将其修复到顶部?你想要一个粘性标题?
  • 不幸的是,我在您的 CSS 中找不到“位置:固定”。此外,要使边框半径起作用,您还需要指定其他边框属性,例如边框宽度和颜色。没有边框宽度,边框半径不会生效。

标签: html css navbar


【解决方案1】:

将这两个块添加到您的 css 中: 1.你需要定位外部块,而不是你的 ul 在里面。 2. 把你的半径放在你的 sub ul 上,而不是里面的 li。

.nav_wrapper {
  position: fixed;
  right: 0;
  top: 0;
  left: 0;
}
.sub_nav1 {
  border-bottom-right-radius: 7px;
  border-bottom-left-radius: 7px;
}

【讨论】:

  • 完美运行,我不知道我必须定位容器!谢谢!
  • 安迪,欢迎来到 SO! :) 请使用the tour 来赢得您的第一个闪亮徽章;)
【解决方案2】:

position:fixed 添加到.nav_wrapper 本身,而不是其子元素。

【讨论】:

  • 根本没用
【解决方案3】:

试试这个:

.nav_wrapper {
    line-height: 30px; //to taste
    height: 34px;//to taste
    width: 100%;
    position: fixed;
    top: 0;
    left: 0;
     z-index: 98;
}

【讨论】:

  • 将内部元素放入 nav_wrapper 中,宽度和位置也固定为 100%。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-12
  • 1970-01-01
  • 2015-07-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多