Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different background image depending on screen resolution?

Tags:

jquery

css

what do you think is the best way to handle background images on different screen resolutions?

I was thinking of making a separate background for each of the most popular resolutions and just have Jquery load the appropriate one. I am open to CSS as well, thank you everyone :)

like image 824
pufAmuf Avatar asked Oct 08 '11 20:10

pufAmuf


2 Answers

Use media queries:

@media (min-width:800px) { background-image: url(bg-800.jpg) }
@media (min-width:1024px) { background-image: url(bg-1024.jpg) }
@media (min-width:1280px) { background-image: url(bg-1280.jpg) }

You're never going to be able to cover every single possible size, especially when you consider all those users whose browsers aren't full screen, so your design should be somewhat flexible to account for this.

like image 141
robertc Avatar answered Sep 29 '22 10:09

robertc


Update:

For cross browser support, my answer below is the "roll your own" method. Over the last couple years, however, there have been some excellent polyfills developed to solve this issue. It's usually a good idea to go with one of these rather than redoing all that work. Respond.js is one of the better known ones. Just link to it:

<script src="/path/to/respond.js"></script>

Then write your media queries in your css file and you're done.


As robertc pointed out, css media queries should be your starting point, but it should be noted that they are not a complete solution to this problem. Unfortunately, css media queries are not supported across all browsers (cough IE). The media query code in your css files will simply be ignored by these browsers.

In addition to css media queries, I would consider having a backup javascript solution that will determine the viewport size, then simply add a class to the body tag. This example uses jquery simply for the sake of brevity:

function setImageClass() {
    switch(true) {
        case($(window).width()>600): $("body").removeClass("w200 w400").addClass("w600");
        break;
        case($(window).width()>400): $("body").removeClass("w200 w600").addClass("w400");
        break;
        default: $("body").removeClass("w400 w600").addClass("w200");
        break;
    }
}

$(document).ready(function() {
    setImageClass();
});

$(window).resize(function() {
    setImageClass();
});

Btw, these sizes are not realistic, simply for easy testing on all devices. Remember, the css media queries will be ignored, so in addition to those, you need to set up the rules for the classes that the javascript sets:

.w200 { background:red }
.w400 { background:orange }
.w600 { background:yellow }

However, you don't want the 2 css rules colliding when both the queries and the js work, so your media queries should use the same names (and be placed afterwards). Eg:

@media (min-width:200px) {
    .w200 {
        background:red;
    }
}
...

I put up a fiddle to show this in action. This only includes the javascript solution, but again, I fully support media queries as the main solution. Here is the full screen link.

like image 32
uɥƃnɐʌuop Avatar answered Sep 29 '22 10:09

uɥƃnɐʌuop