(After searching extensively, I couldn't find the exact same question and answer)
I have a dilemma with displaying YouTube's hqdefault thumbnails on my page. It seems that they are 480 by 360 pixels, resulting in black bars at the top and bottom for all 16:9 ratio videos (which make up the majority).
For example, here is a link to one such thumbnail:
My question is:
I want the image to automatically scale to fit its container's width, which will be a percentage of the total window's width (without knowing the exact pixel value in advance). I also want to hide the black bars without distorting the image's ratio.
Is it possible to achieve this using CSS alone (preferably with good browser support)? I am willing to assume that all images are 16:9, and for those of other ratios, I'm fine with cropping part of the image to display only a portion in 16:9.
Thank you
(PS: Although I have a JS solution, I am curious if this can be accomplished through CSS. The JS solution involves getting the container's width with JS, adjusting it to a 16:9 aspect ratio, stretching the image, centering it, and hiding the excess areas – essentially concealing the black bars at the top and bottom)