CSS can only fade out without hiding afterwards

I'm struggling to create a CSS class and animation that will fade out or move a div along with its contents, resulting in the div having both display:none and visibility:hidden

My attempts so far have been unsuccessful! I can either get the animation to work or make it appear as if the div has been "removed"

This simple example showcases the problem

.hide {

    animation-name:fadeOut;
    animation-duration:1s;
    /*visibility: hidden;
    display: none;*/
}

@keyframes fadeOut {
    from {
        opacity: 1;
        margin-left: 0%;
    }

    to {
        opacity: 0;
        margin-left: -100%;
    }
}
<div class="hide">
    <div style="padding:20px;background:orange;">
        <div style="padding:5px;background:azure;">
            My content
        </div>
     </div>
</div>

I also attempted to update the CSS like this:

to {
    opacity: 0;
    margin-left: -100%;
    visibility: hidden;
    display: none;
}

I even tried using https://jsfiddle.net/

As you can see, I have commented out the hiding part in the CSS (even though the opacity hides it).

Do you think it's possible to apply the fadeout effect and then change the visibility and display without resorting to JavaScript?

Answer №1

Ensure that your animation stays on the final (key)frame by adding animation-fill-mode: forwards;. This will prevent it from restarting or refreshing to the beginning.

For more information on animation-fill-mode, visit the link.

An alternative way to code this animation:

.hide {
  animation: fadeOut 1s forwards;
}

.hide {
    animation-name:fadeOut;
    animation-duration:1s;
    animation-fill-mode: forwards; /* added */
    /*visibility: hidden;
    display: none;*/
}

@keyframes fadeOut {
    from {
        opacity: 1;
        margin-left: 0%;
    }

    to {
        opacity: 0;
        margin-left: -100%;
        height: 0; /* added */
    }
}
<div class="hide">
  <div style="padding:20px;background:orange;">
    <div style="padding:5px;background:azure;">
      My content
    </div>
  </div>
</div>

   <div>
 Other content
   </div>


Scrollbar correction

To address the issue with the scrollbar, you can return the hidden element to its initial position using margin: 0; (or the original margin setting):

@keyframes fadeOut {
    0% {
        opacity: 1;
        margin-left: 0%;
    }

    99% {
        opacity: 0;
        margin-left: -100%;
        height: 0;
    }
    100% {
        opacity: 0;
        margin-left: 0; /* added */
        height: 0;
    }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The appearance of new divs will not be seen beneath a fixed header

My webpage has a fixed header image in the first div, but I'm having trouble displaying subsequent divs/sections underneath it. Despite my efforts, I can only see the header image and not the div positions below it. Any idea why this might be happeni ...

Is there a way to extract a particular value from a JSON URL and transfer it to a JavaScript variable?

I am looking to extract current exchange rates from a JSON URL for implementation in a webpage. Specifically, I want to retrieve a particular exchange rate (such as the US Dollar) and store it in a variable for use within a JavaScript function. Below is a ...

Warning: Fastclick alerting about an ignored touchstart cancellation

Having an issue with a double popup situation where the second popup contains selectable fields. Below is the code snippet I am using to display the second popup: $("#select1").click(function(e) { e.stopPropagation(); var tmplData = { string:[& ...

The label element in a CSS form is not displaying correctly

After clicking submit on the form located at this link: I am experiencing an issue where the validation messages are not displaying as intended. I would like them to appear next to the input elements, inline with the form. I suspect that there may be a p ...

How can I personalize the preview feature in a Bootstrap rich text editor?

Currently, I am utilizing Bootstrap's rich text editor by this method $('#editor').wysiwyg();. Utilizing AJAX, I am passing the HTML content created by .wysiwyg to save it in the database. Later on, I plan to retrieve the saved HTML data to ...

How to switch iframe in Python using Selenium without identifying it by ID

As I dive into the world of Selenium with Python, I am faced with the challenge of locating an input element. This task is proving difficult because the input is nested within an iframe. Despite my attempts to switch to the iframe, I am unable to do so as ...

24-hour flatpickr timepicker

Can anyone assist me in setting my hour format to 24 hours instead of AM/PM in Angular? I've been struggling with it for the past 2 days. Below are my TypeScript and HTML code snippets: TypeScript: flatpickrOptions: any = { locale: French, enable ...

Transferring information between pages in PHP

Currently, I am exploring the most effective way to pass data between two pages (Page A to Page B) using PHP for a specific scenario: Page A: This page displays a gallery of images with titles. The PHP file makes a database call to an images table, which ...

linking in Ruby on Rails

I'm facing a bit of confusion with something that may seem minor: Could someone guide me on how to create a hyperlink from one ruby-file.html.erb to another? ...

Is there a way to create a smooth opacity animation for a background image?

https://i.sstatic.net/PuxYm.jpg I am attempting to create a simple animation on my header using pink red lines that I want to make disappear and reappear on a keyframe. However, when I apply my styles, the entire container is animated instead of just the ...

The CSS pointer cursor is not responding when applied to a div element

I'm having trouble changing the cursor type on a div. HTML: <div class='hover-effect'> <label for='file-input'> <img src='http://i.imgur.com/MIY6LmH.png' alt='Upload File' title='Up ...

Incorporate an external CSS file programmatically

Can I apply the SomeStyle.css CSS file to an aspx page from its code behind? ...

What is the best way to make a div that maintains its size even when the content inside is modified?

My goal is to develop a media feed that includes various types of content such as text, images, and videos. To ensure user safety, I want to conceal any explicit posts with a warning that requires the user to click in order to view the content. Although I ...

Using webapp2 to serve a stylesheet in a non-Google App Engine environment

After successfully deploying an app using webapp2/jinja2 and a Paste server, I encountered difficulties serving static stylesheets. I managed to access static files by following this method. Additionally, I implemented a StaticFileHandler that I discovere ...

Spread out the items within an inline block

Is there a way to create space between icons that are in an inline block without them touching each other? One solution could be to add a container around each icon, center the icons within the containers, and then place the containers in the block. You c ...

Unable to alter the background color of the text box

I am currently struggling with my code where I am trying to overlay a text box on an image. Even after setting the background color to white, it still shows up as transparent. I have successfully done this in the past, but for some reason, it is not work ...

Purchase Masonry Supplies for Optimal Arrangement

I've been using a neat little tool called jquery-isotope to create a masonry style layout for the blog posts on my website. It's set up with two columns, showcasing two different post types - long form articles and image-only posts. The masonry ...

The addition of a cancel swipe feature to an HTML5 eBook is causing the text input field for note-taking to malfunction

I am currently working on the development of a process to create HTML5 based eBooks for both desktop and mobile use using Adobe InDesign and exporting them with a plugin called In5. This plugin allows for the incorporation of html, css, and javascript duri ...

I need to condense my layout from three columns to just two columns in HTML

The html code for "Date Accessed" is meant to be in one column as a header, while the actual dates should be in another column, but somehow they are all appearing in a single column. I am having trouble figuring out why. <!DOCTYPE html> {% load stati ...

What are some creative ways to customize and animate the cursor or caret within an input field?

Just to clarify, I am working with React and Material-UI. I have a task at hand where I need to modify the appearance of the caret within an input element by adding an animation to it. I have managed to change its color using caret-color and set a default ...