Open In App

How to spin text on mouse hover using HTML and CSS?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

The spinning of text on mouse hover is known as the Spin Effect or the Rotation Effect. In this effect, each alphabet of the word is rotated along with any one of the axes (preferably Y-axis). Each word is wrapped inside in <li> tag and then using CSS:hover Selector selector we will rotate each alphabet on Y-axis. We will divide this article into two sections, in the first section we will create the basic structure of the text that will spin. IN the second section we will make that text structure spinnable when the user the hover on that.

Creating Structure: In this section we will create the structure by using HTML. 

  • HTML Code: In this we have created an unordered-list and wrap each alphabet inside an list-item(li). 

HTML




<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport"
              content="width=device-width, initial-scale=1.0" />
        <title>Spin Text</title>
    </head>
    <body>
        <ul>
            <li>G</li>
            <li>e</li>
            <li>e</li>
            <li>k</li>
            <li>s</li>
            <li>f</li>
            <li>o</li>
            <li>r</li>
            <li>G</li>
            <li>e</li>
            <li>e</li>
            <li>k</li>
            <li>s</li>
        </ul>
    </body>
</html>


Designing Structure: In this section we will make that structure spinnable and add the little bit of decoration. 

  • CSS Code: First we have provide some basic styling like margin, padding and background. Then we have aligned our list-items horizontally using the float property. Finally, use the hover selector to rotate each alphabet along Y-axis on a particular degree. If you want you can use n-th child property to apply some delay to rotation of each alphabet.The use of n-th child comes to personal preferences and needs so if you feel like using it you can surely go for it. 

CSS




<style>
            body {
                margin: 0;
                padding: 0;
            }
 
            ul {
                padding: 50px;
                margin: 0;
                position: absolute;
                top: 20%;
                left: 25%;
            }
            ul li {
                list-style: none;
                color: green;
                float: left;
                font-size: 40px;
                transition: 0.8s;
            }
 
            ul:hover li {
                transform: rotateY(360deg);
            }
</style>


Final Solution: It is the combination of the above two codes. 

HTML




<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport"
              content="width=device-width, initial-scale=1.0" />
        <title>Spin Text</title>
        <style>
            body {
                margin: 0;
                padding: 0;
            }
 
            ul {
                padding: 50px;
                margin: 0;
                position: absolute;
                top: 20%;
                left: 25%;
            }
            ul li {
                list-style: none;
                color: green;
                float: left;
                font-size: 40px;
                transition: 0.8s;
            }
 
            ul:hover li {
                transform: rotateY(360deg);
            }
        </style>
    </head>
    <body>
        <ul>
            <li>G</li>
            <li>e</li>
            <li>e</li>
            <li>k</li>
            <li>s</li>
            <li>f</li>
            <li>o</li>
            <li>r</li>
            <li>G</li>
            <li>e</li>
            <li>e</li>
            <li>k</li>
            <li>s</li>
        </ul>
    </body>
</html>


Output: 

 



Last Updated : 27 Sep, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads