简体   繁体   English

CSS3 Less(预处理器)-公共类

[英]CSS3 Less (Preprocessor) - Public classes

I'm trying to define generic classes for my website in a file called "generic.less" and it's correctly linked in the head in my html file. 我正在尝试在名为“ generic.less”的文件中为我的网站定义通用类,并且在我的html文件的head中正确链接了它。 This "less" file contains: 此“较少”文件包含:

/****GENERIC CLASSES****/
/**********************/
.border-box{-webkit-box-sizing:border-box; -moz-box-sizing:border-box; box-sizing:border-box;}
.unselectable{-moz-user-select:none; -khtml-user-select:none; -webkit-user-select:none; -ms-user-select:none; user-select:none;}
.grayscale{-webkit-filter:grayscale(100%); filter:grayscale(100%);}
.fullcenter{top:0; bottom:0; left:0; right:0; margin:auto; position:absolute;}

And then I'm trying to inherit these classes from different "less" files but It won't work. 然后,我试图从不同的“较少”文件继承这些类,但是它将无法正常工作。 I'm guessing that "less" preprocessor takes the classes as private classes defined in the same "less" file (sorry for my Java interpretation). 我猜想“较少”的预处理器会将这些类作为在同一“较少”文件中定义的私有类(对我的Java解释很抱歉)。

Is there any way I can define those classes so I can inherit them from other "less" file? 有什么方法可以定义这些类,以便可以从其他“较少”文件继承它们? That way I don't have to duplicate the classes for each new class file. 这样,我不必为每个新的类文件复制类。

HTML head looks like this: HTML头看起来像这样:

<!-- STYLE -->
<link rel="stylesheet" type="text/less" href="<?echo $path_styles;?>generic.less"> <!-- Generic classes -->
<link rel="stylesheet" type="text/less" href="<?echo $path_styles;?>index.less">
<link rel="stylesheet" type="text/less" href="<?echo $path_styles;?>bio.less">
<link rel="stylesheet" type="text/less" href="<?echo $path_styles;?>discografia.less">

A LESS file doesn't know about other LESS files when compiled. 编译时,LESS文件不了解其他LESS文件。
In each of your LESS files (except generic.less itself, of course), you can import your generic file with @import . 在每个LESS文件中(当然,除了generic.less本身之外),您都可以使用@import 导入您的常规文件。 As it'd output many times the CSS of this file, you should add the option (reference) : file is imported so variables and mixins are known to the rest of the file import-ing it but it isn't output. 由于它会多次输出此文件的CSS,因此您应该添加选项(reference) :文件已导入,因此导入文件的其余部分都知道变量和混合,但不会输出。

@import (reference) "generic.less"

Sidenote: are you using less.js , even in development? 旁注:即使在开发中,您是否也使用less.js Server-side tools like gulp-less or Prepros (v4 was free software) are much convenient and fast imho. 服务器端工具,如gulp-less gulp或Prepros(v4是免费软件),非常方便快捷。

As you see from the LESS , it's very simple to inherit classes, to use them on a separate file you just need to @import them to the main LESS file (to the beginning) http://lesscss.org/features/#import-directives-feature LESS可以看到,继承类非常简单,要在单独的文件上使用它们,只需要将它们@import到主LESS文件中(从头开始) http://lesscss.org/features/#import -directives特征

.bordered {
  border-top: dotted 1px black;
  border-bottom: solid 2px black;
}

And we want to use these properties inside other rule-sets. 我们想在其他规则集中使用这些属性。 Well, we just have to drop in the name of the class where we want the properties, like so: 好吧,我们只需要在需要属性的类的名称中输入,如下所示:

#menu a {
  color: #111;
  .bordered;
}

.post a {
  color: red;
  .bordered;
}

We can also use functions the same way.. 我们也可以以相同的方式使用函数。

.transition(@transition) {
    -webkit-transition: @transition;
    -moz-transition: @transition;
    -o-transition: @transition;
    transition: @transition;
}
.opacity(@opacity) {
    opacity: @opacity / 100;
    filter: ~"alpha(opacity=@{opacity})";
}

a {
    .transition(all 0.4s);
    &:hover {
        .opacity(70);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM