bootstrap hover移动所有链接

时间:2017-12-14 21:08:49

标签: html css twitter-bootstrap navigation hover

我正在玩bootstrap而且我遇到了一些问题。我创建了一个带有少量链接的导航,我想在我悬停链接时显示底部边框。我做到了,现在,当我悬停时,链接是"移动"到顶部,即使我没有给他们任何保证金或填充...... 这里是JSFiddle的例子,你需要调整结果窗口的大小才能看到整个菜单。

https://jsfiddle.net/zhzmz6fa/

                     

<div class="collapse navbar-collapse" id="navbarTogglerDemo02">
  <ul class="navbar-nav ml-auto mt-2 mt-lg-0">
    <li class="nav-item active">
      <a class="nav-link" href="#">Home<span class="sr-only">(current)</span></a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">O nas</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Opis</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Kontakt</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#">Archiwum</a>
    </li>
  </ul>
</div>

.navbar {
    font-family: sans-serif;
    background-color: orangered;

}

.navbar-toggler {
    border: none;
}

.nav-item {
    padding-left: 1.5rem;
}

.nav-link:hover,
.nav-link:active {
    border-bottom: 2px solid black;
    transition: border-bottom .5s;
}

2 个答案:

答案 0 :(得分:1)

边界计算宽度时额外计算。所以,如果你有

.nav-link {
  border-bottom: 2px solid transparent;
}

该元素的宽度为51px。

这也是您的代码所发生的事情。解决方案是始终给它一个边框,但让它透明。

<connectionStrings>
<add name="MyConnection" connectionString="Dsn=NGDEV" providerName="System.Data.SqlClient"/>
</connectionStrings>

答案 1 :(得分:0)

border的替代方案是伪元素。

根据需要设置样式,将其opacity设置为0.在:hover:active上将opacity更改为1.

&#13;
&#13;
.navbar {
  font-family: sans-serif;
  background-color: orangered;
}

.navbar-toggler {
  border: none;
}

.nav-item {
  padding-left: 1.5rem;
}

.nav-link {
  position: relative;
}

.nav-link:after {
  content: '';
  position: absolute;
  left: 0;
  right: 0;
  display: block;
  height: 2px;
  opacity: 0;
  background: black;
}

.nav-link:hover:after,
.nav-link:active:after {
  opacity: 1;
}
&#13;
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous">

<nav class="navbar navbar-expand-lg navbar-light fixed-top">
  <a href="#"><img src="http://via.placeholder.com/250x100" class="navbar-brand"></a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo02" aria-controls="navbarTogglerDemo02" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>

  <div class="collapse navbar-collapse" id="navbarTogglerDemo02">
    <ul class="navbar-nav ml-auto mt-2 mt-lg-0">
      <li class="nav-item active">
        <a class="nav-link" href="#">Home<span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">O nas</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Opis</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Kontakt</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Archiwum</a>
      </li>
    </ul>
  </div>
</nav>
&#13;
&#13;
&#13;