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:

turns out translatex uses element width while left uses viewport/parent element width.
how make translatex use viewport/parent width?
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
Post a Comment