Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryWeb Content Preparation

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

CSS Background Image Change

A div or web page can have its background image changed with the CSS @media code.

The background image may be switched when the browser width changes — when a phone's orientation is changed for example or when a person on a computer manually adjusts the width of their browser. The background image can also be changed when the page is printed.

No JavaScript is required.

An example is an ad for an app. If the ad is displayed in a browser larger than a certain width, the background is an image suggesting a laptop or desktop computer. When the ad is displayed on a narrower screen, the background image may be one suggesting a cellphone.

Another example is to swap out an elaborate background image to a plain white image or no image at all for printing the page.

In essence, it works like this.

When the page is loaded, this is the default (as an example).

#divid { background-image:url("http://example.com/image1.jpg"); }

When the screen width is, or becomes, less than 500px, the image is switched.

@media screen and (max-width:500px)
{
   #divid { background-image:url("http://example.com/image2.jpg"); }
}

Here is a working installation. When the screen is 500 pixels wide or wider, the background image is the Willmaster logo. When the screen is under 500 pixels wide, the image is a small cartoon quote balloon. Both background images appear in the top-left part of the screen.

[screen text content]

The example removes the background image when the web page is printed.

Here is the CSS:

#article-demo { background-image:url("https://www.willmaster.com/images/wmlogo_icon.gif"); }

@media screen and (max-width:500px) { 
   #article-demo { background-image:url("https://spamfreeform.com/images/SFF-icon-35.png"); } 
}

@media print { 
   #article-demo { background-image:url(); } 
}

The URLs in parenthesis are the URLs to the images in the div. The @media print has no URL in the parenthesis which removes the image during printing.

The Divs with Background Images Willmaster Library article contains information about positioning background images within a div.

When you need to switch (or remove) a background image depending on screen width or page printing, it can be done with the CSS @media code.

This article first appeared with an issue of the Possibilities newsletter.

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC