Open In App

How to Zoom an Element on Hover Using CSS ?

Last Updated : 10 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Zooming an element on hover using CSS involves creating an effect where an element, such as an image or button, increases in size when the user hovers over it. This interactive technique enhances user engagement by highlighting elements through smooth scaling animations.

Approach

  • HTML Structure: Uses a <div> element with the class .sudo to apply the zoom effect.
  • Base Styling: The .sudo class sets padding, background color, size, and border-radius for the element.
  • Transition Setup: Adds transition: transform .2s for smooth animation when the hover effect is triggered.
  • Hover Effect: The .sudo:hover class uses transform: scale(1.5) to zoom the element to 1.5 times its size.
  • Cross-Browser Compatibility: Includes -ms-transform and -webkit-transform for support in older versions of IE and Safari.

Example: Here we are following above explained approach.

html
<!DOCTYPE html>
<html>

<head>
    <title>
        How to zoom an element
        on Hover using CSS ?
    </title>
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <style>
        * {
            box-sizing: border-box;
        }

        .sudo {

            padding: 50px;
            background-color: red;
            transition: transform .2s;
            width: 160px;
            height: 160px;
            margin: 0 auto;
            border-radius: 29px;
        }

        .sudo:hover {
            -ms-transform: scale(4.6);
            /* IE 9 */
            -webkit-transform: scale(1.5);
            /* Safari 3-8 */
            transform: scale(1.5);
        }
    </style>
</head>

<body>
    <center>
        <h1>
            GeeksForGeek
        </h1>

        <h2>
            How TO – Zoom on Hover
            in inline CSS
        </h2>

        <div class="sudo">
            Hover on Me
        </div>
    </center>
</body>

</html>

Output:

z




Similar Reads