Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS margin-left transition not working

I'm trying to do a transition from the center to left and reduce the height of an image. The height transition is working fine, but the margin just teleports to the left instead of animating.

this is my code:

#logo_img {
    height: 55px;
    width: 55px;
    background-color: red;
    margin-left: auto;
    margin-right: auto;
    display: block;
    transition: all 1s ease-in-out;
}

#logo_img.tiny {
    height:45px;
    margin-left: 0;
}

JS:

$('#logo_img').addClass('tiny');

working example: http://jsfiddle.net/v0w6s3ms/1/

any help?

like image 644
DarkW Avatar asked Jun 02 '15 05:06

DarkW


People also ask

What is the default value of the left margin in CSS?

Specifies a fixed left margin in px, pt, cm, etc. Default value is 0px. Negative values are allowed. Read about length units Sets this property to its default value.

When does the transition effect start in CSS?

The transition effect will start when the specified CSS property (width) changes value. Now, let us specify a new value for the width property when a user mouses over the <div> element:

What is a transition property in CSS?

Special welcome offer: get $100 of free credit . The transition property is a shorthand property used to represent up to four transition-related longhand properties: transition properties allow elements to change values over a specified duration, animating the property changes, rather than having them occur immediately.

How do I know when a style is transitioning?

That is, when the styles are changed (e.g. on hover on), they properties will transition, and when the styles change back (e.g. on hover off) they will transition. For example, the following demo transitions on hover, but not on hover off: This browser support data is from Caniuse, which has more detail.


2 Answers

You can't animate auto property instead try something like this

$(function() {
  setTimeout(function() {
    $('#logo_img').addClass('tiny');
  }, 1000);
});
#logo_img {
  height: 55px;
  width: 55px;
  background-color: red;
  margin-left: calc(50% - 55px);
  margin-right: auto;
  display: block;
  transition: all 1s ease-in-out;
}
#logo_img.tiny {
  height: 45px;
  margin-left: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section id="logo_img"></section>
like image 198
Akshay Avatar answered Sep 21 '22 11:09

Akshay


You want to transition from "margin-left:auto" to "margin-left:0". Auto is not a defined value, thats why it can't be decreased to zero. Set margin-left: 50% instead "auto".

like image 24
Pvb Avatar answered Sep 23 '22 11:09

Pvb