This is what I just thought, look what you think. Therefore, we use :after and create a line below the text. This only works if the parent has a width (for centering).
HTML:
<div>Test</div>
CSS
div { width: 30px; } div:hover:after { content: ""; display: block; width: 5px; border-bottom: 1px solid; margin: 0 auto; }
Demo
Updated CSS:
div { display: inline-block; }
Not sure why I didn't think about it, but you can just use inline-block to get it to the center without a parent having a width.
DEMO HERE
Here is a link using the same method, just make you confused.
DEMO HERE
So, now they told me that I should even point out the most obvious thing, so here is an update for people who donโt know that width can be percentage.
width: 70%;
Changed the width from 5px to 70% , so it will expand with the width of the text.
DEMO HERE
Rudy
source share