Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use Twitter Bootstraps .navbar-fix-to-top only on mobile devices?

I have Twitter Bootstrap 3 webpage with this navbar:

<div class="col-xs-12 col-md-10 col-md-offset-1">
    <nav class="navbar navbar-inverse" role="navigation">
      <div class="navbar-header">
        <button class="navbar-toggle" type="button" 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 href="#" class="navbar-brand">
          Title
        </a>
      </div>
      <div class="collapse navbar-collapse navbar-ex1-collapse">
        ...
      </div>
    </nav>
</div>

It looks good on desktop with some top margin. But is it possible to use the .navbar-fixed-to-top only on mobile devices? So that on a mobile device the navbar is always on top without top, left and right margin?

like image 614
sghgw Avatar asked Jan 30 '14 19:01

sghgw


People also ask

What is bootstrap navbar how it works for mobile and desktop screens?

The Bootstrap navbar can be dynamic in its positioning. By default, it is a block-level element that takes its positioning based on its placement in the HTML. With a few helper classes, you can place it either on the top or bottom of the page, or you can make it scroll statically with the page.

Which Bootstrap CSS class will you use to put the navbar at the top of the page Feel free to check out the bootstrap website?

Within this tag, we will call the class=”navbar navbar-default” which are the inbuilt classes of the bootstrap that give the top space of your web page to your navigation part and the default part is for the default view of that bar which is white in color.

What is bootstrap navbar in mobile web development?

Bootstrap is an HTML, CSS, and JavaScript framework. By using it, you can easily build mobile-first responsive websites. It provides you with many pre-set CSS styling for your web element, including navigation bars.


1 Answers

The simplest way is using only CSS. No javacript required. I used the styles for .navbar-top-fixed from the navbar.less, inside the .navbar-default. If you have a different classname just change it. If your navbar is heigher than 50px you should change the body margin-top padding also.

@media (max-width: 767px) {
    body {
        margin-top: 50px;
    }
    .navbar-default {
        position: fixed;
        z-index: 1030; //this value is from variables.less -> @zindex-navbar-fixed
        right: 0;
        left: 0;   
        border-radius: 0;
        top: 0;
        border-width: 0 0 1px;
    }      
}
like image 151
Valeriu Ciuca Avatar answered Oct 19 '22 11:10

Valeriu Ciuca