Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Random Background Loader help

Featured Replies

I have got a random background image loading into the css body tag, but the image isnt inheriting the background properties of the body tag. why would this be?

 

<!DOCTYPE html>
<html>
<head>
<title>Welcome</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
<script>
jQuery(document).ready(function(){
var randomNum = Math.ceil(Math.random()*3); /* Pick Random Number */
jQuery("body").css({'background' : 'url(img/bg' + randomNum + '.jpg)'});
});
</script>
<style>
html {
min-height: 100%;
background-size: cover;
}
body {
margin: 0;
padding: 0;
}
</style>
</head>
<body>
</body>
</html>

have you tried

 

document.body.style,background=''('+Math.floor(Math.random()*randombgs.length)]+')')

 

instead of

 

jQuery("body").css({'background' : 'url(img/bg' + randomNum + '.jpg)'});
});

 

or if you really wanna use body

 

$('body').css({'background-image': 'url(images/' + images[Math.floor(Math.random() * images.length)] + ')'});

 

p.s it's possible to do the same thing with CSS alone

 

http://sonspring.com/journal/easy-random-css-backgrounds

 

saves you the trouble of checking for javascript

Edited by D.Schuster

  • Author

OK I went with the php script and it works fine, the only problem im getting is when using the background-size: cover; its not fitting the whole image to the size of the screen. I want the splash image to fit the screen.

OK I went with the php script and it works fine, the only problem im getting is when using the background-size: cover; its not fitting the whole image to the size of the screen. I want the splash image to fit the screen.

 

you can do that with css

 

background-size:cover;

background-position:center;

  • Author

thanks that does help although one small problem. I seem to be getting on the left side a white line. The image is big (4928 x 3264) but when i take repeat off i get a white line down the left side?

 

<style>
html {
margin: 0;
padding: 0;
min-height: 100%;
}
body {
margin: 0;
padding: 0;
background: url('img/splash/rotator.php') no-repeat;
background-size: cover;
background-position: center;
}
</style>

thanks that does help although one small problem. I seem to be getting on the left side a white line. The image is big (4928 x 3264) but when i take repeat off i get a white line down the left side?

 

<style>
html {
margin: 0;
padding: 0;
min-height: 100%;
}
body {
margin: 0;
padding: 0;
background: url('img/splash/rotator.php') no-repeat;
background-size: cover;
background-position: center;
}
</style>

html{
margin:0;
padding:0;
}

body{
width:100%;
..etc.
}

Edited by D.Schuster

thanks, that didnt quite work, but thanks anyhoo. Much appreciated

 

no?

add position:absolute;

top:0;

left:0;

to the body

  • Author

now the image sits half way UP off the screen.

 

<style>
html {
margin: 0;
padding: 0;
}
body {
margin: 0;
padding: 0;
width: 100%;
background: url('img/splash/rotator.php') no-repeat;
background-size: cover;
background-position: center;
position: absolute;
top:0;
left:0;
}
</style>

now the image sits half way UP off the screen.

 

<style>
html {
margin: 0;
padding: 0;
}
body {
margin: 0;
padding: 0;
width: 100%;
background: url('img/splash/rotator.php') no-repeat;
background-size: cover;
background-position: center;
position: absolute;
top:0;
left:0;
}
</style>

 

everything looks like it should work, may I see the PHP code you've used? and the HTML, better yet link me to the site

i need to compress the image more so bare with the load time, but were it is http://www.nurdit.com/andybeeson

 

troubleshoot it, set the body's background-color:green;

 

see to where the green goes, that's the body of your document, if it goes down below the background-image then everything is okay and it's the pic that's wrong, try something like background-size:contain; instead of cover then

 

if the green stops at the bottom of the image, then the body has somehow reduced it's height

Edited by D.Schuster

I think Schuster's first CSS only suggestion & following post was correct. This seems to work for me in Safari, just added a min-height to stretch it. This won't work in older browsers though, as with background-size: cover.

 

<!DOCTYPE html>
<html>
   <head>
       <title>Welcome</title>
       <style>
       html, body{
           margin:0;
           padding:0;
           min-height:100%;
           width:100%;
       }
       body{
           background: url('/img/splash/rotator.php') no-repeat;
           background-size: cover;
           background-position: center;
       }
       </style>
   </head>

   <body>

   </body>
</html>

Edited by andyl

Have you copied and pasted my code? I get the screen below in Chrome, even with 'lime' as the background colour - can't see a 1px line. Have you tried in IE9 / Firefox?

 

0wSt3.jpg

 

Edit: Out of interest, try:

 

<!DOCTYPE html>
<html>
<head>
	<title>Welcome</title>
	<style>
		html, body{
			margin:0;
			padding:0;
			min-height:100%;
			width:100%;
		}
		body{
			background: url('http://www.nurdit.com/andybeeson/img/splash/rotator.php') no-repeat;
			background-size: cover;
			background-position: 0 0;
		}
	</style>
</head>

<body>
</body>
</html>

 

And if that doesn't work, try:

 

<!DOCTYPE html>
<html>
<head>
	<title>Welcome</title>
	<style>
		html, body{
			margin:0;
			padding:0;
			min-height:100%;
			width:100%;
		}
		body{
			background: url('http://www.nurdit.com/andybeeson/img/splash/rotator.php') no-repeat;
			background-size: cover;
			background-position: -1px 0;
		}
	</style>
</head>

<body>
</body>
</html>

Edited by andyl

Hmm, I only get a line with my second snippet above.

 

The below works perfectly for me in Chrome/Safari in OS X - try fully refreshing (CTRL+SHIFT+R)?

 

<!DOCTYPE html>
<html>
   <head>
       <title>Welcome</title>
       <style>
           html, body{
               margin:0;
               padding:0;
               min-height:100%;
               width:100%;
           }
           body{
               background: url('http://www.nurdit.com/andybeeson/img/splash/rotator.php') no-repeat;
               background-size: cover;
               background-position: 0 0;
           }
       </style>
   </head>

   <body>
   </body>
</html>

 

Edit: It's a Windows specific problem for Chrome, see this thread. Haven't found a work around yet.

Edited by andyl

  • Author

the only way iv worked around it is to add a background of a dark color to mask it ever so slightly. thanks andy

the only way iv worked around it is to add a background of a dark color to mask it ever so slightly. thanks andy

 

No probs, bit of a nuisance but it's not too noticeable.

 

You got a link to try this in chrome

 

Try this tutorial demo (demo #1) in Chrome on Windows. Same bug, right next to the scrollbar on the right, there is a 1px line where the image doesn't meet the browser width: http://css-tricks.com/perfect-full-page-background-image/

 

It's definitely a Chrome/Windows problem.

 

Try this tutorial demo (demo #1) in Chrome on Windows. Same bug, right next to the scrollbar on the right, there is a 1px line where the image doesn't meet the browser width: http://css-tricks.co...ckground-image/

 

It's definitely a Chrome/Windows problem.

 

 

No 1px gap here perhaps doesn't affect chrome & xp

No 1px gap here perhaps doesn't affect chrome & xp

 

Are you sure it's not there? It's very hard to tell against Windows scrollbars.

 

Perhaps it's a special treat for Windows 7 users ;)

 

Edit: dpuk, I was pointing out that the bug occurs even on that tutorial - so I wouldn't worry about it too much. That said, the tutorial is useful for what you're trying to achieve.

Edited by andyl

  • Author

Andy and others,

 

Iv managed a good work around by using the CSS tutorial provided by Andy and a bit of php in an img tag to randomize the images on page load. Thanks alot for your help on this topic, but of a head scratcher but at least we no there is a bug out there.

 

<!DOCTYPE html>
<html>
<head>
<title>Welcome</title>
<style>
img.bg {
/* Set rules to fill background */
min-height: 100%;
   min-width: 1024px;

/* Set up proportionate scaling */
   width: 100%;
   height: auto;

/* Set up positioning */
   position: fixed;
   top: 0;
   left: 0;
}
@media screen and (max-width: 1024px){
img.bg {
 left: 50%;
    margin-left: -512px;
}
}
</style>
</head>
<body>
<img src='img/bg<?php echo rand(1,2); ?>.jpg' class="bg" />
</body>
</html>

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.