0

Actually I have a css snipped like this:

.item1 {
    background-image: url("../img/myimage1.jpg");
    background-repeat: no-repeat;
    background-position: top center;
    background-size: cover;
}

I will replace this background image with an CDN image url:

background-image: url("https://cdn.com/img/myimage1.jpg");

I would like to make sure that if there is a problem with the CDN provider that an image is loaded anyway. How can I now specify an alternative local image path in addition to the defined CDN url?

Joël A
  • 196
  • 1
  • 2
  • 13

4 Answers4

2

It can be done using javascript. But using your provided CSS codes just add the second to your url something like this:

background-image: url("https://cdn.com/img/myimage1.jpg"), url("https://otherdomain/img/myimage1.jpg");
JustAce
  • 244
  • 1
  • 6
  • 18
1

@JustAce answer would do the job. but it can be done with JS way if your interested in: HTML:

<test onerror="changeImg()" >sdf</test>

JS:

function changeImg(){
    $('.item1').css('backgroud-image','src(".....")')
}
FadedForEver
  • 146
  • 10
0

Just copy the path from the Image from ur Devise in your CSS

Like:

.item1 {
    background-image: url("C:\Useer\Images\my_Image.jpg");
    background-repeat: no-repeat;
    background-position: top center;
    background-size: cover;
}
-1
.item1 {
    background-image: url("../img/myimage1.jpg"), url("https://cdn.com/img/myimage1.jpg");
    background-repeat: no-repeat;
    background-position: top center;
    background-size: cover;
}
  • 4
    A good answer should provide an explanation rather than just a piece of code. Please, try to explain what led you to write this code and how it would help face the issue. – David Aug 22 '19 at 12:25