LESS importing CSS and relative paths

不想你离开。 提交于 2019-11-30 17:14:36
Planky

When running lessc use the --relative-urls flag.

lessc --relative-urls

It's poorly documented, but by default it is false which means that all @imported files will maintain their urls (e.g. background-image, font-face, etc) as they are. This is because less was already doing this and many users expect it to leave their urls alone.

When the relative-urls flag is used, lessc rewrites all of the urls according to the location of the less/css file being imported.

Example

/dir1/style/main.less

// if you don't include (less) lessc will leave bootstrap
//   as an @import at the top of the lessified css file
@import (less) '../bootstrap/bootstrap.min.css';

/dir1/lib/bootstrap/bootstrap.min.css

background-image:url("../img/bs-img.gif");

Result:

/dir1/style/main.css

background-image:url("../bootstrap/img/bs-img.gif");

Check out the docs for command line usage. https://github.com/cloudhead/less.js/wiki/Command-Line-Usage. There's an option called --root-path that will prepend existing urls so that they will work in the output css file.

lessc [option option=parameter ...] <source> [destination]

lessc -rp=will/be/prepended sourcefile.less path/to/destination

For example:

Here is the original url, with the file in css/src/nest

background-image: url('../../../imgs/bg.png');

And this is what I would do on the command line. Note that the -rp argument should point from the output directory to the original file location

lessc -rp=src/nest css/src/nest/nesty.less css/nesty.less

And the output, with the file in css/

  background-image:url('src/nest/../../../imgs/bg.png')

There is a --relative-urls option, but I can't get it to work. I'm working build script that uses the workaround I described above. Build-o-Matic

This is how I handled determining the path [link]

--relative-urls flag has its in-browser counterpart.

<script>
less = {
    env: "development",
    relativeUrls: true
};
</script>
<script src="less.min.js"></script>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!