Expand Text on Hover Using CSS

Expand Text on Hover Using CSS
Code Snippet:Expand Text on Hover.
Author: Tejas Shetty
Published: January 10, 2024
Last Updated: January 22, 2024
Downloads: 810
License: MIT
Edit Code online: View on CodePen
Read More

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">&nbsp;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.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X