1

I've got the following problem - I'm using a simple JS code to replace CSS file on the fly, depending on the browser window size. It looks like this:

<script type="text/javascript">
//<![CDATA[
<!--
function adjustStyle(width) {
    width = parseInt(width);
    if (width < 701) {
        $("#size-stylesheet").attr("href", "narrow.css");
    } else if ((width >= 701) && (width < 1120)) {
        $("#size-stylesheet").attr("href", "medium.css");
    } else {
       $("#size-stylesheet").attr("href", "wide.css"); 
    }
}

$(function() {
    adjustStyle($(this).width());
    $(window).resize(function() {
        adjustStyle($(this).width());
    });
});

-->
//]]>
</script>

However, in addition to that - I'm also using LESS.CSS (http://lesscss.org/). Well, those two don't work together well - with LESS, as I understand, CSS is embedded into HTML document itself, so the CSS file isn't replaced. Is there any way to make it work? Or maybe a better way ?

Irminsul
  • 225
  • 2
  • 4
  • 14
  • "with LESS, as I understand, CSS is embedded into HTML document itself" Incorrect. LESS simply compiles into plain CSS. Do it server-side for production, and you're golden. Simply link to the .less files instead and have your server do the compiling (and caching) – xec Oct 29 '13 at 15:56

1 Answers1

2

Can't you just use media queries?

<link rel='stylesheet' media='screen and (max-width: 701px)' href='narrow.css' />
<link rel='stylesheet' media='screen and (min-width: 701px) and (max-width: 1120px)' href='medium.css' />
<link rel='stylesheet' media='screen and (min-width: 1120px)' href='wide.css' />
Prisoner
  • 27,391
  • 11
  • 73
  • 102
  • Well, I was thinking about it. I decided to use jQuery instead, because of browser compability - versions below IE9 don't support media queries? – Irminsul Oct 29 '13 at 16:06