Back
Learn how to build an elegant add friend button that transforms from a plus icon to descriptive text on hover, enhancing user experience with smooth CSS transitions.
<div class="button"> <span class="plus">+</span> <span class="text">Add Friend</span> </div>
.button { background-color: rgb(78, 180, 203); width: 50px; height: 50px; border-radius: 25px; display: flex; justify-content: center; align-items: center; transition: 0.3s; overflow: hidden; cursor: pointer; color: #fff; }Both our text elements need to occupy the same visual space within the button. We use absolute positioning to place them in the same location, allowing us to swap between them without affecting the button's layout. We apply a transition to ensure smooth fading between states when the visibility changes.
.plus, .text { position: absolute; transition: 0.3s; }The magic happens when users hover over our button. The width expands from 50px to 100px while maintaining the same height, transforming from a circle to a rounded rectangle—a shape more suitable for containing text. This expansion animation takes 0.3 seconds, creating a smooth, satisfying interaction that draws the user's attention to the emerging text.
.button:hover { width: 100px; transition: 0.3s; }As the button expands on hover, we want the plus symbol to gracefully disappear. We accomplish this by animating its opacity from 1 (fully visible) to 0 (completely transparent) over 0.3 seconds. This creates a fade-out effect that feels natural and prevents an abrupt visual change that might be jarring to users.
.button:hover .plus { opacity: 0; transition: 0.3s; }By default, we keep the "Add Friend" text completely invisible by setting its opacity to 0. This ensures that only the plus symbol is visible in the button's initial state.
.text { opacity: 0; }When the user hovers over the button, we animate the text's opacity from 0 to 1 over 0.3 seconds, creating a fade-in effect that perfectly complements the plus symbol's fade-out. This coordinated transition creates the impression that the plus symbol is being replaced by the text, providing clear feedback about the button's purpose.
.button:hover .text { opacity: 1; transition: 0.3s; }This expandable button design provides an elegant solution for interfaces where space is at a premium, such as mobile applications or densely packed dashboards. The initial compact circular form conserves space, while the expansion on hover reveals the button's full purpose in a way that feels engaging and intuitive to users. The pure CSS implementation ensures compatibility across browsers without requiring JavaScript, making it both performant and accessible.
<div class="button"> <span class="plus">+</span> <span class="text">Add Friend</span> </div> <style> .button { background-color: rgb(78, 180, 203); width: 50px; height: 50px; border-radius: 25px; display: flex; justify-content: center; align-items: center; transition: 0.3s; overflow: hidden; cursor: pointer; color: #fff; } .plus, .text { position: absolute; transition: 0.3s; } .button:hover { width: 100px; transition: 0.3s; } .button:hover .plus { opacity: 0; transition: 0.3s; } .text { opacity: 0; } .button:hover .text { opacity: 1; transition: 0.3s; } </style>Thank you for reading this article.
Comments
No comments yet. Be the first to comment!