Javascript DOM CSS Style backgroundImage Property set

Introduction

Set a background image for a document:

document.body.style.backgroundImage = "url('background1.png')";

View in separate window

<!DOCTYPE html>
<html>
<body>

<h1>Hello World!</h1>
<button type="button" onclick="myFunction()">Set background image</button>

<script>
function myFunction() {/* w  ww  . j  a v a 2s .c o m*/
  document.body.style.backgroundColor = "#f3f3f3";
  document.body.style.backgroundImage = "url('background1.png')";
}
</script>

</body>
</html>



PreviousNext

Related