0

I have a < div > element

I know:

width: 500px,

height: 250px,

background-size: "240% 320%" or "1200px 800px",

background-position: -807px -526px,

also I know original image size: { width: 600px height: 400px }

I need to calculate background-position in percents

and not only for this case, I need to do it dynamic, so need a formula

Denis
  • 17
  • 3
  • What if you use % in background position? https://css-tricks.com/almanac/properties/b/background-position/ – bhansa Mar 25 '20 at 10:55
  • Just replace 'px' to '%'? – Denis Mar 25 '20 at 10:58
  • What is the purpose of this requirement? to fit in the image in the `Div`? – Manjuboyz Mar 25 '20 at 11:00
  • No, I need to convert pixels to percentages. It's not to make the image fit anywhere. User himself can dynamically move a picture in the background, I calculated the pixels. Now I need to convert them as a percentage so that when you scale div, the background-image will be scaled too. – Denis Mar 25 '20 at 11:04
  • in the duplicate you will find a section called "Relation between pixel and percentage values" and also at the end "Make percentage behave differently" – Temani Afif Mar 25 '20 at 11:10

0 Answers0