css - get translateX to use viewport width rather that element with. -


i using css left animation , i'm moving on translatex performance reasons. stumbled upon following:

two divs:

<div class="box box1">using css left:20%;</div> <div class="box box2">using css transform: translatex(20%);</div> 

and css:

.box {     width:500px;     background:red;     height:50px;     position: relative; }  .box1 {     left:20%; }  .box2 {     transform: translatex(20%); } 

produces this:

enter image description here

turns out translatex uses element width while left uses viewport/parent element width.

how make translatex use viewport/parent width?

http://jsfiddle.net/vd2nk/

you do:

.box2 {     transform: translatex(20vw); } 

which, combined with

body {     margin: 0; } 

gives same result left: 20%;. note, thought, vw unit (which equals 1/100th of width of viewport) requires modern browser.

here's fixed fiddle: http://jsfiddle.net/vd2nk/6/


Comments

Popular posts from this blog

Why can rails not find a route created by a helper? -

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -