Tips for preventing CSS animation from reverting back to its initial position

After transforming 2 div tags into blocks, I created a code that causes the blocks to move outwards. The issue I'm facing is that they return to their original position after moving. I want the two blocks in the animation to move out to the sides and stay there without returning to the center.

:)

.dark1 {
  background-color: black;
  height: 100vh;
  position: absolute;
  left: 0px;
  top: 0px;
  width: 50%;
  animation: example1 5s;
}

.dark2 {
  background-color: red;
  height: 100vh;
  position: absolute;
  top: 0px;
  right: 0px;
  width: 50%;
  animation: example 5s;
}

@keyframes example {
  50% {
    right: -500px;
    top: 0px;
  }
}

@keyframes example1 {
  50% {
    left: -500px;
    top: 0px;
  }
}
<!DOCTYPE html>
<html>

<body>
  <div class="dark1"></div>
  <div class="dark2"></div>

</body>

</html>

Answer №1

The animation is reversing due to the 50% specified in the keyframe definition, combined with the default animation-fill-mode of none, causing the animation to reset at its conclusion.

To prevent this reversal behavior, consider defining the keyframes at 100% (or using to) and applying an animation-fill-mode of forwards or both:

.dark1 {
  background-color: black;
  width: 50px;
  height: 100vh;
  position: absolute;
  left: 0px;
  top: 0px;
  width: 50%;
  animation: example1 5s;
  animation-fill-mode: both;
}

.dark2 {
  background-color: red;
  width: 50px;
  height: 100vh;
  position: absolute;
  top: 0px;
  right: 0px;
  width: 50%;
  animation: example 5s;
  animation-fill-mode: both;
}

@keyframes example {
  100% {
    right: -500px;
  }
}

@keyframes example1 {
  100% {
    left: -500px;
  }
}
<!DOCTYPE html>
<html>

<body>
  <div class="dark1" gt;</div>
  <div class="dark2"></div>

</body>

</html>

Answer №2

try implementing animation-fill-mode: forwards

.dark3 {
  background-color: blue;
  width: 50px;
  height: 100vh;
  position: absolute;
  left: 0px;
  top: 0px;
  width: 50%;
  animation: example2 5s;
  animation-fill-mode: forwards
}

.dark4 {
  background-color: green;
  width: 50px;
  height: 100vh;
  position: absolute;
  top: 0px;
  right: 0px;
  width: 50%;
  animation: example3 5s;
  animation-fill-mode: forwards
}

@keyframes example2 {
 100% {
    right: -700px;
    top: 0px;
  }
}

@keyframes example3 {
  100% {
    left: -700px;
    top: 0px;
  }
}
<body>
  <div class="dark3"></div>
  <div class="dark4"></div>

</body>

Answer №3

One way to achieve this is by using a single element and writing less code.

.box {
  background: linear-gradient(black 0 0) 0 0 / 50% 100% no-repeat, 
              linear-gradient(red 0 0) 100% 0 / 50% 100% no-repeat;
  height: 100vh;
  animation: example 5s forwards;
}

@keyframes example {
  to {
    background-size: 25% 100%;
  }
}
<div class="box"></div>

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

Expanding divisions vertically without the constraints of fixed heights

Instead of resorting to fixed heights and absolute positioning with top: and bottom:, I'm challenging myself to find a CSS solution without using those methods. I want to explore different ways to solve this problem and enhance my skills. I have a st ...

Position an element to the right inside its parent container with varying width in Internet Explorer 7 or earlier

I am trying to align a button to the right edge of a table inside a container-div that has no set width. The goal is to have the button float on the right side of the table, lining up with its right edge. While my current approach works in most browsers, i ...

What steps should I take to create a slider using my image gallery?

I have a unique photo gallery setup that I'm struggling with: As I keep adding photos, the cards in my gallery get smaller and smaller! What I really want is to show only 4 photos at a time and hide the rest, creating a sliding effect. I attempted ad ...

implement a jQuery loop to dynamically apply css styles

Attempting to utilize a jQuery loop to set a variable that will vary in each iteration through the loop. The plan is for this variable to be assigned to a css property. However, the issue arises where every css property containing the variable ends up with ...

Tips for changing a fullscreen HTML5 video appearance

I discovered a way to change the appearance of a regular video element using this CSS code: transform: rotate(9deg) !important; But, when I try to make the video fullscreen, the user-agent CSS rules seem to automatically take control: https://i.sstatic. ...

CSS tip: Display only the latest x entries on a list

Can CSS be used to display only the most recent x items in a list? The code below reveals every item except the first two. How can I make it so that only the last two items are displayed in the list? ul li { display: none !important; } ul li:nth-las ...

Using a zoom background image in an HTML document without any accompanying text

I am trying to create a zoom effect on a background image without affecting the text overlaying it. Here is the CSS code: .page1-box { width: 1200px; height:800px; overflow:hidden; } .page1 { width: 100%; height: 100%; background: ...

Enhancing 2D video viewing with Threejs interactivity

I want to create an interactive 2D video using three.js and maintain the aspect ratio of the video when resizing the browser window. Here is the code I am currently using: var camera, scene, renderer; var texture_placeholder, distance = 500; init() ...

CSS and HTML modal not closing

I've been working on creating a custom popup using HTML, CSS, and some jQuery functions. My idea was that when I click on the main div, it should expand in size and display a cancel button, which it does successfully. However, the issue arises when tr ...

The background color of social media links spills over onto other links, creating a messy overlap

I have been trying to confine the teal hover effect within the boundaries of my social media links. Despite adjusting padding, heights, and widths using CSS properties, the hover effect still extends beyond the right border. Here is how it appears without ...

Minimize the gap between icon and text on paper-icon-item in Polymer

Is there a way to decrease the spacing between the icon and text in paper-icon-item? I'm looking for a solution to this issue. https://i.sstatic.net/kZ9sy.png I attempted the following: paper-icon-item { --paper-item-icon: { padding-right: 0; ...

Enable a single column to scroll until the content is fully displayed, and then stay in a fixed position

My HTML content consists of two columns, with the first column being a sidebar that should have limited content. The second column holds the main content and can span several pages. The desired functionality is for the first column to scroll until the end ...

- "Queries about Javascript answered with a drop-down twist

Having some trouble with setting up a straightforward FAQ dropdown feature. Could someone lend a hand and see what might be going wrong? Appreciate your help! CSS #faqs h3 { cursor:pointer; } #faqs h3.active { color:#d74646; } #faqs div { height:0; o ...

What is the formula for determining the REAL innerWidth and innerHeight?

I am currently working on a responsive website using Bootstrap and I am looking to create an element that perfectly fits the screen size. When I set the height/width to 100%, the browser includes the toolbar, other tabs, and the Windows taskbar in the cal ...

What is the reason for min-content not being compatible with auto-fill or auto-fit?

My confusion lies in the fact that this code snippet works: .grid { display: grid; grid-template-columns: repeat(4, min-content); } Whereas this one does not: .grid { display: grid; grid-template-columns: repeat(auto-fill, min-content); } I am ...

Center the text within the div and have it expand outwards from the middle if there is an abundance of text

I have a div that is in the shape of a square box and I want it to be centered. No matter how much text is inside, I would like it to remain in the middle. I am using JQuery to create my square box and would like to center it using CSS. Here is my code: &l ...

Incorporating Google fonts into email designs

I am struggling to get Google fonts to display properly in my newsletter emails. Here is the code snippet I have been using: $html=" <html> <style> @import url(http://fonts.googleapis.com/css?family=Lato|Simonetta); </style> <body& ...

Displaying a banner at the bottom of a React page

I need to display a banner with the text "All items..." on every page. Currently, I have set the position attribute to fixed, but the banner is aligned to the left and I want it to be centered horizontally instead. Below is my code snippet: export const Ba ...

javascript - determine whether a hyperlink has been accessed

Is there a method to determine if a link has been visited? In Firefox, the color of a link changes after clicking on it, which leads me to believe it is possible. Edit: This question pertains to a Firefox extension, so I am unable to modify the HTML or CS ...

Is there a way to apply border-radius to the header of a table in Bootstrap 5?

My use of Bootstrap 5 for styling a table has encountered an issue. Even with the border-radius set on the table-dark class, the thead element's border does not change. I am looking for a way to address this problem. Any suggestions? .table-dark { ...