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.

Format/Style an Image that is Loaded by a Script

Featured Replies

Hi I am newbie having issues with a javascript...

 

Background: I am designing an HTML greeter/login screen for MDM display manager on linux mint.

 

Problem: I am using a script to load a random image as the background for the login screen. The script does this perfectly well, however the image is displayed in its native resolution (massive) and is not scaling to fit the screen. My question is this; how do I format/style the images that are loaded by the script? Do I use CSS and if so, how?

 

This is my script (which is stored as an external .js file and loaded with a <script> tag in the main HTML file)

 

function changeImg(imgNumber) {
var myImages = ["bg/bg_1.jpg", "bg/bg_2.jpg", "bg/bg_3.jpg", "bg/bg_4.jpg", "bg/bg_5.jpg", "bg/bg_6.jpg", "bg/bg_7.jpg", "bg/bg_8.jpg", "bg/bg_9.jpg"];
var imgShown = document.body.style.backgroundImage;
var newImgNumber =Math.floor(Math.random()*myImages.length);
document.body.style.backgroundImage = 'url('+myImages[newImgNumber]+')';
}

window.onload=changeImg();

CSS.

 

Firstly make sure the background images are no larger than 500kb each.

 

Size wise, depending on the image, 1600px should suffice in width.

 

Then:

body {
    background-position: 50% 50%;
    background-size: cover;
}

Be aware that background size cover is only supported down to IE9.

 

http://caniuse.com/#search=background-size

 

You can look at other solutions here: http://css-tricks.com/perfect-full-page-background-image/

Edited by CHEWX

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.