When it comes to Bootstrap list-group items, adding
style="display: inline-block"
will not have any impact because the Bootstrap
list-group
utilizes flexbox with a vertical orientation (column):
.list-group {
display: flex;
flex-direction: column;
}
.list-group-item {
position: relative;
display: block;
}
If you desire a horizontal arrangement for list-group items instead of the default vertical layout, simply include the list-group-horizontal
class to the list-group element. This class alters the default list-group flex direction from column
(vertical) to row
(horizontal).
To demonstrate this, here is the generated HTML code based on the aforementioned:
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="89ebe6e6fdfafdfbe8f9c9bda7bfa7b9">[email protected]</a>/dist/css/bootstrap.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/fontawesome.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/brands.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/solid.min.css">
</head>
<body class="p-4">
<h4>Existing Navbar</h4>
<div class="navbar">
<ul class="list-group w-75 mx-auto" style="background-color: #e0e0e0">
<li class="list-group-item">
<i class="fab fa-twitter" id="twitter"></i>
</li>
<!-- some items to stand in for Nav components -->
<li onclick="alert('ambulance')" class="d-flex align-items-center list-group-item">
<i class="fas fa-ambulance"></i>
</li>
<li onclick="alert('apple')" class="d-flex align-items-center list-group-item">
<i class="fas fa-apple-alt"></i>
</li>
<li onclick="alert('baby carriage')" class="d-flex align-items-center list-group-item">
<i class="fas fa-baby-carriage"></i>
</li>
<li onclick="alert('biking')" class="d-flex align-items-center list-group-item">
<i class="fas fa-biking"></i>
</li>
<li class='mx-auto'>
<button class='btn butn p-3'>Tweet</button>
</li>
</ul>
</div>
</body>
It's evident that the list-group items are laid out vertically.
Here is the same HTML output with the addition of list-group-horizontal
:
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="75171a1a01060107140535415b435b45">[email protected]</a>/dist/css/bootstrap.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/fontawesome.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/brands.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/solid.min.css">
<body class="p-4">
<h4>Modified Navbar</h4>
<p>Changes:</p>
<ul>
<li>Removed "d-flex" from all list group items (had no effect)</li>
<li>Added "list-group-horizontal" to ul (puts all li elements on same line)
</li>
</ul>
<div class="navbar">
<ul class="list-group w-75 mx-auto list-group-horizontal" style="background-color: #e0e0e0">
<li class="list-group-item">
<i class="fab fa-twitter" id="twitter"></i>
</li>
<!-- some items to stand in for Nav components -->
<li onclick="alert('ambulance')" class="align-items-center list-group-item">
<i class="fas fa-ambulance"></i>
</li>
<li onclick="alert('apple-alt')" class="align-items-center list-group-item">
<i class="fas fa-apple-alt"></i>
</li>
<li onclick="alert('baby-carriage')" class="align-items-center list-group-item">
<i class="fas fa-baby-carriage"></i>
</li>
<li onclick="alert('biking')" class="align-items-center list-group-item">
<i class="fas fa-biking"></i>
</li>
<li class='mx-auto'>
<button class='btn butn p-3'>Tweet</button>
</li>
</ul>
</div>
</body>
To provide a full perspective, below is the code from the initial query with list-group-horizontal
added to the <ul>
:
const Navbar = () => {
return (
<div className='Navbar'>
<ul className='list-group w-75 mx-auto list-group-horizontal'>
<li className='list-group-item'>
<i className='fab fa-twitter' id='twitter' />
</li>
{nav.map((item) => (
<Nav key={v4()} iconClass={item.iconClass} name={item.name} />
))}
<li className='mx-auto'>
<button className='btn butn p-3'>Tweet</button>
</li>
</ul>
</div>
);
};
export default Navbar;
Furthermore, here is the updated Nav component with the removal of d-flex
from the <li>
:
const Nav = ({ iconClass, name }) => {
return (
<li onClick={onClick} className='align-items-center list-group-item'>
<i className={iconClass} />
<span>{name}</span>
</li>
);
};
export default Nav;