1

Statement: Making my website Responsive.

What I have done so far: I've tried using @media queries, making external css pages according to resolution.

Problem: I made 5 style sheets; 1024.css, 1280.css, 1366.css, 1440.css, 1920.css . All seem to work fine except the 1024.css! When I load the website in 1024 resolution it automatically turns to mobile-type site. The nav turns into a button, all the elements get dislocated.

What I'm looking for?: I want to load the css style sheets thru javascript. For e.g; I want to be able to load 1280.css when the screen's resolution is 1024px .

I'm a beginner at js. So I can't make advanced functions.

But so far I've prepared this algorithm:

If (max-width = 1024px){replace style.css with 1280.css}

If (max-width = 1366px){replace style.css with 1366.css}

If (max-width = 1440px){replace style.css with 1440.css}

If (max-width = 1920px){replace style.css with 1920.css}

Else if (max-width = 1280px){replace style.css with 1280.css}

PS:I've Searched Google and Stackoverflow and found nothing.

UPDATE: I tried what you said Abhishek Pandey but here is the result . you can check it on http://shalepizza.tk/ Im currently working on the index page.

To check the appropriate resolution, when the page is loaded, press F12 and Ctrl+Shift+M

I link my CSS like this : <link href="/static/1024.css" rel="stylesheet" type="text/css" />

6
  • 1
    Your current approach is the correct one. You'd be better off finding out why it's not working as expected. Commented Sep 5, 2016 at 10:36
  • 1
    why use js - why not use media queries? It's unclear what went wrong when you say you've tried that Commented Sep 5, 2016 at 10:38
  • Try this css-tricks.com/resolution-specific-stylesheets Commented Sep 5, 2016 at 10:42
  • You can load all your css and the media queries will do the job. Loading the main css in js is generaly not an efficient way (inconsistent delay, ..) Commented Sep 5, 2016 at 10:47
  • I'd suggest you to share the relevant parts of the code that's not working (how you include your CSS and may be CSS itself and a link to the result page), probably we can help with fixing them. But anyway, you should specify what are you asking about. Commented Sep 5, 2016 at 10:48

3 Answers 3

4

You can use html instead of js

<link href="/static/1024.css" media="screen and (max-width:1024px)" rel="stylesheet"  type="text/css" />
<link href="/static/1366.css" media="screen and (max-width:1366px)" rel="stylesheet" type="text/css" />

or best option is to use @media queries in css file

/* min size style*/
@media screen and (max-width:320px) {
   /* put your css style in there */
}

/* middle size style */
@media screen and (min-width:321px) {
   /* put your css style in there */
}

/* large size style */
@media screen and (min-width:800px) {
   /* put your css style in there */
}
Sign up to request clarification or add additional context in comments.

Comments

0

May I ask you why you want to use Javascript for that? HTML Has something called Media Queries (Rule: @media), and with that you can specify which CSS it needs to run by a specific size.

A Media query starts with @media. To tell the code that's about the Screen Port, use screen. Finish off with and (min-width:1024px){ and enter your CSS there.

For Example. This is your style.css;

@media screen and (min-width:1024px){
  background-color:#0f0f1f;
}

@media screen and (min-width:2411px){
  background-color:000000;
}

Don't include other css files like this. When you want to do that, you can give the Media attribute in your CSS include.

 media="screen and (min-width:1024px)" 

Comments

0
<body  onresize="myFunction()">
function myFunction() {
            var width = window.outerWidth;
            if (width < 960){
                $('link[href="style1.css"]').attr('href','style2.css');
            }else{
                $('link[href="style2.css"]').attr('href','style1.css');
            }
        }

Comments

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.