cssless

Using CSS variables in LESS


This might seem basic, but I can't figure out how to use CSS variables in LESS?

variables.css:

.root {
    --header-color: white;
    --content-color: yellow;
}

styles.less:

@import "../variables.css";
.header {
   color: @header-color;
}

I get error "@header-color is undefined".


Solution

  • LESS allows you to use normal CSS code, so use one option could be just use the variable as CSS:

    @import "../variables.css";
    .header {
       color: var(--header-color);
    }
    

    Also, you can save the css var to a LESS var:

    @import "../variables.css";
    @header-color: var(--header-color);
    
    .header {
       color: @header-color;
    }