Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS transition width and height from center of div

I have elements with position: absolute; that I want to transition in certain situations. However, the origin of the width and height transition seems to depend on the top/bottom left/right values.

Is there any way to have more control over this?

I am specifically looking to transition from the center of the div.

Is there any solution that doesn't rely on transitioning also the top/bottom left/right values?


Edit:

I want to keep the width and height transitioning.

Thank you for the answers but using Transform scale is not a solution in this case. Percentages in the Transform property refer to the size of the element's border box, not the container. See for example this JSFiddle, how the end result of hovering over the two elements is different.


JSFiddle

div, span {
  width:30%;
  height:30%;
  background:pink;
  transition:all 1s ease;
  position:absolute;
}
*:hover{
  width:10%;
  height:10%;
}
div{
  top:10%;
  left:10%;
}
span{
  bottom:10%;
  right:10%;
}
<div></div>
<span></span>
like image 941
Alvaro Avatar asked Nov 25 '16 11:11

Alvaro


People also ask

How do you add transition to height in CSS?

For animate the "height" of element with CSS Transitions you need use "max-height". If use the "height: auto", the effect not works. Is necessary some value for the CSS create a CSS animate, and you can use "max-height" with a great value for emulate this effect.

How do I make a div auto adjust width?

Using width, max-width and margin: auto; Then, you can set the margins to auto, to horizontally center the element within its container. The element will take up the specified width, and the remaining space will be split equally between the two margins: This <div> element has a width of 500px, and margin set to auto.

Does transition work on height?

We can't transition height , but we can transition max-height , since it has an explicit value. At any given moment, the actual height of the element will be the minimum of the height and the max-height .


1 Answers

Well, you can always use transform - scale for that matter:

div {
  background:pink;
  width:200px;
  height:200px;
  transition:all 1s ease;
}
div:hover{
        -webkit-transform: scale(0.1);
        -ms-transform: scale(0.1);
        transform: scale(0.1);
}
<div></div>
like image 160
Chen Avatar answered Sep 27 '22 17:09

Chen