Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the right relative paths in my compiled LESS css files?

Tags:

css

less

I'm having a problem with LESS breaking relative URLs in my compiled files. For example, I have;

├── style.less
├── style.css
├── assets
│   ├── img
│   │   └──  bg.png
│   ├── less
│   │   └──  included.less

Style.less imports included.less which has the following line;

body {background: url(../img/wall-texture.png);}

But the output in style.css becomes

body {background: url(assets/less/assets/less/../img/wall-texture.png);}

What's going on here, and how can I fix this so that my paths remain correct after compiling? I realize that perhaps my relative path in included.less needs to be adjusted, and that's fine, but currently, with how less is doubling "assets/less" it makes it extremely convoluted to get the right path while maintaining a reasonable folder structure. Besides that I'm using git submodules to include different LESS projects, so I don't really want to change either the code in the less files or the folder structure, I just want to coerce LESS into compiling correctly. (I've tried all of the Windows compilers I can find, and they all behave the same.)

Any help greatly appreciated!

like image 528
Jo Sprague Avatar asked Oct 13 '12 18:10

Jo Sprague


People also ask

How do I find the relative path of a file?

A relative path starts with / , ./ or ../ . To get a relative path in Python you first have to find the location of the working directory where the script or module is stored. Then from that location, you get the relative path to the file want.

How do you specify relative paths?

A relative path refers to a location that is relative to a current directory. Relative paths make use of two special symbols, a dot (.) and a double-dot (..), which translate into the current directory and the parent directory. Double dots are used for moving up in the hierarchy.

What is relative path in CSS?

When linking to files within the same project, use a relative path. The path is determined by where a file is located within the directory. So in this example, the index file is in the same folder as the css and image files.

Is absolute path or relative path better?

Absolute URLs must be used to link to other websites that are not located on the same domain. Relative URLs, on the other hand, are more easy to use because they are relative to the page they are on.


2 Answers

There may well be a solution that resolves your compiling. If not, one possible solution (which does require you to change your LESS files) is to interpolate the path. That way, the compiler might leave it alone as far as appending to it:

body {background: url(~"../img/wall-texture.png");}
like image 167
ScottS Avatar answered Sep 25 '22 22:09

ScottS


Have a look at https://github.com/marklagendijk/WinLess/issues/12 It seems to be related with exactly the version you are using (1.5.3). I recommend to update to the latest winless build which is already 1.8.0.

There have been several issues with relative paths along the way of winless. But most of them seem to be fixed. See also https://github.com/marklagendijk/WinLess/issues/search?q=path

Please note that the default behavior of the less compiler is actual to retain the relative path as you would expect.

like image 38
Patrick Hammer Avatar answered Sep 23 '22 22:09

Patrick Hammer