This might have been asked before and I tried the answer given in this thread: How to get dimensions for a background image when background-size is set to contain?
But in this case, the background-size
is set to cover
, instead of contain
.
The background size is originally 1920x1080
. I have a div-element which basically always scales its width and height to 100%, meanwhile the background-image if the said div is set to cover.
1920 / 1080 gives us a ratio of about 1.7, so whenever the browser widow is scaled like so that the div's width/height ration is over or under 1.7 aka. not in the correct ratio - things break. So I need a way to calculate what is the size of the background image to correctly set things up.
Edit: Here is an example jsfiddle of the situation: https://jsfiddle.net/ahvonenj/35pbovmz/
If you resize the result area, you can see that the result area itself has some width and height, say 400x600. However, this 400 width and 600 height is incorrect for my calculations. I need to know what the size of the background image is. Notice how the background keeps its ratio by basically leaving some of the image out.
The result area width and height are only correct when the result area width/height is exactly the ratio of the background image, which is about 1.7 in this case.
Edit 2: This is now almost working: https://jsfiddle.net/ahvonenj/35pbovmz/