Menu de navigation fixé à gauche avec Bootstrap

Bootstrap propose des composants pratiques à mettre en œuvre facilement et sans créer une seule ligne de code. La création d'une barre de navigation nécessite juste les classes navbar et navbar-default.

Pour créer un menu de navigation fixé à gauche (compatible Bootstrap 3.3.7), nous allons créer une classe navbar-fixed-left :

/* navbar-fixed-left */

@media (max-width: 767px) {
  .navbar-text {
    margin-top: 15px;
    margin-bottom: 15px;
    padding-right: 15px;
    padding-left: 15px;
  }
}

.navbar-fixed-left {
    padding-top: 10px;
    width: 160px;
    position: fixed;
    border-radius: 0;
    height: 100%;
    z-index: 1029;
    /*
    font-family: verdana;
    font-size: 12px;
    font-weight: 200;
    */
}

.navbar-fixed-left .navbar-nav {
    float: left;
    margin: 0;
}

.navbar-fixed-left .navbar-nav > li {
    float: none;  /* Cancel default li float: left */
    width: 159px;
}

.navbar-fixed-left .navbar-nav > li > a {
    padding-top: 10px;
    padding-bottom: 10px;
    line-height: 20px;
} 

.navbar-fixed-left .navbar-nav > li > .dropdown-menu {
    margin-top: -30px;
    margin-left: 150px;
}

.navbar-fixed-left .navbar-nav .open .dropdown-menu {
    position: absolute;
    float: left;
    background-color: #fff;
    min-width: 160px;
    /*
    padding: 5px 0;
    margin: 2px 0 0;
    */

    border: 1px solid #ccc;
    border: 1px solid rgba(0, 0, 0, .15);
    border-width: 0 1px 1px;
    border-radius: 0 0 4px 4px;
    -webkit-box-shadow: 0 6px 12px rgba(0, 0, 0, .175);
            box-shadow: 0 6px 12px rgba(0, 0, 0, .175);
}



/* set default padding container */
.navbar-fixed-left + .container,
.navbar-fixed-left + .container-fluid {
    padding-left: 180px;
}


.app-main-content {
    padding-top: 0px;
}



@media (max-width: 767px) {
    .navbar-fixed-left {
        width: 60px;

    }

    .navbar-fixed-left + .container,
    .navbar-fixed-left + .container-fluid {
        padding-left: 80px;
    }

    .navbar-fixed-left .navbar-nav > li {
        width: 59px;
    }
    .navbar-fixed-left .navbar-nav > li > .dropdown-menu {
        margin-top: -30px;
        margin-left: 50px;
    }
}



/* navbar-fixed dropdown */

@media (max-width: 767px) {
  .navbar-fixed-left .navbar-nav .open .dropdown-menu > li > a {
    color: #333;
  }
  .navbar-fixed-left .navbar-nav .open .dropdown-menu > li > a:hover,
  .navbar-fixed-left .navbar-nav .open .dropdown-menu > li > a:focus {
    color: #262626;
    background-color: #f5f5f5;
  }
  .navbar-fixed-left .navbar-nav .open .dropdown-menu > .active > a,
  .navbar-fixed-left .navbar-nav .open .dropdown-menu > .active > a:hover,
  .navbar-fixed-left .navbar-nav .open .dropdown-menu > .active > a:focus {
    color: #fff;
    background-color: #337ab7;
  }
  .navbar-fixed-left .navbar-nav .open .dropdown-menu > .disabled > a,
  .navbar-fixed-left .navbar-nav .open .dropdown-menu > .disabled > a:hover,
  .navbar-fixed-left .navbar-nav .open .dropdown-menu > .disabled > a:focus {
    color: #777;
  }

}

 

Pour ajouter cette barre de menu sur une page, il faudra uniquement ajouter le code suivant :

<body role="document" >


        <nav id="navbar-custom" class="navbar navbar-default navbar-fixed-left">
            <div class="navbar-header">
                <a class="navbar-brand" href="#">Brand</a>
            </div>
            <!-- votre menu ICI -->
        </nav>



        <div class="container-fluid" >
            <div class="app-main-content">

                <!-- votre contenu ICI -->
                
            </div>
        </div>


</body>

 

Pour combiner une barre de menu fixée dans la partie supérieure et une barre de menu fixée à gauche 

<body role="document" >

        <!-- top navbar start -->
        <nav class="navbar navbar-default navbar-fixed-top">
        <div class="container-fluid">
            <div class="navbar-header">
                <a class="navbar-brand" href="javascript:void(0);">Mon site</a>
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>   
            </div>

            <div class="collapse navbar-collapse" id="myNavbar">
                <ul class="nav navbar-nav">
                    <li class="active"><a href="javascript:void(0);">Home</a></li>
                    <li><a href="javascript:void(0);">Page 1</a></li>
                    <li><a hhref="javascript:void(0);">Page 2</a></li>
                    <li class="dropdown">
                        <a class="dropdown-toggle" data-toggle="dropdown" href="#">Page 3
                        <span class="caret"></span></a>
                        <ul class="dropdown-menu">
                            <li><a href="javascript:void(0);">Page 3-1</a></li>
                            <li><a href="javascript:void(0);">Page 3-2</a></li>
                            <li><a href="javascript:void(0);">Page 3-3</a></li>
                        </ul>
                    </li>
                    <li><a href="javascript:void(0);">Page 4</a></li>
                </ul>
            </div>
        </div>
        </nav>



        <!-- left navbar start -->
        <nav id="navbar-custom" class="navbar navbar-default navbar-fixed-left">
            <div class="navbar-header">
                <!--<a class="navbar-brand" href="#">Brand</a>-->
            </div>
            <ul class="nav navbar-nav">
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" title="Page 1" ><i class="fa fa-cogs" aria-hidden="true"></i> <span class="hidden-xs">Page 1</span> <i class="fa fa-caret-right" aria-hidden="true"></i></a>
                    <ul class="dropdown-menu" role="menu">
                        <li><a href="javascript:void(0);" >Page 1-1</a></li>
                        <li><a href="javascript:void(0);" >Page 1-2</a></li>
                        <li><a href="javascript:void(0);" >Page 1-3</a></li>
                        <li><a href="javascript:void(0);" >Page 1-4</a></li>
                        <li><a href="javascript:void(0);" >Page 1-5</a></li>
                    </ul>
                </li>
                <li><a href="javascript:void(0);" title="Page 2" ><i class="fa fa-user-circle-o" aria-hidden="true"></i> <span class="hidden-xs">Page 2</span></a></li>
                <li><a href="javascript:void(0);" title="Page 3" ><i class="fa fa-tachometer" aria-hidden="true"></i> <span class="hidden-xs">Page 3</span></a></li>
                <li><a href="javascript:void(0);" title="Page 4" ><i class="fa fa-shopping-cart" aria-hidden="true"></i> <span class="hidden-xs">Page 4</span></a></li>
                <li><a href="javascript:void(0);" title="Page 5" ><i class="fa fa-history" aria-hidden="true"></i> <span class="hidden-xs">Page 5</span></a></li>

                
            </ul>
        </nav>



        <div class="container-fluid" >
            <div class="app-main-content">

                <!-- votre contenu ICI -->
                
            </div>
        </div>


</body>

 

 

Démonstration : https://ben0it.fr/test/bootstrap-navbar-fixed-left/

 

Étiquettes