Yes, there are several ways to get the <a> and <i> elements on the same level. Here are the best approaches:
Method 1: Put the Icon Inside the Link (Recommended)
The simplest solution is to move the icon inside the <a> tag:
<ul class="nav nav-pills">
<li class="nav-item">
<a class="nav-link active" href="#">
Active
<i class="fa fa-times ml-2"></i>
</a>
</li>
</ul>
Method 2: Use Flexbox on the List Item
Keep your current HTML structure and make the <li> a flex container:
<ul class="nav nav-pills">
<li class="nav-item d-flex align-items-center">
<a class="nav-link active" href="#">Active</a>
<i class="fa fa-times ml-2"></i>
</li>
</ul>
Method 3: Custom CSS with Flexbox
For more control, add a custom class:
<ul class="nav nav-pills">
<li class="nav-item nav-item-with-icon">
<a class="nav-link active" href="#">Active</a>
<i class="fa fa-times"></i>
</li>
</ul>
<style>
.nav-item-with-icon {
display: flex;
align-items: center;
gap: 8px;
}
.nav-item-with-icon .nav-link {
margin-bottom: 0; /* Remove any bottom margin */
}
</style>
Method 4: Separate Click Areas
If you want the icon to be clickable separately (e.g., for a close/delete action):
<ul class="nav nav-pills">
<li class="nav-item d-flex align-items-center">
<a class="nav-link active" href="#">Active</a>
<button class="btn btn-link p-0 ml-2 text-danger">
<i class="fa fa-times"></i>
</button>
</li>
</ul>
Method 5: Absolute Positioning
For an overlay effect:
<ul class="nav nav-pills">
<li class="nav-item position-relative">
<a class="nav-link active pr-4" href="#">Active</a>
<i class="fa fa-times position-absolute" style="right: 10px; top: 50%; transform: translateY(-50%);"></i>
</li>
</ul>
Method 1 is usually the best choice if the icon is part of the link's functionality. Method 2 with flexbox is ideal if you need the icon to be a separate clickable element. Choose based on your specific use case!