Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scale an element when it loads using only CSS?

I'm loading an element that has the initial css values of :

.popOver {
  height: 100%;
  width: 100%;
  position: fixed;
  background-color: #d9dfe5;
  transition: all 2s ease-in-out; 
  transform: scale(0,0); 
}

I need to change to scale(1, 1) when the element loads in the page and see the transition. Anyone can help?

like image 917
Adam Boostani Avatar asked Jan 26 '15 06:01

Adam Boostani


People also ask

How do you scale elements in CSS?

scale() The scale() CSS function defines a transformation that resizes an element on the 2D plane. Because the amount of scaling is defined by a vector, it can resize the horizontal and vertical dimensions at different scales. Its result is a <transform-function> data type.

How do you scale right in CSS?

By default the transform-origin is 50% 50% , you can reset that to 100% 50% , the first value 100% is x-offset, and the second value 50% is y-offset. To make the div to scale for both width and height, simply change scaleX to scale .

How do you use transition scale in CSS?

CSS syntax example for scale Don't forget to add a transition! Without applying transition, the element would abruptly change sizes. Add the transition to the parent selector (not the hover selector). To make the transition smooth on both hover-over/hover-off.

How do you make things bigger when Hover CSS?

Try moving your mouse over this box: It scales up exactly 1.5 times the original size — so it becomes 50% bigger when you move your mouse over (hover) it. The CSS transform property's scale method can either increase or decrease the size of elements.


1 Answers

transition will apply the moment you load the page so that is not an ideal solution in your situation, what you will need is CSS @keyframes where you need to set scale(0,0) to the class and then scale(1,1) for 100% as keyframes will shoot after the page is completely loaded.

Demo (Refactored the code a bit and added animation-fill-mode to prevent the popup from scaling back to 0 so using rev 2)

.popOver {
    height: 100%;
    width: 100%;
    position: fixed;
    background-color: #d9dfe5;
    -webkit-animation: bummer 2s;
    animation: bummer 2s;
    -webkit-transform: scale(0,0); 
    transform: scale(0,0);
    -webkit-animation-fill-mode: forwards;
    animation-fill-mode: forwards; /* Add this so that your modal doesn't 
                                      close after the animation completes */
}

@-webkit-keyframes bummer {
    100% {
        -webkit-transform: scale(1,1); 
    }
}

@keyframes bummer {
    100% {
        transform: scale(1,1); 
    }
}

Here as I explained before, am setting the initial scale of the element to 0,0 and than am animating it to 1,1 using keyframes. The time of the animation can be controlled by tweaking the 2s which is nothing but 2 Seconds.

like image 151
Mr. Alien Avatar answered Nov 02 '22 23:11

Mr. Alien