javascript - Dynamically set <div> height & margins based on the same <div> width -
i creating responsive site , want adjust height based on it's width. going parallax & responsive site. site's responsiveness, decided have container element adaptive , adjust size based on media queries. then, child have width of 90% or so.
i wanted write either js or of jquery set height & margins of based on new width @ each break point. think easier trying set new "px" height each of these break points. , since want have parallax effects, need set s margins % instead of pixels same effect on multiple devices.
please let me know if of doesn't make sense.
thank much.
if want width , height same, use: width: 100vw;height: 100vw;
here solution, resizable div maintains 16x9 aspect ratio. 9/16*100=56.25%
http://jsfiddle.net/ks2jh/512/
css:
.aspectwrapper { display: block; width: 90%; /* whatever width */ background-image: url('http://placehold.it/160x90'); background-size: cover; background-repeat: no-repeat; } .aspectwrapper::after { padding-top: 56.25%; /* percentage of containing block _width_ */ display: block; content: ''; } html:
<div class="aspectwrapper"> </div>
Comments
Post a Comment