Open In App
Related Articles

How to align navbar items to the right in Bootstrap 4 ?

Improve Article
Improve
Save Article
Save
Like Article
Like

The .ml-auto class in Bootstrap can be used to align navbar items to the right. The .ml-auto class automatically aligns elements to the right. In this article, we will align the navbar to the right in two different ways, below both the approaches are discussed with proper example.

Example 1: In the first example, we use the .ml-auto class of Bootstrap 4 to align navbar items to the right. The .ml-auto class automatically gives a left margin and shifts navbar items to the right.

  • Program:




    <!DOCTYPE html>
    <html>
      
    <head>
      
        <!-- Including the bootstrap CDN -->
        <link rel="stylesheet" href=
        <script src=
        </script>
        <script src=
        </script>
        <script src=
        </script>
    </head>
      
    <body>
      
        <!-- Creating a navigation bar using the
             .navbar class of bootstrap -->
        <nav class="navbar navbar-expand-sm bg-light">
      
            <ul class="navbar-nav ml-auto">
                <li class="nav-item">
                    <a class="nav-link" href="#">
                      About
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">
                      Contacts
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">
                      Settings
                    </a>
                </li>
            </ul>
        </nav>
    </body>
      
    </html>

    
    

  • Output:

Example 2: In this example, we do not use any pre-defined Bootstrap 4 class to align the items. In this example, we create a navbar and then using CSS gives the left margin as an auto which shifts the navbar items to the right.

  • Program:




    <!DOCTYPE html>
    <html>
      
    <head>
      
        <!-- Including the bootstrap CDN -->
        <link rel="stylesheet" href=
        <script src=
        </script>
        <script src=
        </script>
        <script src=
        </script>
        <style>
            .navbar-nav {
                margin-left: auto;
            }
        </style>
    </head>
      
    <body>
      
        <!-- Creating a navigation bar using the
             .navbar class of bootstrap -->
        <nav class="navbar navbar-expand-sm bg-light">
      
            <ul class="navbar-nav">
                <li class="nav-item">
                    <a class="nav-link" href="#">
                      About
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">
                      Contacts
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">
                      Settings
                    </a>
                </li>
            </ul>
        </nav>
    </body>
      
    </html>

    
    

  • Output:

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 02 Mar, 2020
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials