Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript - Emulate CSS Media Queries?

In my CSS file I have this:

@media (max-width: 480px) {
  div {
     background: red;
  }
}

Emulate with javascript

Is there a way to emulate 480px device width with Javascript? I'm thinking of a button with click event to make the CSS beleve the device with is max 480px width.

I will be using jQuery for it if it's possible to emulate media queries.

like image 941
Jens Törnell Avatar asked Dec 13 '25 21:12

Jens Törnell


1 Answers

You can set the viewport manually in JS, which could be the solution. This SO question covers it Dynamic viewport

Set the content attribute to include a width parameter:

document.getElementById("viewport").setAttribute("content", "width=1024, initial-scale=0, maximum-scale=1.0, minimum-scale=0.25, user-scalable=yes");

from http://www.wearecube.ch/how-to-do-responsive-optout-by-setting-the-view-port-with-javascript-or-ruby-on-rails/

like image 198
Dylan Valade Avatar answered Dec 15 '25 11:12

Dylan Valade