Are you looking for the margin to adjust continuously as the viewport width changes, or do you prefer it to change discretely?
For example, would you like a margin of 12.583vw for widths up to 2250px, and then switch to 29.802vw for larger widths? Or perhaps you'd prefer a margin that increases smoothly in relation to the screen width.
The former option can be achieved using media queries:
@media (width >= 2250px) {
div { margin-left: 29.802vw; }
}
@media (width < 2250px) {
div { margin-left: 12.583vw; }
}
Implementing the latter approach would involve using some form of calc():
body { margin: calc([some function]vw); }
In response to a comment, here's a brief explanation:
In short, JavaScript will be required to calculate and update the margin dynamically.
To linearly interpolate between the specific margin/width pairs provided, we need to determine the slope and intercept of the line that correlates margin with screen size, denoted as a and b in the equation margin = a * width + b
.
Given the equations for the desired vw margin values:
12.583 = a * 1351 + b
29.802 = a * 2250 + b
We can solve for a and b:
a ≈ 0.01915
b ≈ 13.284
Therefore, the calculated expression for the margin would be:
calc(((100vw * 0.01915) - 13.284px) * 1vw);
However, this calculation cannot be directly done in CSS due to unit compatibility issues. Instead, JavaScript is utilized to correctly compute and apply the margin adjustments as demonstrated above.