How to Create Text Reveal Effect for Buttons using HTML and CSS?


In this article, we will discuss the approach to creating test reveal effect for buttons using HTML and CSS.

Buttons are the most important user interface component for any website. It is very important to design the buttons in a creatively unique way. The text-reveal effect for a button is used when it is used to reveal some offer or exciting content for enhancing the user experience.

The approach is to cover the button with a strip of the same dimension as of button and then moving it in any one direction on mouse-hover.

To move forward with the approach mentioned we need to know about two selectors before and hover which will be used for the text reveal effect for the button approach with the css properties.

The ::before selector CSS pseudo-element, is used to add the same content multiple times before the content of other elements. This selector is the same as ::after selector. It helps to create the pseudo-element that represents the first child of the selected element & is generally used for adding decorative content to an element using the content property. Its default value is inline.

Syntax

The following is the syntax to the before selector −

element ::before{
   content:
}

The :hover selector CSS pseudo-class is used to style elements when the mouse hovers over them. It can be used on every element where it will select elements when you mouse over them.

Syntax

The following is the syntax to the before selector −

element :hover{	
   // CSS declarations;
}

The following HTML code snippet implements the creation of a simple button using the button tag.

Index.html

<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="UTF-8" />
      <meta name="viewport" content=
         "width=device-width, initial-scale=1.0" />
      <title>Create Text Reveal Effect for Buttons using HTML and CSS</title>
   </head>
   <body>
      <button>Text Reveal</button>
   </body>
</html>

CSS Code −

The following are the steps for implementing the CSS code −

Step 1 − Apply some basic styling to the button like adding padding and border-radius to have rounded corners.

Step 2 − Now use the before selector to create a strip of the same dimension to cover the whole button.

Step 3 − Now use the hover selector to move the strip to any one direction as it is moved to the left in the example.

Note − You can move the strip to any direction according to your need. Also, you can use some other properties to tweak the effect according to you.

Index.css

<style>
	button {
		position: absolute;
		top: 50%;
		left: 50%;
		font-size: 20px;
		padding: 15px;
	}
	button::before {
		content: "";
		position: absolute;
		top: 0%;
		left: 0%;
		width: 100%;
		height: 100%;
		background: blue;
		transition: 0.5s ease-in-out;
	}
	button:hover::before {
		left: -100%;
	}
</style>

Example

Complete Code − It is the combination of the above two sections of code.

<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="UTF-8" />
      <meta name="viewport" content="width=device-width, initial-scale=1.0" />
      <title>Text Reveal Effect for Buttons</title>
      <style>
         button {
            position: absolute;
            top: 50%;
            left: 50%;
            font-size: 20px;
            padding: 15px;
         }
         button::before {
            content: "";
            position: absolute;
            top: 0%;
            left: 0%;
            width: 100%;
            height: 100%;
            background: blue;
            transition: 0.5s ease-in-out;
         }
         button:hover::before {
            left: -100%;
         }
      </style>
   </head>
   <body>
      <button>Text Reveal</button>
   </body>
</html>

Supported Browsers − The browsers supported by pointer-events Property are listed below −

  • Google Chrome 1.0
  • Edge 12.0
  • Internet Explorer 11.0
  • Firefox 1.5
  • Opera 9.0
  • Safari 4.0

Note − Opera 4-6 supports with single-colon.(::before).

This article was focused on How to Create a Text Reveal Effect for Buttons using HTML and CSS using the before and the CSS selector.

Updated on: 12-Dec-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements