On one of my website pages, I always have a Div element with a Background Image that changes based on Media Queries (for example, I fetch a smaller resolution from my CDN on mobile phones).
However, since I retrieve the Image URL on Page Load, I need to set it in CSS. This would work fine for a computed Style property, but those do not support Media Queries.
So far, the only solution I have found is using a Resize Event Listener, but I would prefer a cleaner solution.
Here is an example of how it looks in CSS with a predefined URL:
/* Custom, iPhone Retina */
@media only screen and (min-width : 320px) {
.image {
background: linear-gradient(rgba(255, 255, 255, 0.0), rgba(0, 0, 0, 0.95)), url("https://res.cloudinary.com/.../image/upload/c_scale,w_900/v.../3.jpg") no-repeat center;
background: -webkit-linear-gradient(rgba(255, 255, 255, 0.0), rgba(0, 0, 0, 0.95)), url("https://res.cloudinary.com/.../image/upload/c_scale,w_900/v.../3.jpg") no-repeat center;
}
}
/* Extra Small Devices, Phones */
@media only screen and (min-width : 480px) {
.image {
background: linear-gradient(rgba(255, 255, 255, 0.0), rgba(0, 0, 0, 0.95)), url("https://res.cloudinary.com/.../image/upload/c_scale,w_1300/v.../3.jpg") no-repeat center;
background: -webkit-linear-gradient(rgba(255, 255, 255, 0.0), rgba(0, 0, 0, 0.95)), url("https://res.cloudinary.com/.../image/upload/c_scale,w_1300/v.../3.jp"") no-repeat center;
}
}