Search code examples
bootstrap-4flexboxcontainersnavbar

Bootstrap 4 - Moving 1 element to opposite end of container


I've tried to find if a similar question has been asked before but couldn't find exactly this same issue. I am trying to move the phone number from the left, with the other elements, to the far right of the container. I've tried a few different commands but can't get it to budge. Any insights would be appreciated!

 <!DOCTYPE html>
<html>
<head>
    <title>Retirement4U</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
    <script src="https://kit.fontawesome.com/b6e6bb0ba7.js" crossorigin="anonymous"></script>
    <link rel="stylesheet"type="text/css" href="retirement4u.css">
</head>
<body>

    <nav class="navbar navbar-expand-sm navbar-dark bg-custom p-1">
        <img id="head-img" src="Retirement-Solutions-4u-Final-logo.png" alt="Retirement-Solutions-4U-Final-logo">
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarNav">
            <div class="container">
                <ul class="navbar-nav">
                    <li class="nav-item active">
                        <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="#">Features</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="#">Pricing</a>
                    </li>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link head-phone ml-auto justify-content: right" href="#"><i class="fas fa-phone-alt"></i> 888-218-0324</a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
</body>
</html>

Solution

  • To Shift The Entire Navbar to the Right

    add margin-left: auto to the css for your navbar-nav element. You can do that in many different ways, giving the element an id or your own class name and styling those in a css file for example. The most simple way to test would be with inline styling, as in:

    <div class="collapse navbar-collapse" id="navbarNav">
        <div class="container">
            <ul class="navbar-nav" style="margin-left: auto;">
                ...
            </ul>
         </div>
    </div>
    

    To Shift Just the Phone Number

    In order to do this, we need to make the navbar-nav element, which contains all the links, large enough to position the links it contains. In your current implementation, the navbar-nav element the same size as what it contains, so, naturally, the links are all right next to eachother. To do this, we make the navbar-nav element the same width as its enclosing container by styling it with width: 100%. Now that there's empty space inside the navbar-nav element to space out the links, we can get the phone number to shift over to the right by styling it with margin-left: auto, which fills in open space with the left margin of the phone number item.

    <div class="collapse navbar-collapse" id="navbarNav">
        <div class="container">
            <ul class="navbar-nav" style="width: 100%;">
                <li class="nav-item active">
                    <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">Features</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">Pricing</a>
                </li>
                <li class="nav-item" style="margin-left: auto;">
                    <a class="nav-link head-phone ml-auto justify-content: right" href="#"><i class="fas fa-phone-alt"></i> 888-218-0324</a>
                </li>
            </ul>
        </div>
    </div>