Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Idea for CSS-only responsive images using CSS3 generated content and attr() function. Added media query for high-resolution devices. No browser implementation as of May 2011
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>CSS responsive images</title>
<style>
/* Doesn't stop original source image being
downloaded too */
@media (min-device-width:600px) {
img[data-src-600px] {
content: attr(data-src-600px, url);
}
}
@media (min-device-width:600px and -webkit-min-device-pixel-ratio:2,-moz-min-device-pixel-ratio:2,-o-min-device-pixel-ratio: 2/1,min-device-pixel-ratio:2) {
img[data-src-600px-2x] {
content: attr(data-src-600px-2x, url);
}
}
@media (min-device-width:800px) {
img[data-src-800px] {
content: attr(data-src-800px, url);
}
}
@media (min-device-width:800px and -webkit-min-device-pixel-ratio:2,-moz-min-device-pixel-ratio:2,-o-min-device-pixel-ratio: 2/1,min-device-pixel-ratio:2) {
img[data-src-800px-2x] {
content: attr(data-src-800px-2x, url);
}
}
</style>
</head>
<body>
<img src="image.jpg"
data-src-600px="image-600px.jpg"
data-src-600px-2x="image-600px-2x.jpg"
data-src-800px="image-800px.jpg"
data-src-800px-2x="image-800px-2x.jpg"
alt="">
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.