Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change a css class property value for a specific mediaquery?

I have the class

.foo {
  width: 30px;
}
@media only screen and (max-width:768px){
   .foo {
     width: 20px;
   }
}

And I want to change the value of width from 20px to 15px with javascript for the mediaquery only. When I do it, it changes the class for all resolution discounting the mediaquery in the CSS.

like image 838
the duck Avatar asked Oct 26 '25 05:10

the duck


1 Answers

You can try this:

if (window.matchMedia("(max-width: 768px)").matches) {
  document.querySelector('.foo').style.width = "15px";
}
like image 177
Vitaliy Javurek Avatar answered Oct 28 '25 19:10

Vitaliy Javurek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!