HTMLfixIT.com JavaScript swap stylesheet based on screen resolution tutorial.





How to swap stylesheets based on screen width using JavaScript.

1. Place the following in its entirety between the head tags of your page.


2. Create the stylesheets.

Create a default stylesheet for the page, (called default.css) and then make 3 copies of it, call them test1.css, test2.css and test3.css. Edit the 3 testX stylesheets so that the first one (test1.css) is suited to resolutions at or below 640x480, the second (test2.css) to suit resolutions at or below 1024x768, and finally edit the last one so that it suits resolutions above 1024x768. Save the finished stylesheets into the same directory called css that is located in the directory containing the page you want to swap stylesheets.

3. Set the code to start when the page loads.

Replace your body tag with the following:


Congratulations!

If you did all that correctly, your page should now change stylesheets based on your screen resolution. If you want to offer people an option box by which they can select which stylesheet to run manually, here are some additional steps you should take.

Add this javascript code inside the head tags of your page.


Place this select box somewhere within your body tags.


The end result will look like this. (select one, it works.)

If you are unsure of any of the above steps, view the source code of this page as it has all the relevant bits in the relevant places.

Regards

Franki