This problem has got me at my wit's end.
Imagine a series of divs
lined up horizontally. Each one is sized as a percentage of the total width, but that total width is unknown.
For instance:
---------------------------------
| | | | |
---------------------------------
0% 20% 40% 80% 100%
Now, I need to set a background image that spans across all the divs. However, each div might have different requirements for how the background image should be positioned and its size.
So, the first div (0% - 20%) needs a background-width
of 500%
and a background-position
of (0, 0)
The second div (20% - 40%) also requires a background-width
of 500%
and a background-position
of (25%, 0) (more details on this later)
The third div (40% - 80%) should have a background-width
of 250%
and a background-position
of (~66.66%, 0)
Finally, the last div (80%-100%) will again have a background-width
of 500%
with a background-position
of (100%, 0)
It might seem like a trivial issue, but I'm struggling with calculating the correct values for these background-positions
based on the div's width and position in the strip. Even though I have a rough understanding of percentage positioning, I can't quite wrap my head around it. Any guidance on this matter would be greatly appreciated.
Edit: After some trial and error, I believe I've cracked the code on this. However, before I share my solution, I'd like someone to verify its accuracy because my brain is too fried to grasp the reasoning behind it at this moment. Perhaps after some rest, I'll try to comprehend it myself. Here's how I managed to calculate the correct values:
background_position_x = div_strip_position_x + div_width * div_strip_position_x / (100 - div_width)
(All values are in percentages)