Loading CSS Based on Screen Resolution Using Javascript
Posted By Keith Dsouza On September 29, 2009 @ 6:43 pm In How To | 5 Comments
While working on the redesign of Techie buzz [1], I had several things in mind that I wanted to include in the design. One of the things was to make the design widescreen friendly by making use of a larger content area.

Throughout the design process, I asked several team members to test the design on different browsers. However, all the testers had one thing in common, they were all using widescreen monitors.
Once I rolled out the new design, one of my friends and fellow bloggers Deepak Jain sent me a message saying that the design was broken on his PC. On further investigation, I found that he was using a monitor with a max resolution of 1024×768 pixels. The new design did not particularly work well on those resolutions.
Now I personally did not want to change the width to just fit properly in 1024×768 resolutions. To overcome that I came up with a much better solution.
Doing this made the them work properly on both 1024×76 pixel and higher screen resolutions.
Here is the code that can be used to include CSS based on different resolutions.
<script type="text/JavaScript">
var screenwidth = screen.width;
if (screenwidth <= 1024) {
document.write('<link rel="stylesheet" href="http://cdn2.techie-buzz.com/ntc/regular.css" type="text/css" media="screen" />');
}
else {
document.write('<link rel="stylesheet" href="http://cdn2.techie-buzz.com/ntc/widescreen.css" type="text/css" media="screen" />');
}
</script>
Have fun with your next design. If you want more such useful and interesting tips, don't forget to visit the Webmaster Tips [2] section.
Article printed from Techie Buzz: http://techie-buzz.com
URL to article: http://techie-buzz.com/how-to/include-different-css-for-different-screen-resolutions.html
URLs in this post:
[1] redesign of Techie buzz: http://techie-buzz.com/annoucements/new-techie-buzz-theme.html
[2] Webmaster Tips: http://techie-buzz.com/tag/webmaster-tips
Click here to print.
Copyright © 2006-20011 Techie Buzz. All rights reserved.