css - Resize a Div Based on Height but Retain Aspect Ratio (almost got it) Strange Reload bug -


there lots of solutions maintaining aspect ratio when resizing videos based on width of parent div. of them depend on fact "padding-top" , "padding-bottom" calculated based on width rather height.

i'm trying similar, want resize video based on height of parent div.

to need create div keeps aspect ratio regardless of height. may sort of lame way it, decided use image because can set height of image appropriate aspect ratio 100% , let height sort out.

i close making work. of now, have been able make inner div want do, except not redraw on window resize. however, if resize window reload, works. ideas?

here's demo (it's not codepen issue i've done locally well)

good thought using image , height: 100%. box same aspect ratio image, doesn't reflow box on window resize whatever reason.

here's demo cleaner css , javascript hack fix reflowing on window resize.

in demo, use 16px 9px data uri image set aspect ratio, preventing needless http request. if need different ratio image, use site convert image data uri: http://dataurl.net/#dataurlmaker

i managed solve reflowing issue basic jquery. on $(window).resize(), toggle class on body makes ratio images go text-align: left text-align: justify tricks browser reflowing box.


browser support

works in chrome, safari, mobile safari, firefox, , ie9+. resizing quite smooth overall, safari stutters bit.

ie6/7 don't support display: inline-block box 100% width , correct height. (not bad fallback!)

ie8 sizes box's height correctly, width wide original image (16px). way found around using html5boilerplate style ie conditional tags set embed parent display: block in this demo


css only

if have have css solution, setting animation on ratio image toggle small amount of padding works in chrome, safari , firefox. however, continually forcing browser re-render page layout, grossly inefficient compared rendering page layout on window resize.

.ratio { animation: myfirst 1s infinite; } @keyframes myfirst {     { padding-top: 1px; }     { padding-bottom: 1px; } } 

hope helps!


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

php - joomla get content in onBeforeCompileHead function -