This CSS code snippet helps you to create an expand text effect on the hover event of a span element when the user hovers over its parent element. The effect is achieved using CSS and works by transitioning the max-width of the span element from 0 to 100% on hover.
You can implement this code in your website’s headings for an eye-catching hover effect. Enhance user engagement by revealing additional information or creating interactive headers.
How to Create Expand Text Effect On Hover Using CSS
1. Begin by copying the following HTML structure. Ensure you have an element, like a heading (<h1>
), with a nested <span>
containing the additional text you want to reveal.
<h1>Hover me<span id="hide"> to see some more text</span>!</h1>
2. Paste the CSS code into your stylesheet. This code cleverly utilizes properties like max-width
, overflow
, and transition
to control the visibility of the hidden text. Customize the styles to match your website’s design.
body { height: 100vh; overflow: hidden; background-color: #121212; color: #fafafa; font-family: "Calibri", sans-serif; display: flex; justify-content: center; align-items: center; text-align: center; } /* A quick explaination of how this effect works. A span elemnt won't have a width of it's own. So we convert it to an inline-block using the display property. Then we turn down the max-width of the element to 0. The reason we use max-width is due to a side-effect which is harder to explain in words so just try to replace that with width ;). You'll see that even when we changed the element's width to 0, it's still showing the text. For this we re gonna use overflow property and set it to hidden. To keep the text in one line and aligned properly, we are gonna use the white-space and vertical-align property as set below. The final thing we'll do is to give transition to the max-width property. */ #hide { display: inline-block; max-width: 0%; vertical-align: bottom; overflow: hidden; white-space: nowrap; transition: max-width 1s ease-in-out; } h1 { cursor: default; font-size: 5vw; } /* On hover of the h1 tag, we are gonna max out the max-width. */ h1:hover #hide { max-width: 100%; }
Replace the sample text in the HTML with your own content. Experiment with different elements and tweak the styles to suit your design preferences. This hover effect works well with headings, bringing a modern and dynamic feel to your webpage.
That’s all! hopefully, you have successfully created Expand Text On Hover Using CSS. If you have any questions or suggestions, feel free to comment below.
Similar Code Snippets:
I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences.
I truly enjoy what I’m doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.