Responsive images as CSS background - Piece of Code - stitcher.io
source link: https://www.stitcher.io/blog/responsive-images-as-css-background
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Scout APM helps PHP developers pinpoint N+1 queries, memory leaks & more so you can troubleshoot fast & get back to coding faster. Start your free 14-day trial today.
« back — written by Brent on June 24, 2017
Responsive images as CSS background
Integrating the Responsive Images spec together with CSS backgrounds, allowing for more flexibility for eg. hero images because you can use background-size: cover;
etc., and still have the full benefits of responsive image loading.
<html>
<head>
<style>
img {
width:100%;
}
img.loaded {
display: none;
}
.responsive-image {
width:100%;
height:500px;
background-size: cover;
background-position: center;
}
</style>
</head>
<body>
<div class="responsive-image">
<img src="./small.jpg" srcset="./large.png 3000w, ./medium.jpg 1920w, ./small.jpg 425w" >
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const images = document.querySelectorAll('.responsive-image');
[].forEach.call(images, function (imageContainer) {
const image = imageContainer.querySelector('img');
image.addEventListener('load', function () {
if (!image.currentSrc) {
return;
}
imageContainer.style['background-image'] = "url('" + image.currentSrc + "')";
image.classList.add('loaded');
});
})
});
</script>
</body>
</html>
Next up: Object oriented generators
Follow me: Twitter — RSS — Newsletter — Patreon — GitHub
© 2020 stitcher.io — Cookies & Privacy
Recommend
-
75
How to render responsive images in an optimal way.
-
15
Introduction Hey everyone, this is Nupur Baghel and Paavini Nanda, from the team “101 Days of Summer”. Both of us are computer engineering undergraduate students from New Delhi, India. We were involved with Ser...
-
8
PHP 7.4 in 7 code blocks By continuing your visit to this site, you accept the use of cookies. Read more. Scout APM helps PHP develope...
-
9
PHP 8 in 8 code blocks By continuing your visit to this site, you accept the use of cookies. Read more. Scout APM helps PHP developers...
-
5
Process forks By continuing your visit to this site, you accept the use of cookies. Read more. Scout APM helps PHP developers pinpoint...
-
9
« back — written by Brent on February 17, 2017 Tackling responsive images - part 1 ...
-
8
Array objects By continuing your visit to this site, you accept the use of cookies. Read more. Scout APM helps PHP developers pinpoint...
-
2
Responsive images as CSS background Scout APM helps PHP developers pinpoint N+1 queries, memory leaks & more so you can troubleshoot fast & get back to coding faster. Start you...
-
6
Official CSS Nesting – the last piece of the puzzle 17.03.2021 0 comments 355 days since last revision. Details have probably ch...
-
5
Checkered background using two lines of code in CSS March 30, 2023 · ⋆ CSS
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK