I basically wanted to make content not moving while clicking on an element and changing its font weight. I achieved that but i also wanted to have some kind of a badge next to the clicked element. From what i understand the css trick which makes font weight bold not moving the rest of the content requires display: block and display: inline-block to play around with. I wanted the badge to be next to the element using display: flex but it destroys the css trick and again makes the content move. Is there a way to fix that?
.list{
display: flex;
gap: 2rem;
list-style: none;
}
.item{
display: inline-block;
position: relative;
cursor: pointer;
}
.item:after{
display: block;
content: attr(title);
opacity: 0;
visibility: hidden;
font-weight: bold;
height: 0;
}
.item:active{
font-weight: bold;
}
.badge {
width: 20px;
height: 20px;
background: lightblue;
display: flex;
align-items: center;
justify-content: center;
border-radius: 50%;
}
<ul class="list">
<li class="item" title="First" >First <div class="badge">5</div></li>
<li class="item" title="Second" >Second <div class="badge">5</div></li>
<li class="item" title="Third" >Third <div class="badge">5</div></li>
</ul>
Snippet with display flex (making content move)
.list{
display: flex;
gap: 2rem;
list-style: none;
}
.item{
display: flex;
position: relative;
cursor: pointer;
}
.item:after{
display: block;
content: attr(title);
opacity: 0;
visibility: hidden;
font-weight: bold;
height: 0;
}
.item:active{
font-weight: bold;
}
.badge {
width: 20px;
height: 20px;
background: lightblue;
display: flex;
align-items: center;
justify-content: center;
border-radius: 50%;
}
<ul class="list">
<li class="item" title="First" >First <div class="badge">5</div></li>
<li class="item" title="Second" >Second <div class="badge">5</div></li>
<li class="item" title="Third" >Third <div class="badge">5</div></li>
</ul>