January 2, 201313 yr 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>
January 2, 201313 yr 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 January 2, 201313 yr by D.Schuster
January 2, 201313 yr 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.
January 2, 201313 yr 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;
January 2, 201313 yr 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>
January 2, 201313 yr 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 January 2, 201313 yr by D.Schuster
January 2, 201313 yr thanks, that didnt quite work, but thanks anyhoo. Much appreciated no? add position:absolute; top:0; left:0; to the body
January 2, 201313 yr 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>
January 2, 201313 yr 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
January 2, 201313 yr Author i need to compress the image more so bare with the load time, but were it is http://www.nurdit.com/andybeeson
January 2, 201313 yr 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 January 2, 201313 yr by D.Schuster
January 2, 201313 yr 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 January 2, 201313 yr by andyl
January 2, 201313 yr 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? 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 January 2, 201313 yr by andyl
January 2, 201313 yr 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 January 2, 201313 yr by andyl
January 2, 201313 yr 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
January 2, 201313 yr 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.
January 2, 201313 yr 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
January 2, 201313 yr 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 January 2, 201313 yr by andyl
January 2, 201313 yr 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