- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在为 WordPress 网络创建一个主题系统,该系统支持多种布局主题,可以支持各种大学的配色方案。为此,我定期编译一个包含学校特定变量的 LESS 文件(使用 lessphp),并将其用作主题中的辅助类库。
每所学校在 LESS 中定义了 3 种颜色:@primary
、@secondary
和 @tertiary
。该方法简单明了且实用,但需要在代码中进行大量重复。例如:
//Modifier Classes
.primary-lighter-text {
color: lighten(@primary,20);
}
.sec-lighter-text {
color: lighten(@secondary,20);
}
.tert-lighter-text {
color: lighten(@tertiary,20);
}
//Backgrounds
.primary-bg {
background-color: @primary;
}
.sec-bg {
background-color: @secondary;
}
.tert-bg {
background-color: @tertiary;
}
//Borders
.primary-border{
border-color: @primary;
}
.sec-border {
border-color: @secondary;
}
.tert-border {
border-color: @tertiary;
}
从 LESS 的角度来看并没有什么复杂的,但是如果我想添加一个新的助手类,我必须创建 3 个。有没有更简洁的方法来实现这个?
最佳答案
您可以使用数组循环来简化它。在添加新内容的情况下,您只需修改末尾的数组变量即可。
.loop-column(@index) when (@index > 0) { /* Recursive Mixin with Guard condition. Mixin is processed only when the condition is satisfied */
.loop-column(@index - 1); /* Call the mixin again with a decremented counter */
@ctype: extract(@type, @index); /* Extract the type value corresponding to the index from the array */
@color: extract(@colors, @index); /* Extract the color value corresponding to the index from the array */
/* Form and Output the necessary classes and properties */
.@{ctype}-lighter-text { /* Selector interpolation to dynamically form the selector */
color: lighten(@color,20);
}
.@{ctype}-bg {
background-color: @color;
}
.@{ctype}-border{
border-color: @color;
}
}
.loop-column(length(@type));
@type: primary, sec, tert; /* The color types array */
@colors:#fff, #777, #000; /* The color value array for each type */
/* If required the colors can be kept as separate variables also. Refer 2nd demo. */
更新:(基于 Andrew Cafourek 和 seven-phases-max 的评论)
由于 LessPHP 已过时,应添加以下行并将 length(@type)
替换为实际计数。
.loop-column(0) {};
.loop-column(4);
关于less - 简化重复的LESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24757612/
我在尝试使用 WebStorm LESS File Watcher 时遇到此错误 /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/
我有一个带有以下结构的 .less 样式表: @width:600; .some-style { width:@{width}px; } 这给出了以下错误: Expected '}' on l
说我有以下结构 /node_modules /src /components /component1 /style /style.less
我想使用灰度函数,但 LESS 一直抛出此错误,我没有看到灰度的 mixin 或如何使用它。有什么帮助吗? Error 148 Expected color in function 'graysc
我的文件较少,如下面的目录结构 vendor/includes/file1.less vendor/includes/file1.less vendor/includes/file1
如果我在 LESS 中定义以下嵌套样式: .nav-bar { .navbar-brand { background: #000; } } .nav-bar {
我正在尝试设置一个使用 lein-less 插件编译 .less 文件的 clojurescript 项目。我应该说我是 clojurescript 的新手。问题是它似乎没有运行。它在我运行“lein
有人会这样使用“less”: var less = require('less'); less.render('.class { width: (1 + 1) }', function (e, css
我正在尝试在一个新元素中测试 less.js。我 100% 确定 javascript 文件加载正常,但我的 css 文件夹中的 less 文件一直出现此错误。我打算在启动之前使用 less.app
我有以下更少: .container { .column, .columns { .one& { width: 40px; }
这是一个代码(test.less): body { padding: 50px; } body { padding: 20px; } 为什么在编译成 CSS 后我有这个: body {
我有一个变量 @index我想输出一个选择器,以便当 @index是 3 , 选择器是 [data-sth="3"] . 更长的例子: @index: 3; /* selector here */ {
假设我有一个使用 Less 定义的样式: ul.unstyled, ol.unstyled { margin-left: 0; list-style: none; } 稍后,我想重新使用 un
假设我有以下 Less 设置: .box { border: 1px solid #333; &.error { background-color: Red; } } 如果我想
我有一个 common.less文件,它为不同的皮肤实现了基本的 CSS: @textColor: black; @iconSize: 16px; .container: { color: @
我创建了使用 LESS 功能切换主题的代码。不幸的是,LESS 文件不会在浏览器中编译。如何在浏览器中编译 LESS 文件? 最佳答案 这是一张documentation : Client-side
我想使用 less 来定位类中的特定元素。 在这种情况下,我想定位类 button 的元素,但在此范围内我想定位一个 anchor 标签 a . 目前我有: .button { /* lots
我正在努力做某事。我的项目是这样的: /app /assets /components /stylesheets /less
我目前正在一个项目中使用 Twitter Bootstrap,包括 LESS 文件并使用我们编写的一些附加 LESS 代码进行编译。 最新版本意味着我们需要覆盖一些 Bootstrap LESS 变量
我有这个更少的代码,这工作得很好。我只是想在 less cli 编译它时节省一些空间。 .secondary-content { background: #ffcc80; color:
我是一名优秀的程序员,十分优秀!