Loading CSS Based on Screen Resolution Using Javascript

by Keith Dsouza | Translate | Print
Tuesday, 29th Sep 2009 | Share


Share Loading CSS Based on Screen Resolution Using Javascript on Twitter Share Loading CSS Based on Screen Resolution Using Javascript on Facebook Save Loading CSS Based on Screen Resolution Using Javascript To Delicious Favorites Stumble Loading CSS Based on Screen Resolution Using Javascript Share Loading CSS Based on Screen Resolution Using Javascript on Digg Get Instant Updates as RSS Feeds from Techie Buzz

While working on the redesign of Techie buzz, 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.

techie_buzz_redesign

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.

  1. I separated all the hardcoded width for the layers, and copied it to a separate file.
  2. I then created a copy of the new file, and modified it to work in 1024×728 pixel screen resolution.
  3. I then wrote a conditional Javascript to include the CSS based on the screen resolution of the user.

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://cache.techie-buzz.com/ntc/regular.css" type="text/css" media="screen" />');
    }
    else {
      document.write('<link rel="stylesheet" href="http://cache.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 section.



Share

No Responses so far | Share Your Opinions!

Leave a Comment

Note: We discourage users from using keywords in their names while posting comments, most of them get caught by spam, also it really would be more fruitful in knowing people who comment by their real name, rather than by using a name no one relates to. In future we reserve the right to delete comments from users using a name other than their own.

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>