• Override the default .nav-link { display: block } to inline-block
• vertical-align: middle lines them up on the same baseline
Bonus: if you don’t mind moving the icon into the <a>, you can also do:
html
<a class="nav-link active" href="#">
Active <i class="fa fa-times"></i>
</a>
Then spacing and alignment come for “free” since they’re in the same element.
Pick whichever feels easiest—both will keep your pill text and “×” icon perfectly aligned! 😃
User
currently it is inside, but i have an event on it and have trouble with preventDefault
Assistant
Here’s a pattern that lets you keep the icon inside the <a> (so it’s easier to style together), but still trap clicks on the icon and stop the link from firing:
Make your link use inline-flex so the text + icon line up
Hook the icon’s click, call both preventDefault() (stop navigation) and stopPropagation() (stop the link’s click handler)
html
<ul class="nav nav-pills">
<li class="nav-item">
<!-- inline-flex on .nav-link aligns its children center-vertically -->
<a href="#" class="nav-link active d-inline-flex align-items-center">
Active
<i class="fa fa-times ml-2 js-remove-pill"
aria-hidden="true"
title="Remove"></i>
</a>
</li>
</ul>
css
/* (optional) if you don’t want to add the Bootstrap flex classes) */
/* .nav-link { display: inline-flex; align-items: center; } */
/* .nav-link .fa-times { margin-left: .5rem; } */
js
// Using jQuery for example
$('.nav').on('click', '.js-remove-pill', function(e){
e.preventDefault(); // ✋ stop the <a> from navigating
e.stopPropagation(); // ✋ stop any parent-click handlers
console.log('remove pill clicked');
// …do your “remove” logic here…
});
Explanation
• display:inline-flex; align-items:center; on the <a> keeps text + icon perfectly aligned.
• By binding directly to the icon (or via delegation), calling both preventDefault() and stopPropagation()
guarantees the link won’t fire its click or navigate.