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.

change background based on screen size

Featured Replies

I have a background image on my site. I want it to view a certain way. Is there a way to relegate it so that the background image only shows up on screen resolutions higher than 1024x768 or that it then shows a different background image? Is there a way to do this without javascript and in CSS?

 

8)

I'm pretty sure the only way to do this is javascript.

 

Use js to tell you screen resolution, then have different css files for different resolutions.

  • Author

then how?

No expert at js, but try this:

 

if (window.innerWidth < 800) {

document.write('<link rel="stylesheet" type="text/css" href="namehere.css">');

}

 

Then in html:

 

<link href="namehere.css" rel="stylesheet" type="text/css" />

<script src="nameofjsfile.js" type="text/javascript"></script>

 

For anyone with a resolution less than 800px across.

 

Could change the inner width to change the resolution. Or add more if statements for more options?

  • Author
:hi: so where do i put the js? Where do i put the HTML? Thanks! 8)
  • Author

thanks! :D8)

Put this in the head of your html:

 

<script language="JavaScript">

<!--

if ((screen.width>=1024) && (screen.height>=768))

{

document.write('<link rel="stylesheet" type="text/css" href="css2filename.css">');

}

 

else

{

document.write('<link rel="stylesheet" type="text/css" href="css1filename.css">');

}

 

//-->

</SCRIPT>

 

Will give people with 1024x768 or more resolution css file 2, and anyone with less than that cssfile1. You can change the values at the top. Did you need more options for stylesheets than that?

 

Hope it helps.

Did it work?

 

Use a case statement like i've written below if you need more options...

 

<script language="JavaScript">

<!--

switch(screen.width)

{

case 1024: document.write('<link rel="stylesheet" type="text/css" href="css2.css">'); break;

case 1280: document.write('<link rel="stylesheet" type="text/css" href="css1.css">'); break;

}

 

//-->

</SCRIPT>

 

add more cases if needed.

  • Author
:p it's not quite working. 8) i have two external style sheets linking in as well. what do you suggest. When i get rid of the external style sheets. The background obviously goes away on anything less than 1024x768 but it goes away on anything bigger too. 8)

What I would do personally would be to have something along the lines of:

 

window.onload = function() {
document.body.className = 'screenRes' + screen.width + 'x' + screen.height;
}

 

in your JS and have separate declarations in your CSS file for the different screen resolutions you want to support:

 

body {
background-image:url(bgDefault.png);
}

body.screenRes800x600 {
background-image:url(bg800x600.png);
}

body.screenRes1024x768 {
background-image:url(bg1024x768.png);
}

 

This way, the JS code is lightweight and the dynamic class name should override the default. Plus, it means you can keep your CSS in a single file :)

  • Author

That's my head right now. 8)

 

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Web Development – Search Engine Placement Service – Online Marketing</title>
<script language="JavaScript">
<!--
switch(screen.width)
{
case 1024: document.write('<link rel="stylesheet" type="text/css" href="css/otdformat-small.css">'); break;
case 1280: document.write('<link rel="stylesheet" type="text/css" href="css/otdformat.css">'); break;
}

//-->
</SCRIPT>
<link href="css/otdformat.css" rel="stylesheet" type="text/css" />
<link href="css/otdlayout.css" rel="stylesheet" type="text/css" />
<!--[if IE]>
   <link rel="stylesheet" type="text/css" href="css/otdIE.css" />
   <![endif]-->
   <![if !IE]>
   <link rel="stylesheet" type="text/css" href="css/otdlayout.css" />
   <![endif]>

</head>
Web Development – Search Engine Placement Service – Online Marketing



  • Author
What I would do personally would be to have something along the lines of:

 

window.onload = function() {
document.body.className = 'screenRes' + screen.width + 'x' + screen.height;
}

 

in your JS and have separate declarations in your CSS file for the different screen resolutions you want to support:

 

body {
background-image:url(bgDefault.png);
}

body.screenRes800x600 {
background-image:url(bg800x600.png);
}

body.screenRes1024x768 {
background-image:url(bg1024x768.png);
}

 

This way, the JS code is lightweight and the dynamic class name should override the default. Plus, it means you can keep your CSS in a single file :)

 

can you give me an example? would i put the first code box in my header? what parts do i need to customize and can you give me an example of how I would customize it?

 

thanks 8)

  • Author
Above is much better - told you I wasn't an expert!

 

[Edit] Not sure why what I said wasn't working for you though...

 

Um but that's not working either.

It should do.. add:

 

<script language="JavaScript">

<!--

window.onload = function() {document.body.className = 'screenRes' + screen.width + 'x' + screen.height;}

 

//-->

</SCRIPT>

 

<link rel="stylesheet" href="mainstylesheetname.css" type="text/css" />

 

into your header

 

then the below into your css.

 

body {background-image:url(bgDefault.png);}

body.screenRes800x600 {background-image:url(bg800x600.png);}

body.screenRes1024x768 {background-image:url(bg1024x768.png);}

 

You dont have to change anything except for the .screenres800X600 etc, which you can change to different values if needed. You can also add more body statements.

 

You will also need to replace the bg urls and css url with your own.

 

These codes are all working on my comp...

I hope Mikec1uk's description cleared things up for you - I figured that it was pretty clear, but obviously not!

 

To cloud things even further, I present the IE expression way to do it :D

 

body {
background-image:expression('url(bg' + screen.width + 'x' + screen.height + '.png)');
}

 

Of course, this is IE only. And probably pointless now I think about it :D

  • Author

OMG! It Works YOU GUYS ROCK! Thank So Much! 8):friends::drinks::hi::yahoo::lol:

My invoice is in the post - just kidding, glad to be of assistance ;)

  • Author

haha :D

 

Thanks for all your help!

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.