Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

less.css sharing variables across files

Tags:

less

I am using less.css to simplify my css styling. I'd like to declare a varaible in one less file and share its usage across my many less files. Is this possible? For example:

english.less

@languageFloat: left;

chart.less

div#footer a.web {        display: block;     float: @languageFloat;     color: #cccccc;     margin-right: 10px; } 
like image 556
vondip Avatar asked Oct 13 '11 15:10

vondip


People also ask

Can I use CSS variables in LESS?

As we know, less function can't work with css variables.

How do you import LESS variables?

The best way to do this is to @import your LESS file with all your variables in it. Here's the syntax for the @import keyword: // For LESS file includes, @import "lib. less"; // or @import "lib"; // infers the .

What is the difference between LESS and CSS?

LESS and CSS syntax The second difference between LESS and CSS lies in the syntax. However, the LESS syntax is little different from the CSS one, as the stylesheet language is a CSS preprocessor. This means that any CSS code is a valid LESS code (but additional LESS elements won't work in a plain CSS).

How do you represent a variable in LESS?

Defining a variable: Variables in Less are represented with an at (@) sign. We can assign a value using a colon (:).


1 Answers

The best way to do this is to @import your LESS file with all your variables in it. Here's the syntax for the @import keyword:

// For LESS file includes, @import "lib.less"; // or @import "lib"; // infers the .less extension  // and for plain CSS includes which are added but not parsed by LESS @import "style.css"; 

This works especially well if you serve CSS files to your users (as opposed to the in-browser less.js parsing) because the @import statement will compound your LESS and CSS files into one single CSS file. Maybe you can consider having one controller LESS file that includes your variables, then your other LESS and CSS files so that the end result is one single file you serve to the browser.

I imagine it would be something simple like this:

// Controller.less @import "english.less"; @import "chart.less"; 
like image 148
Nathan Strutz Avatar answered Sep 24 '22 04:09

Nathan Strutz



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!