Animate the transformation of a singular property (scale) while neglecting the modification of another (translate)

The issue at hand is the complexity of the transform property, which includes various parts like translate, scale, and more.

This is a conceptual query concerning an element, such as .loader, which has transform: translate(10px, 10px) and requires animation on the scale property. In this scenario, the browser may ignore the transform: translate(10px, 10px) and only consider the scale.

I am seeking a workaround for this issue.

Here's an example related to this question. Please note that I am not specifically looking for an answer to this particular case (e.g., wrapping the element or including the translate value in the animation definition), but rather a general solution if one exists.

.loading {
  position: relative;
  width: 100px;
  height: 100px;
  background: #eee;
}
.loading:before,
.loading:after {
  content: "";
  width: 50%;
  height: 50%;
  -moz-border-radius: 50%;
  -webkit-border-radius: 50%;
  border-radius: 50%;
  background-color: #fff;
  opacity: 0.6;
  position: absolute;
  top: 0;
  left: 0;
  /* the broswer doesn't take this */
  transform: translate(100px, 300px);
  -webkit-animation: bounce 2s infinite ease-in-out;
  animation: bounce 2s infinite ease-in-out;
}
.loading:after {
  -webkit-animation-delay: -1s;
  animation-delay: -1s;
}
@keyframes bounce {
  0%, 100% {
    transform: scale(0);
    -webkit-transform: scale(0);
  }
  50% {
    transform: scale(1);
    -webkit-transform: scale(1);
  }
}
<div class="loading"></div>

Answer №1

Typically, when incorporating an animation that alters the transform property, it's important to ensure that the transforms specified in the base element are also included within the animation's keyframes. Essentially, the new transforms introduced by the animation should complement the existing transform rather than completely replacing it. The correct approach is outlined below.

.loading {
  position: relative;
  width: 200px;
  height: 200px;
  background: #eee;
}
.loading:before,
.loading:after {
  content: "";
  width: 50%;
  height: 50%;
  border-radius: 50%;
  background-color: #fff;
  opacity: 0.6;
  position: absolute;
  top: 0;
  left: 0;
  transform: translate(100px, 300px);
  animation: bounce 2s infinite ease-in-out;
}
.loading:after {
  animation-delay: -1s;
}
@keyframes bounce {
  0%, 100% {
    transform: scale(0) translate(100px, 300px);
  }
  50% {
    transform: scale(1) translate(100px, 300px);
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<div class="loading"></div>

I provided a similar response here regarding applying multiple animations on an element where each animation independently affects the transform property's values. I added this link for reference purposes and do not believe them to be duplicates.


In light of the above explanation, including the original transform in every animation's keyframes can be challenging when building animation libraries or separating animations into distinct classes. For instance, if you wish to apply the same bounce animation to various elements with unique initial transform settings, incorporating it directly into the animation's keyframe is unfeasible.

In such scenarios, accomplishing the desired outcome using CSS remains possible but significantly intricate (and almost impossible from my perspective) with one single element.

What alternatives exist? One option is to place the animation on a wrapping element.

.loading-wrapper {
  position: relative;
  width: 200px;
  height: 200px;
  background: #eee;
}
.loading-before, .loading-after {
  position: absolute;
  width: 50%;
  height: 50%;
  top: 0px;
  left: 0px;
  animation: bounce 2s infinite ease-in-out;
}
.loading-before:before,.loading-after:before {
  content: "";
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background-color: #fff;
  opacity: 0.6;
  position: absolute;
  top: 0;
  left: 0;
  transform: translate(100px, 300px);
}
.loading-after {
  animation-delay: -1s;
}
@keyframes bounce {
  0%, 100% {
    transform: scale(0);
  }
  50% {
    transform: scale(1);
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<div class="loading-wrapper">
  <div class="loading-before"></div>
  <div class="loading-after"></div>
</div>


The solution presented is fairly versatile and applicable to a wide range of similar situations. However, a drawback is that stacking multiple transformations may necessitate numerous wrappers. There exists no pure CSS method other than integrating original transformations into the animation's keyframes.

Below is another demonstration snippet.

.move-n-scale {
  position: relative;
  height: 100px;
  width: 100px;
  background: sandybrown;
  border: 1px solid chocolate;
  transform: scale(0.5);
  animation: move 1s linear infinite alternate-reverse;
}
.move {
  position: relative;
  display: inline-block;
  animation: move-only 1s linear infinite alternate-reverse;
}
.scale {
  position: absolute;
  height: 100px;
  width: 100px;
  top: 0px;
  left: 0px;
  background: sandybrown;
  border: 1px solid chocolate;
  transform: scale(0.5);
}
@keyframes move {
  from {
    transform: translateX(0px) scale(0.5);
  }
  to {
    transform: translateX(300px) scale(0.5);
  }
}
@keyframes move-only {
  from {
    transform: translateX(0px);
  }
  to {
    transform: translateX(300px);
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<div class='move-n-scale'></div>
<div class='move'>
  <div class='scale'></div>
</div>

Note: I acknowledge your preference against overly specific solutions like wrapping elements, but I included this answer as it represents the most generic solution available to me. The secondary snippet was shared purely to exemplify its generality.

Answer №2

To remove the translate(100px, 300px); from .loading:after, you can instead place it within the @keyframes section as shown below:

@keyframes bounce {
  0%,
  100% {
    transform: scale(0) translate(100px, 300px);
    -webkit-transform: scale(0) translate(100px, 300px);
  }
  50% {
    transform: scale(1) translate(100px, 300px);
    ;
    -webkit-transform: scale(1) translate(100px, 300px);
  }
}

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

Tips for effectively handling requestAnimationFrame

I created a unique function that both scrambles and translates text. The functionality is smooth if you patiently wait for the animation to finish before moving the mouse over to other elements. However, if you try to rush through to the next one, the prev ...

Tips for positioning the sign up button in the navbar

My expertise lies in HTML, but I'm currently honing my CSS skills. I'm facing a challenge with positioning my sign-up button at the extreme right without it being part of the main navigation link section. Does anyone have suggestions on how to ac ...

Flexbox ancestor causing Bootstrap 5 Carousel width to double

A peculiar issue arises with the Bootstrap 5 Carousel placed inside a Flexbox where it unexpectedly expands in width (pushing other flex-items aside) when transitioning between images. This behavior persists even when the immediate container is set to disp ...

Hover effect transition malfunctioning

I've been attempting to incorporate a hover image effect on my website. I included the script in the link below. However, I'm struggling to achieve a smooth fade transition and a 2-second delay on the hover image. Can someone please assist me wit ...

Header Logo centered at the top

I am using a Bootstrap 4 navbar where the nav-brand is centered on a computer window, but shifts to a dropdown when expanded on mobile. Is there a way to keep the brand fixed at the top and center? Here is my navbar code in HTML: <nav class="navba ...

Why is the lower right white space left unfilled by the color red?

I'm having issues with using named lines in grid for layout. The red section is not positioning correctly next to the footer, and I can't figure out where I went wrong. * { box-sizing: border-box; margin: 0; padding: 0; } .container { ...

How to toggle two classes simultaneously using JQuery

Check out this code snippet: http://jsfiddle.net/celiostat/NCPv9/ Utilizing the 2 jQuery plugin allows for the following changes to be made: - The background color of the div can be set to gray. - The text color can be changed to red. The issue arises wh ...

Solution for displaying as a table cell in Internet Explorer 6 and 7: Workaround

I am currently working on creating a single-row CSS table with multiple cells, aiming to vertically center text within each cell. The desired table layout is as follows: <table width="100%" height="54" border="0" bgcolor="red"> <tr> <t ...

Including a pointer image in an image map

Here is a CSS image map setup that I've been using: HTML: <div style="display:block; width:791px; height:1022px; background:url(images/image.jpg); position:relative; margin:2px auto 2px auto;"> <div><a class="imagemaplink" style="left: ...

Incorporate a smooth infinite scroll feature in a CSS carousel that seamlessly loops without

Looking for a solution to the carousel jerk issue when it reaches the end of the loop? Is there a way to seamlessly loop start without any noticeable interruptions? Here is the code in question. Avoiding the use of JavaScript, is there a method to achieve ...

Looking to transform an HTML table into a stylish CSS layout complete with a form submission button?

Recently, I've been delving into the world of CSS and PHP as I work on converting old code entirely into HTML and PHP with a touch of CSS. The visual aspect seems fine, but I've hit a roadblock with the submit form due to an IF statement in PHP. ...

Integrating blade code within blade code

Struggling to craft a button using the Form builder. {!! Form::button('<span style="color: {!! $colour[$i] !!}" class..') !!} The specific details of the button don't matter, all I wanted was to adjust the font color to $colour[$id]. Th ...

Troubleshooting: Issues with React Material-UI breakpoints not functioning

Trying to create a responsive navbar using Material-UI. The goal is to hide the IconButton between 960px and 1920px, and display it from 0px to 960px. However, it seems to work only below 960px. Here's a snippet of the code for IconButton and ul: ...

Ways to align anchor tags in the middle of a DIV element?

<div style="border: 1px solid rgb(0, 0, 0); height: 30px; width: 30px; background-color: rgb(245, 225, 130);"> <div style="margin: 5px;"> <a href="#link">#down2#</a> </div> </div> This is the current code I am w ...

What is the best way to elegantly display a block containing background and text using Angular and ngAnimate?

I'm a beginner when it comes to JavaScript and Angular. I am attempting to use ng-show and ng-hide for my background and text elements. However, I am experiencing an issue with my text: It smoothly hides, but when it is shown again, the text appears b ...

What could be causing the Bootstrap navbar to not toggle when the button is clicked?

Attempting to create a website featuring a collapsible navbar, but encountering some issues. The button is properly identified with the correct ID assigned. Jquery and bootstrap have been included in the necessary order at the bottom of the body. However, ...

Tips for positioning Bootstrap dropdown menus directly below the selected navigation item

Trying to position the "Big Dropdown" menu directly below the corresponding nav item has been challenging. Despite setting a max-width for the menu, achieving proper alignment has proven difficult. I'm looking for a responsive solution that is clean a ...

What is the best way to implement auto-width columns with flexbox in Bootstrap 4?

My goal was to transition from using bootstrap 3 to bootstrap 4 so that I could achieve uniform-sized divs in a row by utilizing flexbox. I acquired the precompiled files from https://getbootstrap.com/docs/4.6/getting-started/download/. After downloading ...

Paged Content: Turning a Lengthy Text into a Book-Like Format

I have a unique idea for displaying text like a flipping book on a web page. Users can use arrow keys to navigate through the content, giving it an interactive and engaging feel. Instead of focusing solely on page transitions, I am looking into implementi ...

Eliminate a selected option from one dropdown menu that corresponds to another dropdown menu

I have been scouring the internet for a while now, but I still haven't found an answer. On my website, I have three dropdown menus. These dropdowns are used for gathering user preferences so that users can control the output of results. I am wonderi ...