2

I searched and found many examples on web, and even here in stackoverflow, but none of them is 100% explained. For those who wants to place a answer with CSS media screen, please don't do that I know about it. But this question is more for IE7+. since it doesn't support "CSS media screen".

This is the example i found:

$(document).ready(function() {

if (screen.width>=800) {
    $("link[rel=stylesheet]:not(:first)").attr({href : "style1.css"});
    } else {
    $("link[rel=stylesheet]:not(:first)").attr({href : "style.css"});
    }
});

I am new to jquery, so how to change this script in case if I have 4 different styles for 4 different screen resolutions.

So I would liek to have something like this:

  1. if screen > 1 && screen < 600
  2. if screen > 601 && screen < 1280
  3. if screen > 1281 && screen < 1600
  4. if screen > 1 1601

Please help me!

2 Answers 2

5

You don't need jQuery for this, using CSS3 media queries instead:

@media only screen and (max-width : 600px) {
   /* Styles for 1*/
}

@media only screen and (min-width : 601px) and (max-width : 1280px) {
   /* Styles for 2 */
}

@media only screen and (min-width : 1281px) and (max-width : 1600px) {
   /* Styles for 3 */
}

@media only screen and (min-width : 1601px) {
   /* Styles for 4 */
}

Ok, as @RaduChelariu mention, to support older browser like IE7 you can use a custom plugin to make CSS3 media queries compatible with.

Sign up to request clarification or add additional context in comments.

2 Comments

Please before you answer, read question, IE7+ does not support @media
Sorry, RADU explained to me. Pease add "This shiv link" to your answer so that other users will understand this. Thank you
1

Actually, media queries are the answer. Just use this shiv to add media query support to IE7 and 8 and you're good to go. No need for messy javascript.

1 Comment

Glad to have been of help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.