Haytam
Haytam

Reputation: 4733

How to make nav-item take the needed width for its content to show in one line?

I'm trying to make my nav-item elements take the needed width for them to display their content in one line, because right now the content gets wrapped in 2 lines:

Code:

https://jsfiddle.net/tavyhkem/2/

    <ul class="navbar-nav mt-2 mt-lg-0">
        <li class="nav-item">
            <a class="nav-link" href="#">
                <h5 class="no-margin-bottom">
                    <i class="fas fa-user"></i>
                    My Account
                </h5>
            </a>
        </li>
        <li class="nav-item">
            <a class="nav-link" href="#">
                <h5 class="no-margin-bottom">
                    <i class="fas fa-shopping-cart"></i>
                    Cart
                </h5>
            </a>
        </li>
    </ul>

Result needed:

I need the items "My Account" and "Cart" to take as much width needed for them to stay in one line.

Is this because of my inline w-100 search bar?

Upvotes: 2

Views: 2322

Answers (1)

WebDevBooster
WebDevBooster

Reputation: 14954

Is this because of my inline w-100 search bar?

Yes, it is but you can fix the problem by applying the class text-nowrap to the items that need to stay in one line like so:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<script defer src="https://use.fontawesome.com/releases/v5.0.6/js/all.js"></script>

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

<nav class="navbar navbar-expand-lg navbar-light bg-light">
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo03" aria-controls="navbarTogglerDemo03" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>
    <a class="navbar-brand" href="#"><h2 class="no-margin-bottom">TITLE</h2></a>

    <div class="collapse navbar-collapse" id="navbarTogglerDemo03">
        <form class="form-inline d-inline w-100 my-2 my-lg-0 mr-2">
            <div class="input-group">
                <input class="form-control" type="search">
                <div class="input-group-append">
                    <button class="btn btn-outline-secondary" type="button">
                        <i class="fas fa-search"></i>
                    </button>
                </div>
            </div>
        </form>
        <ul class="navbar-nav mt-2 mt-lg-0">
            <li class="nav-item text-nowrap" style="width: 158px;">
                <a class="nav-link" href="#">
                    <h5 class="no-margin-bottom">
                        <i class="fas fa-user"></i>
                        My Account
                    </h5>
                </a>
            </li>
            <li class="nav-item text-nowrap">
                <a class="nav-link" href="#">
                    <h5 class="no-margin-bottom">
                        <i class="fas fa-shopping-cart"></i>
                        Cart
                    </h5>
                </a>
            </li>
        </ul>
    </div>
</nav>

As the name suggests, text-nowrap prevents text from wrapping.

Upvotes: 3

Related Questions