Skip to content

Instantly share code, notes, and snippets.

@Andrii-Yukhymenko
Created July 29, 2021 19:14
Show Gist options
  • Save Andrii-Yukhymenko/aca49d0b2b7abee65dfe52ee19c83ced to your computer and use it in GitHub Desktop.
Save Andrii-Yukhymenko/aca49d0b2b7abee65dfe52ee19c83ced to your computer and use it in GitHub Desktop.
ibg
<!--CSS-->
.ibg{
background-position: center;
background-size: cover;
background-repeat: no-repeat;
position: relative;
}
.ibg img{
width: 0;
height: 0;
position: absolute;
top: 0;
left: 0;
opacity: 0;
visibility: hidden;
}
<!--JS-->
function ibg(){
let ibg=document.querySelectorAll(".ibg");
for (var i = 0; i < ibg.length; i++) {
if(ibg[i].querySelector('img')){
ibg[i].style.backgroundImage = 'url('+ibg[i].querySelector('img').getAttribute('src')+')';
}
}
}
ibg();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment