-1

i have searched google and on here but i cant exactly find a correct answer to my problem i used this code:

html {
   background: url('../resources/imgs/bgs/mainbg.jpg') no-repeat center center fixed; 
   -webkit-background-size: cover;
   -moz-background-size: cover;
   -o-background-size: cover;
   background-size: cover;
}

that i got from the internet to set a background image that is always in the center and fits perfecly in the browser and it works great but i ran into a problem. i need to change the background image on a button click but i dont know how to target the html tag is it even possible? many thanks :)

my question was flagged as a duplicate but my problrm is not just getting the style of the html elememnt but changing it the same as the code snippet using only pure javascript

3
  • Possible duplicate of How to get the <html> tag HTML with JavaScript / jQuery? Commented Jul 15, 2018 at 14:30
  • "javascript get html element" typed into Google could have led you to that duplicate directly. Commented Jul 15, 2018 at 14:31
  • Please include code that you have tried, it gives us more context as to what you are trying to achieve and makes it easier to pinpoint where you might be going wrong. Commented Jul 15, 2018 at 23:31

4 Answers 4

1

You have to set both backgroundImage and backgroundSize property. Try the following:

document.querySelector('#btnSetImage').addEventListener('click', function(){
  var html = document.querySelector('html');
  html.style.backgroundImage = "url('https://www.noodleman.co.uk/images/source/google_merchant_bulk_category_assign/google.jpg')";
  html.style.backgroundSize = 'cover';
});
<button type="button" id="btnSetImage">Set Background Image</button>

Sign up to request clarification or add additional context in comments.

Comments

0

Simply use:

document.getElementsByTagName('html')[0].style.backgroundImage = "url('img_tree.png')";

3 Comments

Too fast answer, I fixed it :)
that didnt work the new image is just placed in the corner and not the whole screen
@kriskotooBG so maybe try: document.getElementsByTagName('html')[0].style.background = "url('img.png') no-repeat center center fixed";
0

With jquery you could do the following

$('html').on('click', function(){
    $('html').css({
      "background": "url('https://picsum.photos/1200/3300/?blur') no-repeat center center fixed",
      "background-size": "cover"
    });
});

2 Comments

sorry but i didnt ask for a jquery solution
then you can just add two different styles with javascript ----document.getElementsByTagName('html')[0].style.background = "url('picsum.photos/1200/3300/?blur') no-repeat center center fixed"; document.getElementsByTagName('html')[0].style.backgroundSize = 'cover';
0

You can access the html element directly with documentElement:

/*Just change the image source with javascript*/
document.querySelector('#changeImage').addEventListener('click', function() {
  document.documentElement.style.backgroundImage = 'url("https://www.fillmurray.com/400/400")';
});
/*Base settings in CSS*/

html {
  background: url('https://www.fillmurray.com/g/400/400') no-repeat center center fixed;
  /*This Would be a greyscale image*/
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
}
<div style="color:#FFF;">Bill Murray!</div>
<!-- Just Some Content -->
<button id="changeImage">ChangeImage</button>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.