@Import of less files into a limited scope

不羁岁月 提交于 2019-12-03 06:48:14

问题


I want to import all styles from another less file, but into a limited scope. I'm trying this:

"my-site.less"

.wrapper-class {
    @import "path/to/styles.less";
}

But this doesn't work at all. I'm using the browser-based less.js option, and I can see the GET statement that is run when it actually does the import. But the resultant CSS does not contain any of the styles from the other sheet. If I do it like this, it works:

.wrapper-class 
{
    /* ... */
}
@import "path/to/styles.less";

But this defeats the original purpose. So is there any way to do a limited-scope @import like this in Less? Thanks!


回答1:


According to the css-spec, the @import-declaration has to come before all other declarations in the css-file. So your @import inside the rule is expected to fail. I guess the @importing at the end of the file not failing is goodwill of the browser-vendors.

I guess LESS will abide by the same rules.

EDIT: the question is, why do you want to have those styles scoped? With proper declarations, this should not be necessary.




回答2:


Since this question was posted and answered, pre-processors have implemented @import with scope. This now works in LESS, SASS and Stylus.

example:

.lt-ie9 {
  @import "ie8-fixes";
}



回答3:


The syntax of LESS (and CSS) don't allow @imports into CSS rules. Try use namespace:

styles.less

.styles {
    [your code]
}

Then, replace the @import for namespace:

.wrapper-class {
    .styles;
}

Reference:

http://lesscss.org/features/



来源:https://stackoverflow.com/questions/9623791/import-of-less-files-into-a-limited-scope

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!