CSS native variables not working in media queries

前端 未结 7 1443
情书的邮戳
情书的邮戳 2020-11-30 19:57

I am trying to use CSS variables in media query and it does not work.

:root {
  --mobile-breakpoint: 642px;
}

@media (max-width: var(--mobile-breakpoint)) {         


        
相关标签:
7条回答
  • 2020-11-30 20:50

    One way to achieve what you want is using npm package postcss-media-variables.

    If you are fine with using npm packages then you can take a look documentatoin for same here

    • postcss-media-variables

    Example

    /* input */
    :root {
      --min-width: 1000px;
      --smallscreen: 480px;
    }
    @media (min-width: var(--min-width)) {}
    @media (max-width: calc(var(--min-width) - 1px)) {}
    
    @custom-media --small-device (max-width: var(--smallscreen));
    @media (--small-device) {}
    
    0 讨论(0)
提交回复
热议问题