skip to Main Content

How to change style css value using javascript?

If you change the css style using javascript, then the style will be added to the general css style. See image below.
Question: how to add or change style in media screen?

enter image description here

2

Answers


  1. Solution 1

    Try to give it !important to be

    el.style.setProperty("display", "block !important")
    

    Solution 2

    First, set the display to a variable:

    .div2 {
      --display: none;
      display: var(--display);
    }
    
    el.style.setProperty("--display", "block");
    
    Login or Signup to reply.
  2. You need to check the device width.

    const width = window.innerWidth || document.documentElement.clientWidth ||document.body.clientWidth;
    (width > 500) ? el.style.setProperty("display", "block !important") : el.style.setProperty("display", "none !important")
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search