0

using javascript to calculate; how do I use transform:scale(w,h); to get this div to scale to fit its parent container?

I was in special math group at school so I'm not sure what I am googling (ratio, length comparison, proportion, fraction?) seen as we never learned that stuff (maybe for a day or two).

<div id="container"style="width:164px;height:164px:">

    <div id="square_of_unknown_size"> I want to always fill my parent </div>

</div>

edit: btw, I don't want to use width/height:100% because I have an image inside the square that is fit like that so, i really need to zoom.

this seems to zoom inward to the microscopic level:

'-webkit-transform':'scale('+$('#container').width()+'.'+$('#square_of_unknown_size').width()+','+$('#container').height()+'.'+$('#square_of_unknown_size').height()+')'

px A compare B px get A.B = always fit

1 Answer 1

1

You have to calculate the proportion between the two divs:

var scaleX = $('#container').width() / $('#square_of_unknown_size').width();
var scaleY = $('#container').height() / $('#square_of_unknown_size').height();
$('#square_of_unknown_size').css('-webkit-transform', 'scale(' + scaleX + ',' + scaleY + ')');
Sign up to request clarification or add additional context in comments.

3 Comments

I cant see how its just divide!? and cause my div is always a square shape its like scale(scale+','+scale)
can the effect be reversed?
For example, if the container is 200x200, and the content is 100x100, the scale is 2.0, i.e., the content must be scaled by a factor of 2 to fit the container.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.