问题
I want to display some items next to each other when the window is collapsed in the navbar. Currently, it displays the items in a column like this: Navbar collapsed. I want the image next to the username instead.
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<a class="navbar-brand" href="#">Navbar</a>
<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">
<ul class="ml-auto navbar-nav border border-primary rounded" style="background:blue">
<li class=" nav-item active">
<a class="nav-link" href="#">Hi, Shadow <span class="sr-only">(current)</span></a>
</li>
<li id="something1" class="mt-auto mb-auto nav-item">
<img id="something" src="images/space_stars_blue_free_wallpaper.jpg" class="img-fluid rounded-circle "
alt="..." width="50" height="50">
</li>
</ul>
</div>
回答1:
Just add the flex-row class to the navbar-nav
...
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<a class="navbar-brand" href="#">Navbar</a>
<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">
<ul class="ml-auto navbar-nav border border-primary rounded flex-row" style="background:blue">
<li class=" nav-item active">
<a class="nav-link" href="#">Hi, Shadow <span class="sr-only">(current)</span></a>
</li>
<li id="something1" class="mt-auto mb-auto nav-item">
<img id="something" src="//placehold.it/50" class="img-fluid rounded-circle ml-1" alt="..." width="50" height="50">
</li>
</ul>
</div>
</nav>
https://www.codeply.com/go/U0dP6MM9WJ
回答2:
With Bootstrap 4, they have a inline list class for the unordered list and it's list items. It would look something like this:
<ul class="list-inline">
<li class="list-inline-item">Lorem ipsum</li>
<li class="list-inline-item">Phasellus iaculis</li>
<li class="list-inline-item">Nulla volutpat</li>
</ul>
Here is the link to check out Bootstraps official documentation on it: http://v4-alpha.getbootstrap.com/content/typography/#inline
回答3:
Making the navbar-nav and navbar list items to display: inline-block
will do what you want:
.navbar-nav {
display: inline-block;
}
.navbar-nav > li {
display: inline-block;
}
来源:https://stackoverflow.com/questions/53566435/bootstrap-4-how-to-display-items-in-a-row-when-navbar-is-collapsed