Load CSS depending upon your visitors monitor resolution
By admin on Feb 24, 2010 with Comments 0

Mostly all the webpages are designed with 960px in width. This is because majority of the internet population uses monitr that support 1024px resolution.
But if you consider the other part, those who uses monitor of high resolution say 1600.
In those monitors that web page will runs to the center leaving half of the space along right and left side of the site blank.
As we know very well that in a web page, space management is very necesary.
So it will be nice if we could show more content in less space and attract the visitors of our site.
To do that you can use javascript to find the resolution of the your visitor’s monitor and depending on that you can load the css files.
A working example can be found here.
Here is the javascript function to find the resolution.
function findResolution(base,css)
{
var sWidth = screen.width;
var resolution = 1024;
if (sWidth <= 1100) {
resolution = 1024;
}
else if (sWidth > 1100 && sWidth <= 1300) {
resolution = 1280;
slideWidth = 535;
}
else if (sWidth > 1300 && sWidth <= 1500) {
resolution = 1440;
slideWidth = 570;
}
else if (sWidth > 1500) {
resolution = 1600;
slideWidth = 635;
}
css = base+resolution+css;
document.write(‘<link rel=“stylesheet” type=“text/css” href=“‘+ css +’” />’);
}
In this findResolution function the width of the visitor’s monitor is found out and depending on that the resolution is set. And this function accepts to parameters. They are
- base — baseurl of the location of the css files
- name of the css file.
Working:
I am using this script to load my site for 4 different resolutions 1024, 1280, 1440 and 1600. And i have four different css file as 1024global.css, 1280global.css so on for respective resolutions. Now when call the ‘findresolution’ function from your header page.
findResolution(“http://www.yourdomain.com/css/”,“global.css”);
So that this function links the correct css file depending on the resolution. By this you can make your site look different and more attractive in different monitors. And even you can extend this function to make your site load with different contents depending the resolution. i.e, you can have more contents for monitors with high resolution and vice versa.
Filed Under: All Coupons
About the Author:
