将底部边框添加到悬停/焦点/活动bootstrap wordpress导航菜单项

时间:2016-06-15 19:56:50

标签: css wordpress twitter-bootstrap menu

我想在我的菜单导航按钮上添加边框底部,该按钮悬停在上面,或者已选中,或者是当前菜单项。我不想只是想添加一个“文字装饰:下划线”,我想有一个3px高的边框,只有它上面的导航项的宽度,我找不到适用的例子 - 任何帮助非常感谢:

我正在使用wordpress动态菜单:

<div class="collapse navbar-collapse navbar-ex1-collapse">
   <ul id="menu-main-menu" class="nav navbar-nav">
     <li id="menu-item-32" class="menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-6 current_page_item menu-item-32 active">
     <li id="menu-item-31" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-31">
     <li id="menu-item-30" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-30">
     <li id="menu-item-29" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-29">
     <li id="menu-item-26" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-26 dropdown">
     <li id="menu-item-25" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-25">
     <li id="menu-item-498" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-498">
     <li id="menu-item-499" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-499">
</ul>
</div>

1 个答案:

答案 0 :(得分:2)

如何使边框的宽度等于其上方文本的宽度

您可以将每个链接的文字包装到<span></span>并将the box-shadow property应用于此span

在您的网站上

screenshot

UPD。我添加了下拉菜单的代码。

我根据您的网站制作了a Minimal, Complete, and Verifiable example。您必须将每个链接的文本包装到<span></span>并添加此CSS以解决您的问题:

.dropdown-toggle {
  margin-top: 0;
}
#menu-main-menu .current-menu-item a,
.nav > li > a:focus,
.nav > li > a:hover {
  box-shadow: none;
}
@media (min-width: 768px) {
  .navbar-nav > li > a {
    padding: 0 10px;
  }
  .navbar-nav > li > a > .fa-fw {
    width: auto;
  }
  .navbar-nav > li > a > .fa-fw,
  .navbar-nav > li > a > span {
    display: inline-block;
    padding: 15px 0;
  }
  .navbar-nav > li > a:hover > span,
  .navbar-nav > li > a:focus > span,
  .navbar-nav > .active > a  > span {
    box-shadow: inset 0px -3px 0px #f2ab00;
  }
}

请检查结果:https://jsfiddle.net/glebkema/jacL6zme/

/******** MINIMAL, COMPLETE AND VERIFIABLE EXAMPLE ********/
@import url('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css');
@import url('https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css');
@import url('https://fonts.googleapis.com/css?family=Poppins:300,500,600,700');

body {
  background-color: #3b3e4d;
  color: #fff;
  font-family: "Poppins",sans-serif;
  font-size: 16px;
}

/* fragment of your bootstrap.min.css */
a {
  background-color:transparent !important;
}
#menu-main-menu .current-menu-item a {
  color: #f2ab00;
  box-shadow: inset 0px -3px 0px #f2ab00;
  display: inline-block;
}
.navbar-fixed-top {
  padding-left: 20px;
  padding-right: 20px;
  padding-bottom: 10px;
}
.navbar-nav>li>a {
  line-height: 20px;
}
.nav>li>a {
  position: relative;
  display: block;
  padding: 10px 15px;
  color: #fff;
}
.nav>li>a:focus, .nav>li>a:hover {
  text-decoration: none;
  color: #f2ab00;
  box-shadow: inset 0px -3px 0px #f2ab00;
  display: inline-block;
}

/* fragment of your style.css */
a {
  text-decoration: none;
  -webkit-transition: all 0.3s;
  -moz-transition: all 0.3s;
  -o-transition: all 0.3s;
  transition: all 0.3s;
}
.fa {
  font: 28px/1 FontAwesome;
}
.navbar-nav > li > a {
  font-size:12px;
  font-weight:500;
  font-style: normal;
  padding-top: 10px;
  padding-bottom: 10px;
  letter-spacing: 1px;
}
.navbar-default .navbar-toggle:hover,
.navbar-default .navbar-toggle:focus {
  background-color: transparent;
}
.nav.navbar-nav {
  float: right;
}
.nav>li>a {
  text-transform: uppercase;
}
#menu-item-498 a, #menu-item-499 a {
  color: #f2ab00;
  font-family: FontAwesome;
  margin-top: -5px;
}
#menu-item-498 a:hover, #menu-item-499 a:hover {
  color: #fff;
}
@media (min-width: 768px) {
  ul.nav li.dropdown:hover > ul.dropdown-menu {
    display: block;
  }
}
.navbar-nav > li > .dropdown-menu {
  padding: 0;
}
.dropdown-menu, 
.dropdown-menu > .active > a, 
.dropdown-menu > .active > a:hover, 
.dropdown-menu > .active > a:focus {
    border: none;
    color: #f2ab00;
    font-weight: 600;
}
.dropdown-menu {
    font-size: 12px;
    letter-spacing: 1px;
}
.dropdown-menu > li > a:hover,
.dropdown-menu > li > a:focus,
.navbar-default .navbar-nav .open .dropdown-menu > li > a:hover,
.navbar-default .navbar-nav .open .dropdown-menu > li > a:focus {
  color: #f2ab00;
  background-color: #252839;
  text-transform: uppercase;
}
@media (max-width: 767px) {
  .navbar-default .navbar-nav .open .dropdown-menu>.active>a,
  .navbar-default .navbar-nav .open .dropdown-menu>.active>a:focus,
  .navbar-default .navbar-nav .open .dropdown-menu>.active>a:hover {
    color: #f2ab00;
    background-color: #252839;
  }
}
.dropdown-toggle {
    margin-top: 15px;
}

/* fragment of your font-awesome/css/font-awesome.min.css */
.fa-fw {
  width: .028571429em;
  text-align: center;
}

/* fragment of your page's styles */
.navbar-default .navbar-nav .open .dropdown-menu > li > a, 
.dropdown-menu > li > a {
    color: #ffffff;
}
.dropdown-menu {
    background-color: #252839;
}
.dropdown-menu > li > a {
    padding: 10px 15px;
    color: #fff;
    text-transform: uppercase;
}

/********** THE HEART OF THE MATTER **********/
.dropdown-toggle {
  margin-top: 0;
}
#menu-main-menu .current-menu-item a,
.nav > li > a:focus,
.nav > li > a:hover {
  box-shadow: none;
}
@media (min-width: 768px) {
  .navbar-nav > li > a {
    padding: 0 10px;
  }
  .navbar-nav > li > a > .fa-fw {
    width: auto;
  }
  .navbar-nav > li > a > .fa-fw,
  .navbar-nav > li > a > span {
    display: inline-block;
    padding: 15px 0;
  }
  .navbar-nav > li > a:hover > span,
  .navbar-nav > li > a:focus > span,
  .navbar-nav > .active > a  > span {
    box-shadow: inset 0px -3px 0px #f2ab00;
  }
}
<nav class="navbar-fixed-top navbar" role="navigation">

  <ul id="menu-main-menu" class="nav navbar-nav">
    <li id="menu-item-32" class="menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-6 current_page_item menu-item-32 active"><a href="#"><span>Link</span></a></li>
    <li id="menu-item-31" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-31"><a href="#"><span>Link</span></a></li>
    <li id="menu-item-30" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-30"><a href="#"><span>Link</span></a></li>
    <li id="menu-item-29" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-29"><a href="#"><span>Link</span></a></li>
    <li id="menu-item-26" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-26 dropdown"><a href="#" data-toggle="dropdown" class="dropdown-toggle"><span>Dropdown</span></a>
      <ul role="menu" class="dropdown-menu">
        <li id="menu-item-28" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-28"><a href="#"><span>Link</span></a></li>
      </ul>
    </li>
    <li id="menu-item-25" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-25"><a href="#"><span>Link</span></a></li>
    <li id="menu-item-498" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-498"><a title="facebook" target="_blank" href="#"><i class="fa fa-facebook fa-fw"></i></a></li>
    <li id="menu-item-499" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-499"><a title="twitter" target="_blank" href="#"><i class="fa fa-twitter fa-fw"></i></a></li>  
  </ul>

</nav>

对于Bootstrap导航栏

screenshot

我使用了span:first-child选择器,因为某些链接可能包含<span class="sr-only"></span>

@import url('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css');

body {
  background: #9d9d9d;
}
.navbar-nav > li > a {
  color: #fff !important;
  font-transform: uppercase;
  font-weight: bold;
}
.navbar-nav > li > a:hover > span:first-child,
.navbar-nav > li > a:focus > span:first-child,
.navbar-nav > .active > a  > span:first-child {
  color: #f2ab00 !important;
}

@media (min-width: 768px) {
  .navbar {
    border: none;
  }
  .navbar-nav > li > a {
    padding: 0 15px;
  }
  .navbar-nav > li > a > span:first-child {
    display: inline-block;
    padding: 15px 0;
  }
  .navbar-nav > li > a:hover > span:first-child,
  .navbar-nav > li > a:focus > span:first-child,
  .navbar-nav > .active > a  > span:first-child {
    box-shadow: inset 0px -3px 0px #f2ab00;
  }
}
<nav class="navbar navbar-inverse">
  <div class="container-fluid">
    <div class="navbar-header">
      <button type="button" class="btn navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#"><span>Brand</span></a>
    </div>

    <div class="collapse navbar-collapse navbar-ex1-collapse">
      <ul class="nav navbar-nav">
        <li class="active"><a href="#"><span>Link</span><span class="sr-only">(current)</span></a></li>
        <li><a href="#"><span>Link</span></a></li>
      </ul>
      <ul class="nav navbar-nav navbar-right">
        <li><a href="#"><span>Link</span></a></li>
        <li><a href="#"><span>Link</span></a></li>
      </ul>
    </div>
  </div>
</nav>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>