Add background image with Javascript - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Image

Description

Add background image with Javascript

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <title>Dynamic Bg image</title> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){/*from  w ww  . j a  v a2s . c  o m*/
 var cards = "http://www.java2s.com/style/download.png";
 var posX = 200;
 var posY = 500;
document.getElementById("cardImg").style.background = "url(" + cards + ") "+posX+"px "+ posY+"px";
    }

      </script> 
   </head> 
   <body> 
      <div id="cardImg" style="width:100px;height:100px;"> 
      </div>  
   </body>
</html>

Related Tutorials