Easy Tutorial
❮ Bootstrap Make A Website Bootstrap Scrollspy Plugin ❯

Bootstrap Navigation Bar

Introduction

With Bootstrap, you can create static navigation bars. This tutorial will teach you how to do this.

We will use an HTML5 page with content and see how to add a header navigation bar. Below is the final output:

Here is the HTML5 code for a page without any content.

<!DOCTYPE html> 
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Bootstrap Basic Tab Based Navigation</title>
    <meta name="description" content="Bootstrap Basic Tab Based Navigation Example">
    <link href="../bootstrap/twitter-bootstrap-v2/docs/assets/css/bootstrap.css" rel="stylesheet">
</head>
<body>
<!-- We will create the navigation here -->
</body>
</html>

To create the basic structure with navigation, you need to add the following code immediately after the displayed HTML5 code:

<div class="navbar navbar-fixed-top">
    <div class="navbar-inner">
        <div class="container">
            <!-- Navigation goes here -->
        </div>
    </div>
</div>

Now, let's add the following code:

<ul class="nav">
    <li class="active">
        <a class="brand" href="#">tutorialpro</a>
    </li>
    <li><a href="#">About</a></li>
    <li><a href="#">Portfolio</a></li>
    <li><a href="#">Contact</a></li>
</ul>

If you need to create a dropdown menu, insert the following code after the last displayed HTML block:

<ul class="nav">
    <li class="dropdown">
        <a href="#"
           class="dropdown-toggle"
           data-toggle="dropdown">
            Services
            <b class="caret"></b>
        </a>
        <ul class="dropdown-menu">
            <li><a href="#">Web Design</a></li>
            <li><a href="#">Web development</a></li>
            <li><a href="#">Wordpress Theme development</a></li>
        </ul>
    </li>
</ul>

However, since Bootstrap dropdown menus require JavaScript, you need to insert the following two lines in your HTML page. You can place them before the </body> tag.

<script src="../bootstrap/twitter-bootstrap-v2/docs/assets/js/jquery.js"></script>
<script src="../bootstrap/twitter-bootstrap-v2/docs/assets/js/bootstrap-dropdown.js"></script>

To insert a search form into the navigation bar, simply add the following code after the <ul> containing the dropdown menu list. Note that the class "pull-left" aligns the search form to the left. If you want to align it to the right, use the "pull-right" class.

<form class="navbar-search pull-left">
    <input type="text" class="search-query" placeholder="Search">
</form>

Now, we want to insert interactive buttons into the navigation bar, located on the right side of the navigation bar. We will insert Google Plus, Facebook Like, and Tweet buttons. You need to insert the following code after the search form.

<ul class="nav pull-right">
    <li class="dropdown">
        <a href="#"
           class="dropdown-toggle"
           data-toggle="dropdown">
            Social
            <b class="caret"></b>
        </a>
        <ul class="dropdown-menu">
            <li class="socials"><!-- Place this tag where you want the +1 button to render -->
                <g:plusone annotation="inline" width="150"></g:plusone>
            </li>
            <li class="socials"><div class="fb-like" data-send="false" data-width="150" data-show-faces="true"></div></li>
            <li class="socials"><a href="https://twitter.com/share" class="twitter-share-button">Tweet</a>
                <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script></li>
        </ul>
    </li>
</ul>

The class "socials" is not part of bootstrap.css. Since we want all interactive buttons to be centered, we need to create some padding through it. The code is as follows:

.socials {
padding: 10px;
}

You can write it directly on the page using

Finally, you need to insert two blocks of code. One for the Facebook button and another for Google Plus. These are provided by Facebook and Google to dynamically insert buttons into your page.

So, after the opening body tag, insert the following code for Facebook

<div id="fb-root"></div>
<script>(function(d, s, id) {
        var js, fjs = d.getElementsByTagName(s)[0];
        if (d.getElementById(id)) return;
        js = d.createElement(s); js.id = id;
        js.src = "//connect.facebook.net/en_GB/all.js#xfbml=1";
        fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));</script>

Then before the closing body tag, insert the following code for Google Plus

<script type="text/javascript">
    (function() {
        var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
        po.src = 'https://apis.google.com/js/plusone.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
    })();
</script>

Here is the final code

Example

```html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Bootstrap Navigation Example</title>
    <meta name="description" content="Bootstrap navbar Example">
    <link href="../bootstrap/twitter-bootstrap-v2/docs/assets/css/bootstrap.css" rel="stylesheet">
    <style type="text/css">
        .socials {
            padding: 10px;
        }
    </style>
</head>
<body>
<div id="fb-root"></div>
<script>(function(d, s, id) {
        var js, fjs = d.getElementsByTagName(s)[0];
        if (d.getElementById(id)) return;
        js = d.createElement(s); js.id = id;
        js.src = "//connect.facebook.net/en_GB/all.js#xfbml=1";
        fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));</script>
<div class="navbar navbar-fixed-top">
    <div class="navbar-inner">
        <div class="container">
            <ul class="nav">
                <li class="active">
                    <a class="brand" href="#">w3cschool</a>
                </li>
                <li><a href="#">About</a></li>
                <li><a href="#">Portfolio</a></li>
                <li><a href="#">Contact</a></li>
            </ul>
            <ul class="nav">
                <li class="dropdown">
                    <a href="#"
                       class="dropdown-toggle"
                       data-toggle="dropdown">
                        Services
                        <b class="caret"></b>
                    </a>
                    <ul class="dropdown-menu">
                        <li><a href="#">Web Design</a></li>
                        <li><a href="#">Web development</a></li>
                        <li><a href="#">WordPress Theme development</a></li>
                    </ul>
                </li>
            </ul>
            <form class="navbar-search pull-left">
                <input type="text" class="search-query" placeholder="Search">
            </form>
            <ul class="nav pull-right">
                <li class="dropdown">
                    &lt;a href="#"
<a class="dropdown-toggle" data-toggle="dropdown">
    Social
    <b class="caret"></b>
</a>
<ul class="dropdown-menu">
    <li class="socials"><!-- Place this tag where you want the +1 button to render -->
        <g:plusone annotation="inline" width="150"></g:plusone>
    </li>
    <li class="socials"><div class="fb-like" data-send="false" data-width="150" data-show-faces="true"></div></li>
    <li class="socials"><a href="https://twitter.com/share" class="twitter-share-button">Tweet</a>
        <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script></li>
</ul>
</li>
</ul>
</div>
</div>
</div>
<script src="../bootstrap/twitter-bootstrap-v2/docs/assets/js/jquery.js"></script>
<script src="../bootstrap/twitter-bootstrap-v2/docs/assets/js/bootstrap-dropdown.js"></script>
<script type="text/javascript">
(function() {
    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
    po.src = 'https://apis.google.com/js/plusone.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();
</script>
</body>
</html>

Click here to view the example online.

Please note that you need to add a 40px padding after the body start tag. Add it after the core Bootstrap CSS and before the optional responsive CSS.

Bootstrap also allows you to create responsive navigation bars. Here is the code:

Example

<div class="navbar">
    <div class="navbar-inner">
        <div class="container">
            <!-- .btn-navbar is used as the toggle for collapsed navbar content -->
            <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>

<!-- Be sure to leave the brand out there if you want it shown -->
<a class="brand" href="#">Project name</a>

<!-- Everything you want hidden at 940px or less, place within here -->
<div class="nav-collapse">
    <!-- .nav, .navbar-search, .navbar-form, etc -->
</div>

</div>
</div>
</div>

Click here to download all HTML, CSS, JS, and image files used in this tutorial.

❮ Bootstrap Make A Website Bootstrap Scrollspy Plugin ❯