DEV Community

Cover image for Part 1 : Capturing Attention with Motion UI CSS Animations ✨- : Fade In/Out, Slide In/Out, and Scale Up Effects 🫠
Pratik Tamhane
Pratik Tamhane

Posted on • Updated on

Part 1 : Capturing Attention with Motion UI CSS Animations ✨- : Fade In/Out, Slide In/Out, and Scale Up Effects 🫠

Animations can bring a website to life and make user interactions more engaging. In this post, we'll explore three popular CSS animations: Fade In/Out, Slide In/Out, and Scale Up. I'll walk you through how to implement these effects using simple HTML and CSS examples.

1. Fade In/Out Animation

The Fade In/Out animation is perfect for making elements appear and disappear smoothly. It can be used for notifications, modals, or any other elements that need to fade in or out.

Image description

Example Code :

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Fade In/Out Animation</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: #f4f4f4;
        }

        .element {
            padding: 20px;
            background-color: #007bff;
            color: white;
            border-radius: 5px;
            text-align: center;
            animation: fadeIn 1s ease-in-out;
        }

        @keyframes fadeIn {
            from { opacity: 0; }
            to { opacity: 1; }
        }

        @keyframes fadeOut {
            from { opacity: 1; }
            to { opacity: 0; }
        }

        .element.hide {
            animation: fadeOut 1s ease-in-out;
        }

        button {
            margin-top: 20px;
            padding: 10px 20px;
            border: none;
            background-color: #28a745;
            color: white;
            border-radius: 5px;
            cursor: pointer;
        }

        button:hover {
            background-color: #218838;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="element">This element will fade in and out.</div>
        <button onclick="toggleFade()">Toggle Fade</button>
    </div>

    <script>
        function toggleFade() {
            const element = document.querySelector('.element');
            element.classList.toggle('hide');
        }
    </script>
</body>
</html>

Enter fullscreen mode Exit fullscreen mode

How It Works

  • Initial Animation:
    The .element class has a fadeIn animation that gradually makes the element visible.

  • Hide Class:
    When .hide is added, it triggers the fadeOut animation, making the element disappear.

  • Button Click:
    The toggleFade() function adds or removes the .hide class to control the fade effect.

2. Slide In/Out Animation

Image description

The Slide In/Out animation is useful for creating sliding panels or menus. It makes an element slide in from the side and slide out when hidden.

Example Code :

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Slide In/Out Animation</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: #f4f4f4;
        }

        .menu {
            padding: 20px;
            background-color: #007bff;
            color: white;
            border-radius: 5px;
            text-align: center;
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            animation: slideIn 0.5s ease-in-out;
        }

        @keyframes slideIn {
            from { transform: translateX(-100%); }
            to { transform: translateX(0); }
        }

        @keyframes slideOut {
            from { transform: translateX(0); }
            to { transform: translateX(-100%); }
        }

        .menu.hide {
            animation: slideOut 0.5s ease-in-out;
        }

        button {
            margin-top: 20px;
            padding: 10px 20px;
            border: none;
            background-color: #28a745;
            color: white;
            border-radius: 5px;
            cursor: pointer;
            position: absolute;
            right: 20px;
            top: 50%;
            transform: translateY(-50%);
        }

        button:hover {
            background-color: #218838;
        }
    </style>
</head>
<body>
    <div class="menu">Sliding Menu</div>
    <button onclick="toggleSlide()">Toggle Slide</button>

    <script>
        function toggleSlide() {
            const menu = document.querySelector('.menu');
            menu.classList.toggle('hide');
        }
    </script>
</body>
</html>

Enter fullscreen mode Exit fullscreen mode

How It Works

  • Initial Animation:
    The .menu class has a slideIn animation that moves the element from off-screen to its position.

  • Hide Class:
    The .hide class triggers the slideOut animation, sliding the menu out of view.

  • Button Click:
    The toggleSlide() function toggles the .hide class to control the slide effect.

3. Scale Up Animation

Image description

The Scale Up animation enlarges an element smoothly when hovered over. It's often used for buttons or interactive elements to draw attention.

Example Code :

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Scale Up Animation</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: #f4f4f4;
        }

        .button {
            padding: 15px 30px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            transition: transform 0.3s ease-in-out;
        }

        @keyframes scaleUp {
            from { transform: scale(0.9); }
            to { transform: scale(1); }
        }

        .button:hover {
            animation: scaleUp 0.3s ease-in-out;
        }
    </style>
</head>
<body>
    <button class="button">Hover Me</button>
</body>
</html>

Enter fullscreen mode Exit fullscreen mode

How It Works

  • Hover Animation:
    The .button:hover selector applies the scaleUp animation, scaling the button from 0.9 to its normal size.

  • Transition:
    The transition effect makes the scaling smooth and visually appealing.

Conclusion

These CSS animations—Fade In/Out, Slide In/Out, and Scale Up—can enhance the interactivity of your website and make it more engaging. By incorporating these effects, you can create a more dynamic user experience that draws attention to important elements.

Feel free to experiment with the examples and customize them to fit your design needs!

Happy coding! 🚀

shop Link : https://buymeacoffee.com/pratik1110r/extras

Part Title Link
2 Part 2 : Capturing Attention with Motion UI CSS Animations 🎨- : Bounce Animation, Parallax Scrolling, and Hover Animations 🫠 Read

LinkedIn : https://www.linkedin.com/in/pratik-tamhane-583023217/

Behance : https://www.behance.net/pratiktamhane

Top comments (0)