Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access the styles of collapsed nav menu in bootstrap

I want to change the style of the menu elements in Bootstrap 3's nav menu when the menu is in collapsed mode.

For example:

<div class="navbar navbar-inverse" role="navigation">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-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="#">Project name</a>
    </div>

    <div class="collapse navbar-collapse">
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">Home</a></li>
        <li><a href="#about">About</a></li>
        <li><a href="#contact">Contact</a></li>
      </ul>
    </div><!--/.nav-collapse -->

  </div>
</div>

Is there a way to access the style properties of the "li > a" elements of the menu when it is different responsive states?

Thanks.

Edit: Some more clarity... So if you reduce the width of the screen and force the mobile menu to appear, the menu elements (nav buttons) have the same style. Let's say I wanted to have buttons with the default background (black here) in normal view, but give them a green background in mobile view, how do I do this?

like image 513
user3442612 Avatar asked May 27 '14 09:05

user3442612


People also ask

What is collapse navbar collapse in bootstrap?

Introduction to Bootstrap Collapse Navbar. Navigation bar (Navbar) is a header or index of the web application. It is always placed on top of the web page. The collapse in bootstrap is used for space-saving of large content. It is hidden and shows the content when the user wants.

What is a collapsed navbar?

The navbar-collapse refers to the menu that is in the mobile view with the navbar (contains links, and toggle-friendly content). They are in the bootstrap CSS (see here). See this for a full rundown of how the navbar and collapse work together.


2 Answers

You need to use CSS media queries for doing that. For twitter bootstrap 3, you can use the following media queries & write your CSS for specific view ports -

/* Large Devices, Wide Screens */
@media only screen and (max-width : 1200px) {

}

/* Medium Devices, Desktops */
@media only screen and (max-width : 992px) {

}

/* Small Devices, Tablets */
@media only screen and (max-width : 768px) {

}

/* Extra Small Devices, Phones */ 
@media only screen and (max-width : 480px) {

}

/* Custom, iPhone Retina */ 
@media only screen and (max-width : 320px) {

}

Eg. If in Tablets you want some specific CSS, you will do something like this -

/* Small Devices, Tablets */
@media only screen and (max-width : 768px) {
       .navbar-collapse li a { background: green; }
}
like image 83
Nitish Dhar Avatar answered Nov 15 '22 12:11

Nitish Dhar


With 3.3.5, I noticed that the navbar target has one more class when it is collapsed. I could style my collapsed item like this :

.navbar.in ul li {
    /* style goes here */
}

DEMO(click the mobile icon)

like image 33
David Avatar answered Nov 15 '22 13:11

David