Why won't my @media queries work?

Front-End Web Development: Why would it be better to dynamically apply CSS classes with jQuery as opposed to writing multiple media queries?

  • I know this initially sounds vague. But I was wondering what different use cases could be for using or not using jQuery to manipulate the CSS. Writing media queries is both time consuming and can add a lot of glut to your CSS files. However it can be more readable. But media queries are not supported in many older browsers. On the other hand, jQuery is widely supported, so less compatibility issues. But using the jQuery library itself can add a lot of glut as well. I get a feeling that I am missing something here, so feel free to point out if there is a more efficient or otherwise better approach. Or if I just need to step up to a more advanced technology in order to improve web performance.

  • Answer:

    You would use media queries to create a mobile version of a website or page. Media queries won't look for a type of device, but rather look at screen sizes, orientation (be it landscape or portrait) and resolution (think retina displays). Now, if you want to target say the iPhone3G, you would write a media query to target a screensize of 320px wide in portrait mode or 480px wide in landscape. There's no need to alter your HTML for this, you just write the media query to target specific classes in your existing markup and make them respond to that screen size and orientation. Using jQuery, you would add classes with every screensize. You would still need to define breakpoints (320px, 480px, 960px, etc) and apply specific classes when the screen size matches your breakpoint. Think now, jquery just applies your classes. You will STILL need to write the class style definitions in your CSS. So what did you gain? Absolutely nothing. You're still writing CSS, but instead of using media queries, your're using jQuery to apply those same style definitions to HTML. So why would you use jQuery? Are you targeting IE browsers < 8? Use jQuery. I can't think of a mobile device using that browser though. My advice, figure out your optimal screen size for which you want to design. Say it's 960px, most desktops and portable computers will have this. It won't matter which browser your users use, the screen size is what matters and you've done your job there. Then target other screensizes on both sides of the spectrum with media queries. Done. By the way, give SASS a try. It has mixins for the various breakpoints so it throws your glut away :)

Bogdan Lazar at Quora Visit the source

Was this solution helpful to you?

Other answers

First, let's cover why they're useful... jQuery-driven classes Used for defining visual state. e.g. Set a class on a parent element and the CSS can style the parent and all descendant elements. Great for 'class-tweening' or animating between states. Classes as an event manager. If you have a series of event handlers waiting for elements with specific classes, you could attach the class to the element and the event handlers would kick in (or finish). Classes can be added based on arbitrary rules - e.g. add classes to the elements that exceed a given datapoint. CSS Media Queries Mainly for controlling the visual aspects of the user interface and its content Useful for revealing or hiding functional aspects of the UI based on simple device capabilities. Less links in the chain - just needs HTML, therefore more reliable. Media Queries are applied earlier in the rendering process than jQuery-driven classes. Self-contained and therefore modular. Only needs the accompanying HTML. Now let's look at where these two strategies go head-to-head. Common reasonsAs you see above, the major overlap between the two strategies is when you want to control (mainly visual-) aspects of the UI based on the physical aspects of the consuming device. The right tool for the right jobIf you had to pick between the two, picking the right strategy has a number of factors in play (as illustrates): Who will be authoring and maintaining this strategy? e.g. Developers grok jQuery but sometimes struggle with media queries. What browsers (and therefore devices) are involved? What are their capabilities? (CPU, Memory, Screen size, GPU, etc) What else needs to be achieved at the same time? e.g. Media Queries can't add event handlers or load extra content not present in the document. How critical is it to be rendered immediately? How critical is it to have less moving parts? Less important: How modular or interchangeable must it be? Can you copy it easily from another project? On performancePerformance always used to be about bytes and HTTP requests, but these days the most important factor in Web Performance is perceived performance. i.e. How quickly does it render? Does it look fast? With that in mind, pushing what you can into CSS Media Queries can have a noticeable improvement in perceived performance, especially if you're working on a traditional web app (and not a Client-side Web App, e.g. AngularJS) As the CSS has been completely parsed, the browser starts painting. With any use of script, you're going to have to wait for these scripts (such as jQuery) to be parsed and executed on top of the CSS. hth, -S

Shaun O'Connell

To add to the existing answers, there will be severe search engine crawling issues. Add to that the performance penalty to go through the JS parser before even painting the basic content. Your approach essentially violates the logic behind the basic rule: CSS at the head, JS at the bottom.

Ratul Saha

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.