CSS SWIPE EFFECT
How to create this nice effect called CSS swipe effect. when we hover over a button, you can see that the hex-color background is swiped from left-to-right or right-to-left on the link until it covers the whole thing.
Let's start🙂:
-
HTML Structure:
- Create an anchor tag with the text "hover me."
<a href="#">hover me</a>
-
CSS Styles:
- Style the link:
- Remove text decoration.
- Set the text color, font, size, border, and padding.
- Set the position to relative.
- Add a transition for all properties over .6 seconds.
- Style the link:
a{
font-size: 1.2rem;
display: inline-block;
text-decoration: none;
text-transform: uppercase;
border: .1rem solid;
padding: 1.5rem 3rem;
position: relative;
overflow: hidden;
transition: all .6s;
}
-
Swipe Effect:
- Create a
::before
pseudo-element:- Make the content empty.
- Set
position: absolute
,left: 0
,top: 0
. - Set
height: 100%
,width: 100%
, and a background color (e.g., the same color as the link text). - Set
z-index
to -1 to position it behind the link text. - Initially, use
transform: translateX(-100%)
to hide it to the left (negative value moves left).
- Create a
a::before{
content: '';
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
background-color: rgb(5, 0, 12);
z-index: -1;
transition:all .5s;
transform: translateX(-100%);
}
-
Hover Effect:
- On hover over the link, change the
::before
pseudo-element:- Use
transform: translateX(0)
to bring it back to its original position (smooth transition). - Change the text color to white (or any desired color).
- Use
- On hover over the link, change the
a:hover{
color: white;
}
a:hover::before{
transform: translateX(0);
}
-
Customize the Effect:
- To change the direction of the swipe:
- For right-to-left swipe, set
transform: translateX(100%)
. - For top-to-bottom swipe, use
transform: translateY(-100%)
.
- For right-to-left swipe, set
- To change the direction of the swipe:
Feel free to experiment with these values to create various swipe hover effects.
a{
font-size: 1.2rem;
display: inline-block;
text-decoration: none;
text-transform: uppercase;
border: .1rem solid;
padding: 1.5rem 3rem;
position: relative;
overflow: hidden;
transition: all .6s;
}
a::before{
content: '';
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
background-color: rgb(5, 0, 12);
z-index: -1;
transition:all .5s;
transform: translateX(-100%);
}
a:hover{
color: white;
}
a:hover::before{
transform: translateX(0);
}
See Demo: Codepen