Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS performance with media queries

I am wondering if CSS can be 'heavy', e.g. use a lot of parsing time from a browser.

For example I use a CSS sheet with a lot of specific selectors, like

:last-child, 
:nth-child(n) 
table.sortable thead tr th:not(.table-th-nosort):hover

etc. Can that noticably influence performance?

Same for using media-queries. I want to make a site accessible for mobile devices using CSS3 media queries:

@media screen and (max-width: 600px) {
   #sidebar {
      display: none;
      // etc
   }
}

For now I have about 600 lines of CSS (not minified) in my main file, and for some specific pages include extra CSS files (between 10-300 lines).

Using media queries would add substantially to that I expect. Will that have an effect on performance?

like image 516
Lennart Avatar asked Jan 05 '12 16:01

Lennart


People also ask

Do media queries effect performance?

Actually placing media queries directly in the head section of your page will cause your pages to load much faster than linking to them. Also, linking to an external style sheet for any reason will slow down the page load time as well.

How many media queries should I use in CSS?

You can also have more than one breakpoint for CSS selectors. You might add on additional media queries to continue changing the h1 size as the viewport increases to 62em (992px) and 87em (1392px) wide. You can create as many breakpoints as you would like, and you can use any width that you would like.

Are media queries still used 2022?

Responsiveness is an important part of modern web development. However in the early days we had less number of devices and had the liberty to write as many media queries as we wanted. With time, the screen ratios changed and still keep evolving.

Can you use media queries in CSS?

CSS3 Introduced Media QueriesMedia queries can be used to check many things, such as: width and height of the viewport. width and height of the device. orientation (is the tablet/phone in landscape or portrait mode?)


1 Answers

The easiest way to check is to grab a webkit nightly, or Chrome Canary, then check out the new audit for CSS performance. It lets you see how long each selector takes to run, as well as the % of overall time taken. The new builds of Opera also have a similar tool.

Here's a small screenshot of what it looks like:

CSS performance audit

like image 120
Rich Bradshaw Avatar answered Sep 19 '22 13:09

Rich Bradshaw