Change styles based on dark or light mode

后端 未结 1 1388
礼貌的吻别
礼貌的吻别 2021-01-25 06:26

I want to have a dark and a light theme on my Vue app. I can create dark.scss file and change classes styles and use !important property to override st

相关标签:
1条回答
  • 2021-01-25 07:00

    Well i would not do it with classes. I would create CSS variables with either SCSS or you create CSS variables in :root

    If you do it with the :root method then it should look something like this:

    :root {
       --background: red;
    }
    

    Then you can access it in any component like this for example:

    .class {
       background: var(--background); // the background will appear red
    }
    

    Now you can change the background color with just 1 CSS variables.

    To change the variable with Javascript you just write following:

     root.style.setProperty('--background', "green");
    

    The problem here is that it isnt supported in IE if you care about browser support. So you should create an fallback like this:

    .class {
       background: red; //fallback
       background: var(--background); // the background will appear red
    }
    
    0 讨论(0)
提交回复
热议问题