[Solved] CSS Transition doesn’t work with top, bottom, left, right

I have an element with style

position: relative;
transition: all 2s ease 0s;

Then I want to change its position smoothly after clicking on it, but when I add the style change the transition doesn’t take place, instead, the element moves instantly.


However, if I change the color property, for example, it changes smoothly.


What might be the cause of this? Are there properties that aren’t ‘transitional’?

EDIT: I guess I should have mentioned that this is not jQuery, it’s another library. The code appears to work as intended, styles are being added, but transition only works in the second case?

Solution #1:

Try setting a default value for top in the CSS to let it know where you want it to start out before transitioning:


position: relative;
transition: top 2s ease 0s; /* only transition top property */
top: 0; /* start transitioning from position '0' instead of 'auto' */

The reason this is needed is because you can’t transition from a keyword, and the default value for top is auto.

It is also good practice to specify exactly what you want to transition (only top instead of all) both for performance reasons and so you don’t transition something else (like color) unintentionally.

Respondent: xec

Solution #2:

Perhaps you need to specify a top value in your css rule set, so that it will know what value to animate from.

Respondent: sarahholden

Solution #3:

In my case div position was fixed , adding left position was not enough it started working only after adding display block


Respondent: Sameh

Solution #4:

Something that is not relevant for the OP, but maybe for someone else in the future:

For pixels (px), if the value is “0”, the unit can be omitted: right: 0 and right: 0px both work.

However I noticed that in Firefox and Chrome this is not the case for the seconds unit (s). While transition: right 1s ease 0s works, transition: right 1s ease 0 (missing unit s for last value transition-delay) does not (it does work in Edge however).

In the following example, you’ll see that right works for both 0px and 0, but transition only works for 0s and it doesn’t work with 0.

#box {
    border: 1px solid black;
    height: 240px;
    width: 260px;
    margin: 50px;
    position: relative;
.jump {
    position: absolute;
    width: 200px;
    height: 50px;
    color: white;
    padding: 5px;
#jump1 {
    background-color: maroon;
    top: 0px;
    right: 0px;
    transition: right 1s ease 0s;
#jump2 {
    background-color: green;
    top: 60px;
    right: 0;
    transition: right 1s ease 0s;
#jump3 {
    background-color: blue;
    top: 120px;
    right: 0px;
    transition: right 1s ease 0;
#jump4 {
    background-color: gray;
    top: 180px;
    right: 0;
    transition: right 1s ease 0;
#box:hover .jump {
    right: 50px;
<div id="box">
  <div class="jump" id="jump1">right: 0px<br>transition: right 1s ease 0s</div>
  <div class="jump" id="jump2">right: 0<br>transition: right 1s ease 0s</div>
  <div class="jump" id="jump3">right: 0px<br>transition: right 1s ease 0</div>
  <div class="jump" id="jump4">right: 0<br>transition: right 1s ease 0</div>

Respondent: LWChris

Solution #5:

Are there properties that aren’t ‘transitional’?

Answer: Yes.

If the property is not listed here it is not ‘transitional’.

Reference: Animatable CSS Properties

Respondent: Robert Shaw

Solution #6:

I ran into this issue today. Here is my hacky solution.

I needed a fixed position element to transition up by 100 pixels as it loaded.

var delay = (ms) => new Promise(res => setTimeout(res, ms));
async function animateView(startPosition,elm){
  for(var i=0; i<101; i++){
    elm.style.top = `${(startPosition-i)}px`;
    await delay(1);
Respondent: Andre Bradshaw

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.