Skip to content
Related Articles

Related Articles

How to spin text on mouse hover using HTML and CSS?
  • Last Updated : 15 May, 2020


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).




    <!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>

Desingning 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.




    <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.




<!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:




My Personal Notes arrow_drop_up
Recommended Articles
Page :