Get access to an awe-inspiring collection Of 87 Beautiful Wordpress themes for the price of one Get it now Ad

Bootstrap Sidebar

Published: 25.4.2017 | Last update: 14.6.2017

Today I’d like to show you how to create a collapsible HTML sidebar navigation using Bootstrap 3 with some CSS and jQuery.

Since Bootstrap 3 or even their latest Alpha version 4.0.0 doesn't provide one, we will build four separate solutions, each of them with slightly different features. In each part of the tutorial, I will guide you step by step through all the necessary steps in HTML, CSS and JavaScript.

Bootstrap Sidebar

We're going to build 5 sidebars like this one.

Here is what we're going to build:

Download Sources

Demo #1 Demo #2 Demo #3 Demo #4 Demo - Bonus section

Basic files

Before we dig into coding, we should first set up our workflow with the necessary files.

In all four cases we'll go through today, we will need Bootstrap CSS and JS files, jQuery library, and our custom stylesheet.

So, our startup markup should be as following:

<html !DOCTYPE>
<head>
    <title>Bootstrap 3 Collapsible Sidebar</title>
    <!-- Bootstrap CSS CDN -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <!-- Our Custom CSS -->
    <link rel="stylesheet" href="css/custom.css">
</head>
<body>


    <!-- jQuery CDN -->
    <script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
    <!-- Bootstrap Js CDN -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</body>
</html>

1. Static collapsible sidebar

In this part, we are going to build a simple Bootstrap sidebar that vertically scrolls along with the page. 

 

HTML

Basic document structure

We'll wrap everything in .wrapper box to make advantage of CSS flex property.

In this case, we'll give .wrapper the CSS property align-items: stretch.

By doing this, the sidebar will take the height of the page content. As the content increases, sidebar height dynamically increases.

<div class="wrapper">

    <!-- Sidebar -->
    <nav id="sidebar">
        ...
    </nav>

    <!-- Page Content -->
    <div id="content">
        <!-- We'll fill this with dummy content -->
    </div>

</div>       

Sidebar and its content

Now let's fill our sidebar with some content. Here I'll place there a sidebar navigation menu that will contain demo navigation links. Some of them will also contain drop-down menus.

<div class="wrapper">

    <nav id="sidebar">
        <!-- Sidebar Header -->
        <div class="sidebar-header">
            <h3>Collapsible Sidebar</div>
        </div>

        <!-- Sidebar Links -->
        <ul class="list-unstyled components">
            <li class="active"><a href="#">Home</a></li>
            <li><a href="#">About</a></li>

            <li><!-- Link with dropdown items -->
                <a href="#homeSubmenu" data-toggle="collapse" aria-expanded="false">Pages</a>
                <ul class="collapse list-unstyled" id="homeSubmenu">
                    <li><a href="#">Page</a></li>
                    <li><a href="#">Page</a></li>
                    <li><a href="#">Page</a></li>
                </ul>

            <li><a href="#">Portfolio</a></li>
            <li><a href="#">Contact</a></li>
        </ul>
    </nav>

</div>

We also added .active class to the first item to mark that it is the current page. 

Making dropdowns work

To make a dropdown menu collapsible, data-toggle="collapse" should be added to the link holding the dropdown. 

The link's href attribute must contain the id of the dropdown menu preceded by a hash. In this case, I used #homeSubmenu for this approach.

The dropdown menu itself also should have .collapse class too.

<a href="#homeSubmenu" data-toggle="collapse" aria-expanded="false">Pages</a>
            

Be sure to add aria-expanded to the dropdown's control element <a>.

This attribute explicitly defines the current state of the collapsible element to screen readers and similar assistive technologies. If the collapsible element is closed by default, it should have a value of aria-expanded="false". If you've set the collapsible element to be open by default using an .in class, set aria-expanded="true" on the control instead.

By clicking the anchor, the dropdown will slide up or slide down according to the aria-expanded="" value.

Sidebar toggle button

Now it's time to add the sidebar toggle button, this button will handle opening/closing the sidebar.

We place it outside the sidebar itself. It doesn't matter where it is located as long as it's outside the sidebar, i.e. visible all the time.

<div id="content">
    <button type="button" id="sidebarCollapse" class="btn btn-info navbar-btn">
        <i class="glyphicon glyphicon-align-left"></i>
        Toggle Sidebar
    </button>
<div>

CSS

Now, let's finally apply some styling to our sidebar.

The most important thing for this approach is to use the flex property for the .wrapper div. As I mentioned above, stretch value will equalize both page content and sidebar height.

.wrapper {
    display: flex;
    align-items: stretch;
}          

After that, we'll give the sidebar a fixed width of 250px.  

As a result of using flexbox properties, page content #content will take the remaining width of .wrapper. (as long as we don't use flex-wrap: wrap property) 

Then, we'll use sidebar's width to push the element out of the screen when we don't need it. This behaviour will be applied when the sidebar has an .active class. This class has a negative margin left value that equals to the sidebar width. This will be all for the moment and we will work more with this class We'll use this class later on in the JavaScript part.

.wrapper {
    display: flex;
    align-items: stretch;
}

#sidebar {
    min-width: 250px;
    max-width: 250px;
}

#sidebar.active {
    margin-left: -250px;
}

As we do not know if the content will vertically fill the entire screen, we will set the minimum height of the sidebar to 100vh. vh is an unit thath refers to the viewport height. 

This means that this rule makes the initial height of the sidebar at least equal to the screen height. Also, its height will increase when the page content would increase.

#sidebar {
    min-width: 250px;
    max-width: 250px;
    min-height: 100vh;
}

Styling dropdowns

We will also style the expandable dropdown items in the navigation based on their aria-expanded value. We'll use ::before pseudo elements to add an arrow for these items and depending on its aria-expanded value, the arrow direction will change from up to down and vice versa.

a[data-toggle="collapse"] {
    position: relative;
}

a[aria-expanded="false"]::before, a[aria-expanded="true"]::before {
    content: '\e259';
    display: block;
    position: absolute;
    right: 20px;
    font-family: 'Glyphicons Halflings';
    font-size: 0.6em;
}

a[aria-expanded="true"]::before {
    content: '\e260';
}

Media query

We need a slightly different behaviour for the sidebar on the smaller screens. Instead of appearing by default, it'll be hidden and appear only after clicking the toggle button. Like this, we will save valuable space for your content and show the navigation to the user only when needed.

Basically, what we need to do here is to reverse the .active style:

@media (max-width: 768px) {
    #sidebar {
        margin-left: -250px;
    }
    #sidebar.active {
        margin-left: 0;
    }
}

Additional styling

This would be all regarding the necessary CSS styling that is needed to make your sidebar work. 

To give it a fancy look, I have styled it a bit more. It is all included in the download package, so I will just quickly mention it here without any further comments. If something would be not clear thogh, let me know in the comments.

/*
    ADDITIONAL DEMO STYLE, NOT IMPORTANT TO MAKE THINGS WORK BUT TO MAKE IT A BIT NICER :)
*/
@import "https://fonts.googleapis.com/css?family=Poppins:300,400,500,600,700";


body {
    font-family: 'Poppins', sans-serif;
    background: #fafafa;
}

p {
    font-family: 'Poppins', sans-serif;
    font-size: 1.1em;
    font-weight: 300;
    line-height: 1.7em;
    color: #999;
}

a, a:hover, a:focus {
    color: inherit;
    text-decoration: none;
    transition: all 0.3s;
}

#sidebar {
    /* don't forget to add all the previously mentioned styles here too */
    background: #7386D5;
    color: #fff;
    transition: all 0.3s;
}

#sidebar .sidebar-header {
    padding: 20px;
    background: #6d7fcc;
}

#sidebar ul.components {
    padding: 20px 0;
    border-bottom: 1px solid #47748b;
}

#sidebar ul p {
    color: #fff;
    padding: 10px;
}

#sidebar ul li a {
    padding: 10px;
    font-size: 1.1em;
    display: block;
}
#sidebar ul li a:hover {
    color: #7386D5;
    background: #fff;
}

#sidebar ul li.active > a, a[aria-expanded="true"] {
    color: #fff;
    background: #6d7fcc;
}
ul ul a {
    font-size: 0.9em !important;
    padding-left: 30px !important;
    background: #6d7fcc;
}

Javascript

The idea here is to toggle the .active class to the sidebar on clicking the toggle button.

By default, the sidebar will appear, i.e. it hasn't got .active class yet. After clicking the toggle button, the sidebar will be given an .active class, and pushed out from the screen. The page content will take the full-screen width too.

Reclicking the toggle button will remove the .active class and the sidebar reappears again. And so on. Let's have a look at the code:

$(document).ready(function () {

    $('#sidebarCollapse').on('click', function () {
        $('#sidebar').toggleClass('active');
    });

});

Let's have a look at what we've built View Demo

2. Fixed positioned scrollable sidebar

In this part, we'll make a similar sidebar but it will be fixed. This means that it won't scroll along with the page but it will stay fixed at the same place. This will apply only to larger devices with the width of 768px+.

HTML

This approach is similar to the previous sidebar navigation, therefore we can reuse the previous code.

Our markup should be as following:

<div class="wrapper">

        <nav id="sidebar">
            <!-- Sidebar Header -->
            <div class="sidebar-header">
                <h3>Collapsible Sidebar</div>
            </div>

            <!-- Sidebar Links -->
            <ul class="list-unstyled components">
                <li class="active"><a href="#">Home</a></li>
                <li><a href="#">About</a></li>
                <li><!-- Link with dropdown items -->
                    <a href="#homeSubmenu" data-toggle="collapse" aria-expanded="false">Pages</a>
                    <ul class="collapse list-unstyled" id="homeSubmenu">
                        <li><a href="#">Page</a></li>
                        <li><a href="#">Page</a></li>
                        <li><a href="#">Page</a></li>
                    </ul>
                <li><a href="#">Portfolio</a></li>
                <li><a href="#">Contact</a></li>
            </ul>
        </nav>

        <div id="content">
            <button type="button" id="sidebarCollapse" class="btn btn-info navbar-btn">
                <i class="glyphicon glyphicon-align-left"></i>
                Toggle Sidebar
            </button>
        <div>

</div>

CSS

As we need a fixed height sidebar, we'll get rid of align-items property that stretched items vertically.

However the content extends, the sidebar still will take entire viewport height. For this, we'll replace min-height: 100vh with height: 100vh .

.wrapper {
        display: flex;
}

#sidebar {
        min-width: 250px;
        max-width: 250px;
        height: 100vh;
}

At this point, we've finished all the CSS modifications.

We'll add two extra tricks in the JavaScript, let's have a look at them now.

JavaScript

The first trick is that I'll replace the default browser scrollbar with a custom one. We will need the scrollbar there just in case navbar would be too high to fit in the viewport.

For this purpose, I'll use jQuery Nice scroll plugin. Let's first add its JS file to our HTML file.

        <!-- jQuery CDN -->
        <script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
        <!-- Bootstrap Js CDN -->
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
        <!-- Nice Scroll Js CDN -->
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.nicescroll/3.6.8-fix/jquery.nicescroll.min.js"></script>
    </body>
</html>

Now, let's initialize the plugin and use some of its options

$(document).ready(function () {

    $('#sidebar').niceScroll({
        cursorcolor: '#53619d', // Changing the scrollbar color
        cursorwidth: 4, // Changing the scrollbar width
        cursorborder: 'none', // Rempving the scrollbar border
    });

    $('#sidebarCollapse').on('click', function () {
        $('#sidebar').toggleClass('active');
    });

});

The second trick, I'll make the open dropdowns to be closed while the sidebar is collapsed out. The reason for this is to keep things consistent and to open a navbar with closed dropdowns every time.

$(document).ready(function () {

    $('#sidebar').niceScroll({
        cursorcolor: '#53619d', // Changing the scrollbar color
        cursorwidth: 4, // Changing the scrollbar width
        cursorborder: 'none', // Rempving the scrollbar border
    });

    $('#sidebarCollapse').on('click', function () {
        // open or close navbar
        $('#sidebar').toggleClass('active');
        // close dropdowns
        $('.collapse.in').toggleClass('in');
        // and also adjust aria-expanded attributes we use for the open/closed arrows
        // in our CSS
        $('a[aria-expanded=true]').attr('aria-expanded', 'false');
    });

});

To clarify what we have done here. The class .in is responsible for opening the dropdown menu. If it has one, it's open, if not, it's closed. We told javascript that if this class is present, please remove it. Also, please change the aria-expanded value from true to false so the arrow will return to its logical direction.

This makes our fixed sidebar navigation complete. Let's have a look at what we've built View Demo

3. Fixed scrollable sidebar with a content overlay

In this approach, we'll make a sidebar similar to the one that Google uses for their navbars on tablets and mobile phones in Material design. The sidebar will cover the left part of the page content when it's open and the rest of the content will be covered by dark transparent overlay.

HTML

We'll use the same markup above and add a #dismiss button to the sidebar. This button will be responsible for the closing of the sidebar when it's open.

And since we'll apply a transparent overlay, let's insert an .overlay div at the very bottom of our page for that purpose too.

<div class="wrapper">

        <nav id="sidebar">
            <!-- Close Sidebar Button -->
            <div id="dismiss">
                <i class="glyphicon glyphicon-arrow-left"></i>
            </div>

            <!-- Sidebar Header -->
            <div class="sidebar-header">
                <h3>Collapsible Sidebar</div>
            </div>

            <!-- Sidebar Links -->
            <ul class="list-unstyled components">
                <li class="active"><a href="#">Home</a></li>
                <li><a href="#">About</a></li>
                <li><!-- Link with dropdown items -->
                    <a href="#homeSubmenu" data-toggle="collapse" aria-expanded="false">Pages</a>
                    <ul class="collapse list-unstyled" id="homeSubmenu">
                        <li><a href="#">Page</a></li>
                        <li><a href="#">Page</a></li>
                        <li><a href="#">Page</a></li>
                    </ul>
                <li><a href="#">Portfolio</a></li>
                <li><a href="#">Contact</a></li>
            </ul>
        </nav>

        <div id="content">
            <button type="button" id="sidebarCollapse" class="btn btn-info navbar-btn">
                <i class="glyphicon glyphicon-align-left"></i>
                Toggle Sidebar
            </button>
        <div>

        <div class="overlay"><div>
</div>

CSS

In this approach, we'll get rid of the flex property and we won't even need .wrapper anymore. We'll use position property instead. 

The sidebar will have a fixed positioning at the left part of the screen and content will take the full screen all the time.

By default, the sidebar will be hidden. When the toggle button is clicked, both .overlay and the sidebar will appear above the content.

Let's imagine it as layers, the page content will be the back layer. .overlay will be the middle layer with a transparent black colour. The overlay will cover the content of the page to allow user's eye easily focus on the sidebar itself. Finally, the sidebar will be the front layer.

To achieve this layering behaviour, we will simply use z-index property.

As mentioned before, we will be also adding a  #dismiss button to the sidebar. It  will be positioned absolutely at its top right part.

.wrapper {
    display: block;
}

#sidebar {
    min-width: 250px;
    max-width: 250px;
    height: 100vh;
    position: fixed;
    top: 0;
    left: 0;
    /* top layer */
    z-index: 9999;
}

.overlay {
    /* full screen */
    width: 100vw;
    height: 100vh;
    /* transparent black */
    background: rgba(0, 0, 0, 0.8);
    position: fixed;
    top: 0;
    left: 0;
    display: none;
    /* middle layer, i.e. appears below the sidebar */
    z-index: 9998;
}

#dismiss {
    width: 35px;
    height: 35px;
    position: absolute;
    /* top right corner of the sidebar */
    top: 10px;
    right: 10px;
}

Javascript

In two previous sidebars, the toggle button functionality was to open/close the sidebar. Here, we already have a close button inserted in our sidebar, so the toggle button function is only to open the sidebar.

To clarify the mechanism, by clicking the toggle button both overlay and sidebar appear, and by clicking the sidebar close button, both overlay and sidebar disappear.

$(document).ready(function () {

    $('#sidebar').niceScroll({
        cursorcolor: '#53619d', // Changing the scrollbar color
        cursorwidth: 4, // Changing the scrollbar width
        cursorborder: 'none', // Rempving the scrollbar border
    });

    // when opening the sidebar
    $('#sidebarCollapse').on('click', function () {
        // open sidebar
        $('#sidebar').addClass('active');
        // fade in the overlay
        $('.overlay').fadeIn();
        $('.collapse.in').toggleClass('in');
        $('a[aria-expanded=true]').attr('aria-expanded', 'false');
    });

   
    // if dismiss or overlay was clicked
    $('#dismiss, .overlay').on('click', function () {
      // hide the sidebar
      $('#sidebar').removeClass('active');
      // fade out the overlay
      $('.overlay').fadeOut();
    });
});

This is it. Time to check the demo. View Demo

4. Partially collapsing static sidebar

In this example, we will, instead of building a sidebar that collapses entirely, build a Partially collapsing sidebar. The sidebar will convert into a compressed version after toggle button click.

Let's use the first approach's markup as a starting point.

HTML

To further enhance it, we will insert an icon into the navigation links.

We will use Glyphicons that are a part of the Bootstrap distribution.

<div class="wrapper">

        <nav id="sidebar">
            <!-- Sidebar Header -->
            <div class="sidebar-header">
                <h3>Collapsible Sidebar</div>
                <strong>BS</strong>
            </div>

            <!-- Sidebar Links -->
            <ul class="list-unstyled components">
                <li class="active">
                    <a href="#homeSubmenu" data-toggle="collapse" aria-expanded="false">
                        <i class="glyphicon glyphicon-home"></i>
                        Home
                    </a>
                </li>
                <li>
                    <a href="#">
                        <i class="glyphicon glyphicon-briefcase"></i>
                        About
                    </a>
                    <a href="#pageSubmenu" data-toggle="collapse" aria-expanded="false">
                        <i class="glyphicon glyphicon-duplicate"></i>
                        Pages
                    </a>
                    <ul class="collapse list-unstyled" id="pageSubmenu">
                        <li><a href="#">Page 1</a></li>
                        <li><a href="#">Page 2</a></li>
                        <li><a href="#">Page 3</a></li>
                    </ul>
                </li>
                <li>
                    <a href="#">
                        <i class="glyphicon glyphicon-link"></i>
                        Portfolio
                    </a>
                </li>
                <li>
                    <a href="#">
                        <i class="glyphicon glyphicon-send"></i>
                        Contact
                    </a>
                </li>
            </ul>
        </nav>

        <div id="content">
            <button type="button" id="sidebarCollapse" class="btn btn-info navbar-btn">
                <i class="glyphicon glyphicon-align-left"></i>
                Toggle Sidebar
            </button>
        <div>

</div>

CSS

Instead of pushing the sidebar entirely out of the screen, we'll just shrink its width, and restyle its content to fit this new width. The styles of the compressed version will be added to the class .active

For example, we will downsize the font size of the anchors' text, align it to centre and make it render below the icon. Also, we will move the arrow to the very bottom of every anchor, or adjust the padding around the dropdown links.

The code will be as follows:

/* Shrinking the sidebar from 250px to 80px and center aligining its content*/
#sidebar.active {
    min-width: 80px;
    max-width: 80px;
    text-align: center;
}

/* Toggling the sidebar header content, hide the big heading [h3] and showing the small heading [strong] and vice versa*/
#sidebar .sidebar-header strong {
    display: none;
}
#sidebar.active .sidebar-header h3 {
    display: none;
}
#sidebar.active .sidebar-header strong {
    display: block;
}

#sidebar ul li a {
    text-align: left;
}

#sidebar.active ul li a {
    padding: 20px 10px;
    text-align: center;
    font-size: 0.85em;
}

#sidebar.active ul li a i {
    margin-right:  0;
    display: block;
    font-size: 1.8em;
    margin-bottom: 5px;
}

/* Same dropdown links padding*/
#sidebar.active ul ul a {
    padding: 10px !important;
}

/* Changing the arrow position to bottom center position, 
   translateX(50%) works with right: 50% 
   to accurately  center the arrow */
#sidebar.active a[aria-expanded="false"]::before, #sidebar.active a[aria-expanded="true"]::before {
    top: auto;
    bottom: 5px;
    right: 50%;
    transform: translateX(50%);
}

Media queries

On smaller screens, we'll keep the compressed version as a default active state of the sidebar. I.e., the uncompressed version will not be used on mobiles at all and the compressed version will become visible after clicking the toggle button.

To achieve this, we can only copy the styles from .active to our  mobile media query @media (max-width: 768px)  and add a margin-left functionality to it.

For mobiles, #sidebar.active sidebar will have a negative left margin (it will be off the canvas) and the #sidebar without the .active class will have margin-left set to 0.

@media (max-width: 768px) {
    /* 80px and its content aligned to centre. Pushing it off the screen with the
       negative left margin
    */
    #sidebar.active {
        min-width: 80px;
        max-width: 80px;
        text-align: center;
        margin-left: -80px !important;
    }


    /* Reappearing the sidebar on toggle button click */
    #sidebar {
        margin-left: 0; 
    }


    /* Toggling the sidebar header content, 
       hide the big heading [h3] and showing the small heading [strong] and vice versa
    */
    #sidebar .sidebar-header strong {
        display: none;
    }
    #sidebar.active .sidebar-header h3 {
        display: none;
    }
    #sidebar.active .sidebar-header strong {
        display: block;
    }

    /* Downsize the navigation links font size */
    #sidebar.active ul li a {
        padding: 20px 10px;
        font-size: 0.85em;
    }

    #sidebar.active ul li a i {
        margin-right:  0;
        display: block;
        font-size: 1.8em;
        margin-bottom: 5px;
    }

    /* Adjust the dropdown links padding*/
    #sidebar.active ul ul a {
        padding: 10px !important;
    }

    /* Changing the arrow position to bottom center position, 
      translateX(50%) works with right: 50% 
      to accurately  center the arrow */
    #sidebar.active a[aria-expanded="false"]::before, #sidebar.active a[aria-expanded="true"]::before {
        top: auto;
        bottom: 5px;
        right: 50%;
        transform: translateX(50%);
    }
}

JavaScript

We will not include any additional lines of JavaScript and we'll just use the same function did use in the first sidebar.

$(document).ready(function () {

    $('#sidebarCollapse').on('click', function () {
        $('#sidebar').toggleClass('active');
    });

});

That will be all for now. We should have a nicely working partially collapsing sidebar.

Let's have a look at the results.

View Demo

Further Improvements

There is always room for some further improvements and tweaks.

As a bonus, I would like to show you how to prepare an animated hamburger menu for the toggle button, and also how to add eye-catching animation for the opening and closing of the sidebar panel.

HTML

Again, I will use the simple sidebar from the first example as a base.

But instead of using a Glyphicon for the toggle button, let's place three spans there instead. Each one will represent a bar of the hamburger menu and we will style them with CSS later on.

<div class="wrapper">

        <nav id="sidebar">
            <!-- Sidebar Header -->
            <div class="sidebar-header">
                <h3>Collapsible Sidebar</div>
            </div>

            <!-- Sidebar Links -->
            <ul class="list-unstyled components">
                <li class="active"><a href="#">Home</a></li>
                <li><a href="#">About</a></li>
                <li><!-- Link with dropdown items -->
                    <a href="#homeSubmenu" data-toggle="collapse" aria-expanded="false">Pages</a>
                    <ul class="collapse list-unstyled" id="homeSubmenu">
                        <li><a href="#">Page</a></li>
                        <li><a href="#">Page</a></li>
                        <li><a href="#">Page</a></li>
                    </ul>
                <li><a href="#">Portfolio</a></li>
                <li><a href="#">Contact</a></li>
            </ul>
        </nav>

        <div id="content">
            <button type="button" id="sidebarCollapse" class="navbar-btn">
                <span></span>
                <span></span>
                <span></span>
            </button>
        <div>

</div>

CSS

Animating the toggle button

Let's add some styles to the button and its bars first.

There'll be three bars under each other and we'll give them a fancy transition with a cubic-bezier transition. I often use this CSS animation tool to produce cool transitions, you can choose from some pre-build transitions or make your custom one easily.

 #sidebarCollapse {
    width: 40px;
    height: 40px;
    background: #f5f5f5;
}

#sidebarCollapse span {
    width: 80%;
    height: 2px;
    margin: 0 auto;
    display: block;
    background: #555;
    transition: all 0.8s cubic-bezier(0.810, -0.330, 0.345, 1.375);
}

When the sidebar is open, the toggle button bars will be crossed. When it's off the canvas, the bars will be parallel to each other.

By default, the sidebar will be open, so the initial state of the bars should be crossed. We'll use transform property to achieve that. The first bar will be rotated by 45 degrees, the last bar will be rotated by 45 degrees in the opposite direction. The second bar will be hidden in this moment.

#sidebarCollapse span:first-of-type {
    /* rotate first one */
    transform: rotate(45deg) translate(2px, 2px);
}
#sidebarCollapse span:nth-of-type(2) {
    /* second one is not visible */
    opacity: 0;
}
#sidebarCollapse span:last-of-type {
    /* rotate third one */
    transform: rotate(-45deg) translate(1px, -1px);
}

By clicking the button, the bars turn into the parallel state. To make that, we will use jQuery to toggle .active class on the button. This class cancels the rotation of the bars and makes them all visible.

#sidebarCollapse.active span {
    /* no rotation */
    transform: none;
    /* all bars are visible */
    opacity: 1;
    margin: 5px auto;
}

Animating the sidebar

Now, let's add some 3D CSS animation to the sidebar.

We'll make a door-opening animation when the user closes or opens the sidebar.

First of all, we should add perspective property to the container. Our container in this case is .wrapper. The perspective property defines how many pixels a 3D element is placed from the view and allows you to change the perspective on how 3D elements are viewed.

Then, we'll rotate the sidebar vertically by 100 degrees during collapsing out using transform property.

The transform-origin property allows you to change the position of transformed elements. Here we'll rotate the sidebar from the center left side.

.wrapper {
    display: flex;
    align-items: stretch;
    perspective: 1500px; /* Define how many pixels the sidebar is placed from the view. */
}

#sidebar {
    min-width: 250px;
    max-width: 250px;
    background: #7386D5;
    color: #fff;
    transition: all 0.6s cubic-bezier(0.945, 0.020, 0.270, 0.665);
    transform-origin: center left; /* Set the transformed position of sidebar to center left side. */
}

#sidebar.active {
    margin-left: -250px;
    transform: rotateY(100deg); /* Rotate sidebar vertically by 100 degrees. */
}

Media Queries

On smaller screens, the sidebar will be collapsed out by default. The default state of the hamburger menu should be returned to the parallel state. To achieve this, we should switch the CSS rules from  the standard view.

@media (max-width: 768px) {
    /* Reversing the behavior of the sidebar: 
       it'll be rotated vertically and off canvas by default, 
       collapsing in on toggle button click with removal of 
       the vertical rotation.   */
    #sidebar {
        margin-left: -250px;
        transform: rotateY(100deg);
    }
    #sidebar.active {
        margin-left: 0;
        transform: none;
    }

    /* Reversing the behavior of the bars: 
       Removing the rotation from the first,
       last bars and reappear the second bar on default state, 
       and giving them a vertical margin */
    #sidebarCollapse span:first-of-type,
    #sidebarCollapse span:nth-of-type(2),
    #sidebarCollapse span:last-of-type {
        transform: none;
        opacity: 1;
        margin: 5px auto;
    }

    /* Removing the vertical margin and make the first and last bars rotate again when the sidebar is open, hiding the second bar */
    #sidebarCollapse.active span {
        margin: 0 auto;
    }
    #sidebarCollapse.active span:first-of-type {
        transform: rotate(45deg) translate(2px, 2px);
    }
    #sidebarCollapse.active span:nth-of-type(2) {
        opacity: 0;
    }
    #sidebarCollapse.active span:last-of-type {
        transform: rotate(-45deg) translate(1px, -1px);
    }
}

Javascript

We'll use jQuery to toggle the .active class to switch between the crossed and parallel states.

$(document).ready(function () {
    $('#sidebarCollapse').on('click', function () {
        $('#sidebar').toggleClass('active');
        $(this).toggleClass('active');
    });
});

Let's have a look at the result View Demo

Conclusion

I hope this tutorial has helped you to understand how to implement a Bootstrap sidebar to your project. If you liked the article - let your friends know about it.

Thanks for reading :)

Hi, I'm Ondrej, creator of Bootstrapious. I have published Bootstrap tutorials and freebies here since 2015.

Would you like to choose a topic for a next tutorial or a type of the next freebie? Do you need a consultation or would you like to get an exposure on the Bootstrapious website?

Become one of Bootstrapious' patrons on Patreon and get one of the many great rewards I offer there.

Thanks for stopping by and have a great day ;)

Ondrej, Bootstrapious

You might also like one of our Free Themes

Universal - Business & E-commerce Template

Universal is clean and stylish universal website template built with Bootstrap 3.3. It stands out…

View theme

Italiano - Restaurant or Café Template

Italiano is my free Bootstrap HTML responsive template. You can use it to build an elegant website…

View theme

Under construction template

Under construction is a clean and simple free Bootstrap template. You can have it uploaded as…

View theme

Comments

Comments automatically close after 120 days.

Although I would like to help everyone getting their websites running, I simply can't as my time is really limited.

If you need a consultation or support, I provide a dedicated consulting to Bootstrapious Patrons - just choose a Dedicated support reward on my Patreon page and get in touch. Thank you for understanding and supporting Bootstrapious.

Share on Facebook Share on G+ Share on Twitter Share on LinkedIn