Home > Article > Web Front-end > How to Create Text Reveal Effect for Buttons using HTML and CSS?
In this article, we will discuss ways to create buttons using HTML and CSS to test the display effect.
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 mentioned approach 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 is a CSS pseudo-element used to add the same content multiple times before the content of other elements. This selector is the same as the ::after selector. It helps create a pseudo-element that represents the first child of the selected element and is often used to add decorative content to an element using the content attribute. Its default value is inline.
The following is the syntax of the before selector -
element ::before{ content: }
:hover selector is a CSS pseudo-class used to style elements when the mouse hovers over them. It can be applied to any element, selecting it when the mouse is hovering over it.
The following is the syntax of 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>The Chinese translation of
Complete code − It is a combination of the above two code parts.
<!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 −
Note − Opera 4-6 supports with single-colon.(::before).
This article focuses on how to use HTML and CSS to create a button with a text reveal effect through before and CSS selectors.
The above is the detailed content of How to Create Text Reveal Effect for Buttons using HTML and CSS?. For more information, please follow other related articles on the PHP Chinese website!