CSS: 悬停时导航项顶部的边框

CSS: Border on top of navigation item when hovering

这是模型的屏幕截图:

这是我的:

我仍在处理阴影,底部边框将在容器的整个宽度上保持静态。

这是我的标记:

<div class="masthead">
    <div class="container">
        <a href="#" class="navbar-brand"></a>
    </div>

    <nav class="collapse navbar-collapse">
        <div class="container">
            <div class="row">
                <div class="col-lg-12">
                    <ul class="nav navbar-nav pull-right">
                        <li>How we do it</li>
                        <li>About</li>
                        <li>Clients</li>
                        <li>Whats new</li>
                        <li>Contact</li>
                    </ul>
                </div>
            </div>
        </div>
    </nav>
</div>

这是我的 sass:

.masthead {
    padding-top: 10px;

    nav {
        &.collapse {
            background: -moz-linear-gradient(top,  rgba(0,0,0,0.65) 0%, rgba(0,0,0,0) 100%);
            background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(0,0,0,0.65)), color-stop(100%,rgba(0,0,0,0)));
            background: -webkit-linear-gradient(top,  rgba(0,0,0,0.65) 0%,rgba(0,0,0,0) 100%);
            background: -o-linear-gradient(top,  rgba(0,0,0,0.65) 0%,rgba(0,0,0,0) 100%);
            background: -ms-linear-gradient(top,  rgba(0,0,0,0.65) 0%,rgba(0,0,0,0) 100%);
            background: linear-gradient(to bottom,  rgba(0,0,0,0.15) 10%,rgba(0,0,0,0) 45%);

            text-transform: uppercase;

            ul { padding-top: 10px; }
            ul > li {
                margin: 0 20px;
                &:hover { border-top: 3px solid #7ad3f7 }
            }
        }
    }

    .navbar-brand {
        display: block;
        height: 51px;
        width: 209px;
        background: asset-url('logo.png') top left no-repeat;
        margin-bottom: 15px;
    }
}

我遇到的问题是试图让我的标记看起来像模型。如何让顶部边框(悬停时)出现在导航容器区域之外?

这是一种使用 :before 伪元素的方法:

Example Here

.masthead nav.collapse ul {
    padding-top: 10px;
    position: relative;
}
.masthead nav.collapse ul > li:before {
    content: '';
    position: absolute;
    width: 100%;
    background: #7ad3f7;
    height: 4px;
    top: -14px;
    display: none;
}
.masthead nav.collapse ul > li:hover:before {
    display: block;
}

你可以用边框和定位来完成。

向常规 li 元素添加透明边框(未悬停)。 将悬停颜色更改为 #7ad3f7。还要添加 position: relative;top: -15px; 以正确定位边框。

Fiddle

CSS:

.masthead nav.collapse ul > li {
    display: inline-block;
    border-top: 6px solid rgba(0,0,0,0);
    height: 51px;
    line-height: 51px;
    position: relative;
    top: -15px;
}
.masthead nav.collapse ul > li:hover {
    border-top: 6px solid #7ad3f7;
}

这个例子使用了 :after 伪选择器。我还在悬停效果上添加了一个简单的动画。这是因为我选择使用伪 class,而不是使用边框,这让设计变得更加困难。

li {
  display: inline-block;
  position: relative;
  padding: 10px;
  padding-left: 20px;
  padding-right: 20px;
  background: -moz-linear-gradient(top, rgba(0, 0, 0, 0.65) 0%, rgba(0, 0, 0, 0) 100%);
  background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, rgba(0, 0, 0, 0.65)), color-stop(100%, rgba(0, 0, 0, 0)));
  background: -webkit-linear-gradient(top, rgba(0, 0, 0, 0.65) 0%, rgba(0, 0, 0, 0) 100%);
  background: -o-linear-gradient(top, rgba(0, 0, 0, 0.65) 0%, rgba(0, 0, 0, 0) 100%);
  background: -ms-linear-gradient(top, rgba(0, 0, 0, 0.65) 0%, rgba(0, 0, 0, 0) 100%);
  background: linear-gradient(to bottom, rgba(0, 0, 0, 0.15) 10%, rgba(0, 0, 0, 0) 45%);
}
li:after {
  position: absolute;
  content: "";
  width: 100%;
  height: 5px;
  background: transparent;
  left: 0;
  top: 0;
  transition: all 0.5s;
}
li:hover:after {
  background: #7ad3f7;
  left: 0;
  top: -5px;
}
<ul>
  <li>one</li>
  <li>two</li>
  <li>three</li>
  <li>four</li>
</ul>