less - LESS 是否可以防止重复样式?

标签 less

如果我在 LESS 中定义以下嵌套样式:

.nav-bar {
    .navbar-brand {
        background: #000;
    }
}

.nav-bar {
    .navbar-brand {
        background: #fff;
    }
}

结果是:
.nav-bar .navbar-brand {
  background: #000;
}
.nav-bar .navbar-brand {
  background: #fff;
}

我希望这些可能会被检测为重复项。所以,结果只会是:
.nav-bar .navbar-brand {
  background: #fff;
}

.. 因为第一个样式将被覆盖。我知道网络浏览器会这样对待它,第二个样式属性会覆盖第一个。不过我更关心文件大小。我希望我可以为 Bootstrap 下载 LESS,然后在自定义 LESS 文件中,覆盖该特定项目的样式。编译器是否可以使用此功能?

最佳答案

您应该使用 https://github.com/less/less-plugin-clean-cssadvanced选项打开。

运行 npm install less-plugin-clean-css之后你可以使用 lessc file.less --clean-css="advanced" .

使用上述命令:

.nav-bar {
    .navbar-brand {
        background: #000;
    }
}

.nav-bar {
    .navbar-brand {
        background: #fff;
    }
}

编译成:
.nav-bar .navbar-brand{background:#fff}

另见:How to keep duplicate properties in compiled CSS file when use LESS?

关于less - LESS 是否可以防止重复样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27832173/

相关文章:

div 中第 8 个和第一个元素的 CSS 规则

css - 在 LESS 中使用 CSS 变量

npm - 在 npm 中使用本地 less

css - 如何创建主题化网站?

webpack - Less-Loader : Import URLs are being treated as relative URLs, 没有传递 --relative-url 选项

css - Firefox 不从 .less 文件渲染行高

css - iPad 1 没有检测到 LESS css?

asp.net - 在 Debug模式下使用 BundleTransformer/LESS 运行 Autoprefixer

html - angularJS 中的 CSS 过渡属性自动高度

html - 用于大屏幕标题的 Bootstrap 3 网格列