Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.
SignUp Now!
loading="lazy" should work for thatWhat I am looking for is the ability to hide content (preferably using CSS) on mobile and show on desktop, and any images in the hidden content NOT be loaded on mobile, reliably, under any circumstances.I foundloading="lazy"should work for that
<img id="myImage" src="initial_image.jpg" alt="This image has a src attribute and will load">
<img id="myImage" data-src="initial_image.jpg" alt="This image does not have a src attribute and will not load">
<img id="adaptiveImage" data-src="path_to_image.jpg" alt="Alternative Text">
document.addEventListener("DOMContentLoaded", function() {
var imgElem = document.getElementById('adaptiveImage');
if (window.innerWidth > 768) {
imgElem.src = imgElem.getAttribute('data-src');
}
});
@media (max-width: 768px) {
#adaptiveImage {
display: none;
}
}
It does still apply. The browser doesn't consider the visibility of the element when it's fetching resources. The image will be requested and loaded into memory regardless of visibility changes made with CSS.In the past, images hidden with display:none will still get downloaded by some browsers. Is that, in fact, still the case these days?
Thanks for the replies and code snipsIt does still apply. The browser doesn't consider the visibility of the element when it's fetching resources. The image will be requested and loaded into memory regardless of visibility changes made with CSS.
zzzcode.ai
Code Forum is a community platform where coding enthusiasts can connect with other developers, engage in discussions, ask for help, and share their knowledge with a supportive community. It's a perfect place to improve your coding skills and to find a community of like-minded individuals who share your passion for coding.
We use essential cookies to make this site work, and optional cookies to enhance your experience.