I'm working on a site which has a series of 'hero' panels running down the home page which have text overlaid on large background images.
Ideally I'd like to use inline images with srcset
and sizes
so the browser can choose the most appropriate image based on the screen size, rather than just whacking in the largest possible image as a background-image
and then scaling it down for smaller screens.
So far my markup looks like:
<img
src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=="
srcset="/img/trekking_320.jpg 320w,
/img/trekking_480.jpg 480w,
/img/trekking_600.jpg 600w,
/img/trekking_768.jpg 768w,
/img/trekking_960.jpg 960w"
data-sizes="100vw"
class="lazyload"
>
and CSS:
img {
position: absolute;
height: 100%; width: auto;
}
and overflow:hidden
on the container.
The height of the images is 320px up to 768px, then 480px up to 960px and then a max-height of 600px after that.
I've made a Codepen to illustrate the problem. Everything works fine on retina screens at all different screen sizes (mobile, tablet, laptop) and on a normal dpi screen it's fine too up to 768px wide, but after that the images don't fill the screen.
So is it possible to do all the things I've asked in the title? Am I on the right track or do I need to take a completely different approach?