top of page
Writer's pictureWeb Coding

How to create a fade in and out transition when the user hovers their mouse over

Today I will be teaching you how to create a fade in and out transition when the user hovers their mouse over for your website using CSS animation


HTML

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <title>Document</title>
 <link rel="stylesheet" href="style.css">
</head>
<body>
 <p>Hello!</p>
</body>
</html>

CSS

*{
 padding0;
 margin0;
}

body{
 min-height100vh;
 displayflex;
 align-itemscenter;
 justify-contentcenter;
 background#4cb69f;
}

p{
 font-size128px;
 colorwhite;
 animation-name: fadeIn;
 animation-duration1s;
 animation-timing-functionlinear;
 animation-fill-modeforwards;
}

p:hover{
 cursorpointer;
 animation-name: fadeOut;
 animation-duration1s;
 animation-timing-functionlinear;
 animation-fill-modeforwards;
}

@keyframes fadeOut{
    0% {opacity1;}
    25% {opacity.75;}
    50% {opacity.5;}
    75% {opacity.25;}
    100% {opacity0;}
}

@keyframes fadeIn{
    0% {opacity0;}
    25% {opacity.25;}
    50% {opacity.5;}
    75% {opacity.75;}
    100% {opacity1;}
}

If you found this blog post helpful don’t forget to check out my other projects and if you want more content follow me on


YouTube: https://www.youtube.com/channel/UC2nxqB2usQIS2c8zwVZKWEQ

Instagram: https://www.instagram.com/coding_for_the_web/

Twitter: https://twitter.com/LearnWebCoding

2 views

Comments


bottom of page