^ what Dave said
Essentially, take the smaller of width & height then using transform scale, linearly size up to best possible fit in the centre of the UI div.
The beauty of transform scale is it takes care of mouse events and border widths etc etc and literally zooms in/out.