Different background image depending on screen resolution?

What do you think is the best way to process background images at different screen resolutions?

I was thinking of creating a separate background for each of the most popular solutions and just loading the appropriate jquery. I am open to CSS, thanks to everyone :)

+6
source share
3 answers

Use multimedia 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 will never be able to cover every possible size, especially when you consider all those users whose browsers are not full-screen, so your design should be somewhat flexible to take this into account.

+16
source

Update:

To support cross-browser, my answer below is to "minimize your" method. However, over the past couple of years, excellent policies have been created to solve this problem. Usually it is a good idea to go with one of them, rather than redo everything that works. Respond.js is one of the most famous. Just a link to it:

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

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


As Roberk noted, 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 requests are not supported in all browsers (cough IE). Media request codes in your css files will simply be ignored by these browsers.

In addition to css media requests, I would think of having a backup javascript solution that will determine the size of the viewport and then just add the class to the body tag. This example uses jquery just for 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, just for easy testing on all devices. Remember that css media requests will be ignored, so in addition to these, you need to set up rules for classes that set javascript:

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

However, you do not want the 2 css rules to collide when both requests and js work, so your media requests should use the same names (and be posted later). For instance:

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

I expose the violin to show it 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.

+3
source

There is a jQuery plugin called "backstretch" that has been stretched the background image to fit the size of the web page. Just look here


If you just want to get a screen resolution, then choose which background will be downloaded, then this code can help:

 var h = screen.height; var w = screen.width; var BGList = { '1024-768': 'background-url-1.jpg', '1152-864': 'background-url-2.jpg', '1280-600': 'background-url-3.jpg' } var myBG = BGList[ w+'-'+h ]; if (myBG){ document.write("<style> body {background:url('"+myBG+"')} </style>") }else{ alert("You have a strange screeen !") //--deal with undefined screen resolution here } 
+1
source

Source: https://habr.com/ru/post/898897/


All Articles