The problem is that when the distance between the texts increases, the text goes to a new line and creates this transition thing. That way you can add white-space: nowrap , and you can also use padding-left to enter text and create it in the center:
div { min-width: 150px; width: 180px; height: 25px; line-height: 25px; background: orange; text-align: center; overflow: hidden; transition: all 0.5s; box-sizing: border-box; white-space: nowrap; } div:hover { word-spacing: 80px; padding-left: 80px; background: white; }
<div> <a class="afirst" href="">First</a> & <a class="asecond" href="">Second</a> </div>
Actually the word spacing is applied to the div, so you cannot apply a word hover. It is also easier to apply this technique on the first word, since the second will be hiding with overflow, but I'm not sure how you can do the same with the second word using line spacing.
Here's another idea on how you can get along without markup. I used add-on animation as well as a pseudo-element to hide the first word when the second falls.
div { min-width: 150px; width: 180px; height: 25px; line-height: 25px; background: orange; text-align: center; overflow: hidden; transition: all 0.5s; box-sizing: border-box; white-space: nowrap; } .afirst, .asecond { position:relative; display: inline-block; transition: all 0.5s; } .afirst:hover { padding: 0 44%; background: white; } .asecond:hover { padding: 0 50% 0 0; background: white; } .asecond:hover::before { content:" "; position:absolute; left:-50%; width:50%; top:0; bottom:0; z-index:99; background:#fff; }
<div> <a class="afirst" href="">First</a> & <a class="asecond" href="">Second</a> </div>
I think you can generalize this solution using the : before element on the left and : after in the right to hide the rest.
Here is an example with a few words (but the alignment of the center is not correct, you still need to improve):
div { min-width: 150px; height: 25px; line-height: 25px; background: orange; text-align: center; overflow: hidden; transition: all 0.5s; box-sizing: border-box; white-space: nowrap; } .word { position: relative; display: inline-block; transition: all 0.5s; } .word:hover { background: white; padding: 0 40%; } .word:hover::before { content: " "; position: absolute; left: -50%; width: 50%; top: 0; bottom: 0; z-index: 99; background: #fff; } .word:hover::after { content: " "; position: absolute; right: -50%; width: 50%; top: 0; bottom: 0; z-index: 99; background: #fff; }
<div> <a class="word" href="">First</a> & <a class="word" href="">Second</a> & <a class="word" href="">third</a> & <a class="word" href="">Fourth</a> </div>
Another solution with excellent centering but less animation for other words:
div { position:relative; height: 25px; width:500px; margin:0 auto; line-height: 25px; background: orange; text-align: center; overflow: hidden; transition: all 0.5s; box-sizing: border-box; white-space: nowrap; } .word { position: relative; z-index:9; display: inline-block; transition: all 0.5s; } .word:hover { position:absolute; right:0; left:0; top:0; bottom:0; text-align:center; background: white; z-index:99; }
<div> <a class="word" href="">First</a> & <a class="word" href="">Second</a> & <a class="word" href="">third</a> & <a class="word" href="">Fourth</a> </div>
source share