1

I want to set media queries manually. I have some JavaScript algorithm that tells me what to choose, suppose it is IsMediaA().

@media A { display: block; }
@media B { display: inline-block; }

Is it possible to change used media query depending on IsMediaA() value?

3
  • Why would you want to change a media query with JavaScript? Commented Jun 3, 2015 at 12:31
  • why? o.O why use js to fake what media queries already do? Commented Jun 3, 2015 at 12:31
  • Just my curiosity. Someone asked me if this is possible Commented Jun 3, 2015 at 12:33

1 Answer 1

2

Something like this could do the work.

if(IsMediaA()) {

   $('<style />', {
        'text': '@media A { display: block; }'
   }).appendTo('body');

}

but it's a lot better to workaround with some state classes and style it property in css file:

js:

if(IsMediaA()) $('element').addClass('is-mediaA');

css:

@media A {
    element.is-mediaA { display: block; }
}
Sign up to request clarification or add additional context in comments.

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.