Bootstrap responsive space between navbar items

Viewed 259

I am working on a simple webapp with Bootstrap and I need to make the space between the navbar items responsive so that in smaller screens the navbar-items are close toghether but in larger screens they are further apart from each other.

Here is what i have done at the moment:

.title {
    font-family: -apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji;
    font-size: 26px;
    line-height: 1.25;
}

.btn-bd-sign-in {
    font-weight: 600;
    color: white;
}

.btn-bd-sign-up {
    font-weight: 600;
    color: white;
    border-color: white;
}

.btn-bd-sign-up:hover {
    font-weight: 600;
    color: black;
    border-color: white;
    background-color: white;
}

.home-btn {
    font-weight: 600;
    color: white!important;
}

.home-btn:hover {
    font-weight: 600;
    color: #212529!important;
}

body {
    height: 100vh;
    background: linear-gradient(to right, #33ccff 0%, #ff99cc 100%);
}

button, button:active, button:focus {
    background: none!important;
    border: none;
}

button:hover {
    border-bottom: 3px solid #0275d8;
}
<body>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
        integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
        integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
        crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"
        integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1"
        crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
        integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
        crossorigin="anonymous"></script>
    <div class="container-fluid padding-left">
        <div class="row flex-xl-nowrap">
            <div class="col-4">
            </div>
            <div class="col-8-auto">
                <nav class="navbar navbar-expand navbar-light">
                    <div>
                        <ul class="navbar-nav mr-md-5 mr-lg-5 mr-xl-5">
                            <li class="nav-item">
                                <button class="nav-link" href="#">Profile</button>
                            </li>
                            <li class="nav-item">
                                <button class="nav-link" href="#">Statistics</button>
                            </li>
                            <li class="nav-item">
                                <button class="nav-link" href="#">Polls</button>
                            </li>
                            <li class="nav-item">
                                <button class="nav-link" href="#">Settings</button>
                            </li>
                        </ul>
                    </div>
                </nav>
            </div>
        </div>
    </div>
</body>

How can i achieve this feature?

This is how it looks at the moment:

enter image description here

On mobile devices it looks as expected but i would like on larger devices a little bit more spacing then what it is shown in the image.

2 Answers

Your nav is limited to the container size of div. If you remove auto class and add w-100 to get the full width and add some flex classes, then nav can be stretched.

An example:

  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
        integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
        integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
        crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"
        integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1"
        crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
        integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
        crossorigin="anonymous"></script>
    <div class="container-fluid padding-left">
        <div class="row flex-xl-nowrap">
            <div class="col-4 bg-success">
            </div>
            <div class="col-8 bg-info">
                <nav class="navbar navbar-expand navbar-light w-100">

                        <ul class="navbar-nav mr-md-5 mr-lg-5 mr-xl-5  d-flex justify-content-between w-100">
                            <li class="nav-item">
                                <button class="nav-link" href="#">Profile</button>
                            </li>
                            <li class="nav-item">
                                <button class="nav-link" href="#">Statistics</button>
                            </li>
                            <li class="nav-item">
                                <button class="nav-link" href="#">Polls</button>
                            </li>
                            <li class="nav-item">
                                <button class="nav-link" href="#">Settings</button>
                            </li>
                        </ul>

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

    </div>

As you will see in the Bootstrap docs, the spacing utility classes (padding and margins) are responsive. For example...

px-lg-5 px-sm-3 px-1

responsively applies padding according to screen width...

  • large and up, 5 left/right padding units
  • sm and md, 3 left/right padding units
  • xs, 1 left/right padding unit

Therefore the same can be done with the nav-items...

<ul class="navbar-nav">
    <li class="nav-item px-lg-5 px-sm-3 px-1">
        <button class="nav-link" href="#">Profile</button>
    </li>
    <li class="nav-item px-lg-5 px-sm-3 px-1">
        <button class="nav-link" href="#">Statistics</button>
    </li>
    <li class="nav-item px-lg-5 px-sm-3 px-1">
        <button class="nav-link" href="#">Polls</button>
    </li>
    <li class="nav-item px-lg-5 px-sm-3 px-1">
        <button class="nav-link" href="#">Settings</button>
    </li>
</ul>

Of course, you can change the p-{breakpoint}-{value} according to your needs.

Demo

Related