It's often forgotten that the vertical margin
percentages of elements are based on their containing block's width
, not just the horizontal ones.
This means that if you define margin-top:10%;
, it will create a top margin equal to 10% of its containing block's width.
You can easily observe this phenomenon in action on this jsFiddle. Experiment with resizing the output panel vertically and horizontally, and take note of how the inner block moves up and down.
If your browser supports it, consider utilizing vw
and vh
. However, if these units are unavailable, there's no need to jump straight into Javascript. Many rush to use Javascript without considering potential issues (such as browser resizing). Sometimes, a clever application of CSS can resolve the issue at hand without complicating matters further.