Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

a:hover does not work, with bootstrap

So for some reason, my hover CSS is not working. I am using bootstrap with some of their css. Is there something wrong with my code, or is it because bootstrap's code is overriding mine's?

HTML

<nav class="navbar navbar-default navbar-fixed-top">
  <div class="collapse navbar-collapse">
    <ul class="nav navbar-nav" role="tablist">
        <li class="active"><a href="#">Home</a></li>
        <li><a href="#">HTML</a></li>
        <li><a href="#">CSS</a></li>
        <li><a href="#">About</a></li>        
    </ul>
  </div>
</nav>

CSS

ul>li>a {
    color: black;
    padding: 15px;
}

ul>li>a:hover {
    background-color: #B0B0B0;
    color: red;
}
like image 886
kot09 Avatar asked Mar 12 '15 23:03

kot09


People also ask

Why Hover is not working in Bootstrap?

It's because the default Bootstrap styling is more specific. Save this answer. Show activity on this post. Without seeing exactly whats going on, the issue you're having may be related to not specifying a class on your unordered list.

Why the hovering is not working?

Wrong CSS Format Also, if you incorrectly write a selector and wish to apply a hover effect on it, you will notice that the styling does not work as expected in any browser. To solve the issue, you need to go over the CSS hover code to establish if you use the right selector.

Does hover only work on links?

Definition and Usage The :hover selector is used to select elements when you mouse over them. Tip: The :hover selector can be used on all elements, not only on links.


2 Answers

It's because the default Bootstrap styling is more specific.

These are the selectors that you need to override:

.navbar-default .navbar-nav>li>a:hover,
.navbar-default .navbar-nav>li>a:focus {
  color: #333;
  background-color: transparent;
}

Therefore you could use:

Working Example Here

.navbar-default .navbar-nav>li>a:hover {
  background-color: #B0B0B0;
  color: red;
}
like image 101
Josh Crozier Avatar answered Sep 27 '22 20:09

Josh Crozier


Without seeing exactly whats going on, the issue you're having may be related to not specifying a class on your unordered list.

This may fix the problem:

ul.navbar-nav > li > a {
color:black;
padding: 15px;
}

ul.navbar-nav > li > a:hover {
    background-color: #B0B0B0;
    color:red;
}
like image 45
erier Avatar answered Sep 27 '22 18:09

erier