Open In App

How to apply Transition on CSS Display Property ?

This article will guide the implementation of basic Transition on CSS Display Property, aiming to achieve seamless and aesthetically pleasing transitions when altering the visibility of an element. Upon clicking the button, the image smoothly transitions to create a visually engaging effect.

Opacity and Display Transition Technique

The approach Utilizes both opacity and display properties to achieve a smooth transition effect.

Approach

Example: The example shows transition on CSS using display property using opacity and display transition.






<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,
                                   initial-scale=1.0">
    <title>Opacity and
        Display Transition
    </title>
    <link rel="stylesheet" href="styles.css">
  
</head>
  
<body>
  
    <div class="element">
        <img src=
             alt="gfgimg">
    </div>
  
    <button onclick="toggleElement()">
        Toggle Element
    </button>
  
    <script>
        function toggleElement() {
            const element = document
                .querySelector('.element');
            element.classList.toggle('visible');
        }
    </script>
  
</body>
  
</html>




.element {
    opacity: 0;
    display: none;
    animation: fadeInOut 0.6s ease-in-out;
}
  
.element.visible {
    opacity: 1;
    display: block;
}
  
button {
    margin-left: 10%;
    background-color: #3498db;
    color: #fff;
    padding: 10px 20px;
    font-size: 16px;
    border: none;
    border-radius: 5px;
    cursor: pointer;
    transition: background-color 0.3s ease-in-out,
        color 0.3s ease-in-out;
}
  
button:hover {
    background-color: #2980b9;
    color: #fff;
}
  
@keyframes fadeInOut {
    0% {
        opacity: 0;
        display: none;
    }
  
    50% {
        opacity: 0.5;
        display: block;
    }
  
    100% {
        opacity: 1;
        display: block;
    }
}

Output

Visibility and Display Transition Technique

Use visibility in combination with the display property to create a transition effect.

Approach

Example: The code explains how to Apply Transition on CSS Display Property using the display property.




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, 
                                   initial-scale=1.0">
    <title>Visibility and 
            Display Transition
    </title>
    <link rel="stylesheet" 
          href="styles.css">
      
</head>
  
<body>
  
    <div class="element" 
         id="myElement">
    </div>
  
    <button onclick="toggleElement()">
        Toggle Element
    </button>
  
    <script>
    function toggleElement() {
        const element = document
                        .getElementById('myElement');
        element.classList.toggle('visible');
    }
    </script>
  
</body>
  
</html>




.element {
    width: 200px;
    height: 200px;
    background-color: #ff3a7f;
    opacity: 0;
    visibility: hidden;
    display: none;
    animation: slideInOut 1s ease-in-out;
}
  
.element.visible {
    opacity: 1;
    visibility: visible;
    display: block;
}
  
@keyframes slideInOut {
    0% {
        opacity: 0;
        visibility: hidden;
        transform: translateY(-200px);
    }
  
    50% {
        opacity: 0.5;
        visibility: visible;
        transform: translateY(0);
    }
  
    100% {
        opacity: 1;
        visibility: visible;
        transform: translateY(0);
    }
}
  
button {
    margin-top: 20px;
    background-color: #2ecc71;
    color: #fff;
    padding: 10px 20px;
    font-size: 16px;
    border: none;
    border-radius: 5px;
    cursor: pointer;
    transition: background-color 0.3s ease-in-out,
        color 0.3s ease-in-out;
}
  
button:hover {
    background-color: #27ae60;
    color: #fff;
}

Output

Fade-In and Fade-Out

Example: The example shows how to Apply Transition on CSS Display Property




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, 
                                   initial-scale=1.0">
    <title>Display Transition Example</title>
    <link rel="stylesheet" href="styles.css">
  
</head>
  
<body>
    <h1>GeeksforGeeks</h1>
    <img src=
         alt="gfg"
         class="image">
    <br>
    <button onclick="toggleImage()">
        Toggle To Change
    </button>
    <script src="script.js"></script>
  
</body>
  
</html>




body {
    background-color: rgb(195, 234, 183);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100vh;
    margin: 0;
}
  
h1 {
    color: green;
}
  
img {
    display: block;
    max-width: 100%;
    transition: opacity 0.5s ease-in-out;
}
  
button {
    padding: 1%;
    background-color: rgb(213, 213, 247);
    color: black;
    font-size: large;
    font-weight: bolder;
    cursor: pointer;
}
  
button:hover {
    background-color: rgb(211, 250, 237);
}




function toggleImage() {
    const image = document
                 .querySelector('.image');
    image.style.opacity = 
    (image.style.opacity === '0') ? '1' : '0';
}

Output

 


Article Tags :