Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS background-repeat: no-repeat; still repeating

I have...

    html, body {
    background-size: contain;
    background-repeat: no-repeat;
    }

As my CSS and in my HTML I have

<html>
<head> 
<title>Wut</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>

<body>
    <div id="page">
    </div>
   <script type="text/javascript">
        var imgCount = 3;
        var dir = 'img/';
        var randomCount = Math.round(Math.random() * (imgCount - 1)) + 1;
        var images = new Array();
            images[1] = "1.png",
            images[2] = "2.png",
            images[3] = "3.png",
         document.body.style.background = "url(" + dir + images[randomCount] + ")";
    </script>
</body>

For some reason the background which is randomly selected by the JS scrolls and i don't want/need it to. Also as a side note: I managed to make it stop scrolling at one point but when i added background-color, half of the background image was covered up and you couldn't see the background image itself.

Thanks for the help

like image 924
user2378230 Avatar asked Jun 19 '26 04:06

user2378230


2 Answers

The reason why it happens is because background is a composite property which includes background-color, background-image, background-repeat, background-position, background-attachment. It means that when you set background alone without specifying background-repeat you simply overwrite previously defined rule, it just falls back to default value which is repeat. To fix it you should explicitly provide it:

document.body.style.background = "url(" + dir + images[randomCount] + ") no-repeat";

or set background-image insted:

document.body.style.backgroundImage = "url(" + dir + images[randomCount] + ")";
like image 188
dfsq Avatar answered Jun 20 '26 18:06

dfsq


Add this line

document.body.style.backgroundRepeat="no-repeat";

or another way existing line replace this one

document.body.style.background = "url(" + dir + images[randomCount] + ") no-repeat";

HTML

<html>
  <head> 
   <title>Wut</title>
   <link href="style.css" rel="stylesheet" type="text/css">
  </head>

<body>
    <div id="page">
    </div>
   <script type="text/javascript">
        var imgCount = 3;
        var dir = 'img/';
        var randomCount = Math.round(Math.random() * (imgCount - 1)) + 1;
        var images = new Array();
            images[1] = "1.png",
            images[2] = "2.png",
            images[3] = "3.png",
         document.body.style.background = "url(" + dir + images[randomCount] + ")";
         document.body.style.backgroundRepeat="no-repeat";
    </script>
</body>

CSS no need to define

like image 41
Jaykumar Patel Avatar answered Jun 20 '26 17:06

Jaykumar Patel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!