- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我创建了一系列 CSS 类来轻松控制各种元素的边距和填充。我还利用媒体查询来创建仅适用于特定屏幕宽度的响应类。一切似乎都按预期工作,但是正如您所看到的,我的 Less 编写能力极度缺乏并且代码非常庞大!
我知道可以使用更少的代码来输出所需的 CSS,但是我编写自定义混合和循环的所有尝试都失败了。
是否可以将此代码块简化为更流畅的代码?任何帮助将不胜感激。
干杯!
// Margins and padding
// -------------------------
// Horizontal and vertical offset values
@x-0: 0;
@x-xs: (@grid-gutter-width / 4);
@x-sm: (@grid-gutter-width / 2);
@x-md: @grid-gutter-width;
@x-lg: (@grid-gutter-width * 2);
@x-xl: (@grid-gutter-width * 4);
@y-0: 0;
@y-xs: (@line-height-computed / 4);
@y-sm: (@line-height-computed / 2);
@y-md: @line-height-computed;
@y-lg: (@line-height-computed * 2);
@y-xl: (@line-height-computed * 4);
// Static margins
.mt-0 { margin-top: @y-0; }
.mt-xs { margin-top: @y-xs; }
.mt-sm { margin-top: @y-sm; }
.mt-md { margin-top: @y-md; }
.mt-lg { margin-top: @y-lg; }
.mt-xl { margin-top: @y-xl; }
.mr-0 { margin-right: @x-0; }
.mr-xs { margin-right: @x-xs; }
.mr-sm { margin-right: @x-sm; }
.mr-md { margin-right: @x-md; }
.mr-lg { margin-right: @x-lg; }
.mr-xl { margin-right: @x-xl; }
.mb-0 { margin-bottom: @y-0; }
.mb-xs { margin-bottom: @y-xs; }
.mb-sm { margin-bottom: @y-sm; }
.mb-md { margin-bottom: @y-md; }
.mb-lg { margin-bottom: @y-lg; }
.mb-xl { margin-bottom: @y-xl; }
.ml-0 { margin-left: @x-0; }
.ml-xs { margin-left: @x-xs; }
.ml-sm { margin-left: @x-sm; }
.ml-md { margin-left: @x-md; }
.ml-lg { margin-left: @x-lg; }
.ml-xl { margin-left: @x-xl; }
// Responsive margins
@media (max-width: @screen-xs-max) {
.mt-0-sm { margin-top: @y-0; }
.mt-xs-sm { margin-top: @y-xs; }
.mt-sm-sm { margin-top: @y-sm; }
.mt-md-sm { margin-top: @y-md; }
.mt-lg-sm { margin-top: @y-lg; }
.mt-xl-sm { margin-top: @y-xl; }
.mr-0-sm { margin-right: @x-0; }
.mr-xs-sm { margin-right: @x-xs; }
.mr-sm-sm { margin-right: @x-sm; }
.mr-md-sm { margin-right: @x-md; }
.mr-lg-sm { margin-right: @x-lg; }
.mr-xl-sm { margin-right: @x-xl; }
.mb-0-sm { margin-bottom: @y-0; }
.mb-xs-sm { margin-bottom: @y-xs; }
.mb-sm-sm { margin-bottom: @y-sm; }
.mb-md-sm { margin-bottom: @y-md; }
.mb-lg-sm { margin-bottom: @y-lg; }
.mb-xl-sm { margin-bottom: @y-xl; }
.ml-0-sm { margin-left: @x-0; }
.ml-xs-sm { margin-left: @x-xs; }
.ml-sm-sm { margin-left: @x-sm; }
.ml-md-sm { margin-left: @x-md; }
.ml-lg-sm { margin-left: @x-lg; }
.ml-xl-sm { margin-left: @x-xl; }
}
@media (max-width: @screen-sm-max) {
.mt-0-md { margin-top: @y-0; }
.mt-xs-md { margin-top: @y-xs; }
.mt-sm-md { margin-top: @y-sm; }
.mt-md-md { margin-top: @y-md; }
.mt-lg-md { margin-top: @y-lg; }
.mt-xl-md { margin-top: @y-xl; }
.mr-0-md { margin-right: @x-0; }
.mr-xs-md { margin-right: @x-xs; }
.mr-sm-md { margin-right: @x-sm; }
.mr-md-md { margin-right: @x-md; }
.mr-lg-md { margin-right: @x-lg; }
.mr-xl-md { margin-right: @x-xl; }
.mb-0-md { margin-bottom: @y-0; }
.mb-xs-md { margin-bottom: @y-xs; }
.mb-sm-md { margin-bottom: @y-sm; }
.mb-md-md { margin-bottom: @y-md; }
.mb-lg-md { margin-bottom: @y-lg; }
.mb-xl-md { margin-bottom: @y-xl; }
.ml-0-md { margin-left: @x-0; }
.ml-xs-md { margin-left: @x-xs; }
.ml-sm-md { margin-left: @x-sm; }
.ml-md-md { margin-left: @x-md; }
.ml-lg-md { margin-left: @x-lg; }
.ml-xl-md { margin-left: @x-xl; }
}
@media (max-width: @screen-md-max) {
.mt-0-lg { margin-top: @y-0; }
.mt-xs-lg { margin-top: @y-xs; }
.mt-sm-lg { margin-top: @y-sm; }
.mt-md-lg { margin-top: @y-md; }
.mt-lg-lg { margin-top: @y-lg; }
.mt-xl-lg { margin-top: @y-xl; }
.mr-0-lg { margin-right: @x-0; }
.mr-xs-lg { margin-right: @x-xs; }
.mr-sm-lg { margin-right: @x-sm; }
.mr-md-lg { margin-right: @x-md; }
.mr-lg-lg { margin-right: @x-lg; }
.mr-xl-lg { margin-right: @x-xl; }
.mb-0-lg { margin-bottom: @y-0; }
.mb-xs-lg { margin-bottom: @y-xs; }
.mb-sm-lg { margin-bottom: @y-sm; }
.mb-md-lg { margin-bottom: @y-md; }
.mb-lg-lg { margin-bottom: @y-lg; }
.mb-xl-lg { margin-bottom: @y-xl; }
.ml-0-lg { margin-left: @x-0; }
.ml-xs-lg { margin-left: @x-xs; }
.ml-sm-lg { margin-left: @x-sm; }
.ml-md-lg { margin-left: @x-md; }
.ml-lg-lg { margin-left: @x-lg; }
.ml-xl-lg { margin-left: @x-xl; }
}
// Static padding
.pt-0 { padding-top: @y-0; }
.pt-xs { padding-top: @y-xs; }
.pt-sm { padding-top: @y-sm; }
.pt-md { padding-top: @y-md; }
.pt-lg { padding-top: @y-lg; }
.pt-xl { padding-top: @y-xl; }
.pr-0 { padding-right: @x-0; }
.pr-xs { padding-right: @x-xs; }
.pr-sm { padding-right: @x-sm; }
.pr-md { padding-right: @x-md; }
.pr-lg { padding-right: @x-lg; }
.pr-xl { padding-right: @x-xl; }
.pb-0 { padding-bottom: @y-0; }
.pb-xs { padding-bottom: @y-xs; }
.pb-sm { padding-bottom: @y-sm; }
.pb-md { padding-bottom: @y-md; }
.pb-lg { padding-bottom: @y-lg; }
.pb-xl { padding-bottom: @y-xl; }
.pl-0 { padding-left: @x-0; }
.pl-xs { padding-left: @x-xs; }
.pl-sm { padding-left: @x-sm; }
.pl-md { padding-left: @x-md; }
.pl-lg { padding-left: @x-lg; }
.pl-xl { padding-left: @x-xl; }
// Responsive padding
@media (max-width: @screen-xs-max) {
.pt-0-sm { padding-top: @y-0; }
.pt-xs-sm { padding-top: @y-xs; }
.pt-sm-sm { padding-top: @y-sm; }
.pt-md-sm { padding-top: @y-md; }
.pt-lg-sm { padding-top: @y-lg; }
.pt-xl-sm { padding-top: @y-xl; }
.pr-0-sm { padding-right: @x-0; }
.pr-xs-sm { padding-right: @x-xs; }
.pr-sm-sm { padding-right: @x-sm; }
.pr-md-sm { padding-right: @x-md; }
.pr-lg-sm { padding-right: @x-lg; }
.pr-xl-sm { padding-right: @x-xl; }
.pb-0-sm { padding-bottom: @y-0; }
.pb-xs-sm { padding-bottom: @y-xs; }
.pb-sm-sm { padding-bottom: @y-sm; }
.pb-md-sm { padding-bottom: @y-md; }
.pb-lg-sm { padding-bottom: @y-lg; }
.pb-xl-sm { padding-bottom: @y-xl; }
.pl-0-sm { padding-left: @x-0; }
.pl-xs-sm { padding-left: @x-xs; }
.pl-sm-sm { padding-left: @x-sm; }
.pl-md-sm { padding-left: @x-md; }
.pl-lg-sm { padding-left: @x-lg; }
.pl-xl-sm { padding-left: @x-xl; }
}
@media (max-width: @screen-sm-max) {
.pt-0-md { padding-top: @y-0; }
.pt-xs-md { padding-top: @y-xs; }
.pt-sm-md { padding-top: @y-sm; }
.pt-md-md { padding-top: @y-md; }
.pt-lg-md { padding-top: @y-lg; }
.pt-xl-md { padding-top: @y-xl; }
.pr-0-md { padding-right: @x-0; }
.pr-xs-md { padding-right: @x-xs; }
.pr-sm-md { padding-right: @x-sm; }
.pr-md-md { padding-right: @x-md; }
.pr-lg-md { padding-right: @x-lg; }
.pr-xl-md { padding-right: @x-xl; }
.pb-0-md { padding-bottom: @y-0; }
.pb-xs-md { padding-bottom: @y-xs; }
.pb-sm-md { padding-bottom: @y-sm; }
.pb-md-md { padding-bottom: @y-md; }
.pb-lg-md { padding-bottom: @y-lg; }
.pb-xl-md { padding-bottom: @y-xl; }
.pl-0-md { padding-left: @x-0; }
.pl-xs-md { padding-left: @x-xs; }
.pl-sm-md { padding-left: @x-sm; }
.pl-md-md { padding-left: @x-md; }
.pl-lg-md { padding-left: @x-lg; }
.pl-xl-md { padding-left: @x-xl; }
}
@media (max-width: @screen-md-max) {
.pt-0-lg { padding-top: @y-0; }
.pt-xs-lg { padding-top: @y-xs; }
.pt-sm-lg { padding-top: @y-sm; }
.pt-md-lg { padding-top: @y-md; }
.pt-lg-lg { padding-top: @y-lg; }
.pt-xl-lg { padding-top: @y-xl; }
.pr-0-lg { padding-right: @x-0; }
.pr-xs-lg { padding-right: @x-xs; }
.pr-sm-lg { padding-right: @x-sm; }
.pr-md-lg { padding-right: @x-md; }
.pr-lg-lg { padding-right: @x-lg; }
.pr-xl-lg { padding-right: @x-xl; }
.pb-0-lg { padding-bottom: @y-0; }
.pb-xs-lg { padding-bottom: @y-xs; }
.pb-sm-lg { padding-bottom: @y-sm; }
.pb-md-lg { padding-bottom: @y-md; }
.pb-lg-lg { padding-bottom: @y-lg; }
.pb-xl-lg { padding-bottom: @y-xl; }
.pl-0-lg { padding-left: @x-0; }
.pl-xs-lg { padding-left: @x-xs; }
.pl-sm-lg { padding-left: @x-sm; }
.pl-md-lg { padding-left: @x-md; }
.pl-lg-lg { padding-left: @x-lg; }
.pl-xl-lg { padding-left: @x-xl; }
}
最佳答案
尝试:
@grid-gutter-width: 20px;
@line-height-computed: 40px;
// Margins and padding
// -------------------------
// Horizontal and vertical offset values
@x-0: 0;
@x-xs: (@grid-gutter-width / 4);
@x-sm: (@grid-gutter-width / 2);
@x-md: @grid-gutter-width;
@x-lg: (@grid-gutter-width * 2);
@x-xl: (@grid-gutter-width * 4);
@y-0: 0;
@y-xs: (@line-height-computed / 4);
@y-sm: (@line-height-computed / 2);
@y-md: @line-height-computed;
@y-lg: (@line-height-computed * 2);
@y-xl: (@line-height-computed * 4);
@positions: t top, r right, b bottom, l left;
@grids: 0, xs, md, lg, xl;
.what(@i) when (mod(@i, 2) = 0) {
@what: x;
}
.what(@i) when (default()) {
@what: y;
}
.properties(@prefix, @postfix, @whitespace, @what, @position, @grids, @i: 1) when (@i <= length(@grids)) {
@grid: extract(@grids,@i);
.m@{prefix}-@{grid}@{postfix} {
@var: ~"@{what}-@{grid}";
@{whitespace}-@{position}: @@var;
}
.properties(@prefix, @postfix, @whitespace, @what, @position, @grids, (@i + 1));
}
.whitespace(@positions, @whitespace: margin, @postfix: ~"", @i: 1) when (@i <= length(@positions)) {
@class: extract(extract(@positions, @i), 1);
@position: extract(extract(@positions, @i), 2);
.what(@i);
.properties(@class, @postfix, @whitespace, @what, @position, @grids);
.whitespace(@positions, @whitespace, @postfix, (@i + 1));
}
.whitespace(@positions);
.whitespace(@positions, padding);
@screen-xs-max: 767px;
@screen-sm-max: 767px;
// Responsive
@media (max-width: @screen-xs-max) {
.whitespace(@positions, margin, -sm);
.whitespace(@positions, padding, -sm);
}
@media (max-width: @screen-sm-max) {
.whitespace(@positions, margin, -md);
.whitespace(@positions, padding, -md);
}
上面的代码编译成如下所示的 CSS 代码:
.mt-0 {
margin-top: 0;
}
.mt-xs {
margin-top: 10px;
}
.mt-md {
margin-top: 40px;
}
.mt-lg {
margin-top: 80px;
}
.mt-xl {
margin-top: 160px;
}
.mr-0 {
margin-right: 0;
}
.mr-xs {
margin-right: 5px;
}
.mr-md {
margin-right: 20px;
}
.mr-lg {
margin-right: 40px;
}
.mr-xl {
margin-right: 80px;
}
.mb-0 {
margin-bottom: 0;
}
.mb-xs {
margin-bottom: 10px;
}
.mb-md {
margin-bottom: 40px;
}
.mb-lg {
margin-bottom: 80px;
}
.mb-xl {
margin-bottom: 160px;
}
.ml-0 {
margin-left: 0;
}
.ml-xs {
margin-left: 5px;
}
.ml-md {
margin-left: 20px;
}
.ml-lg {
margin-left: 40px;
}
.ml-xl {
margin-left: 80px;
}
.mt-0 {
padding-top: 0;
}
.mt-xs {
padding-top: 10px;
}
.mt-md {
padding-top: 40px;
}
.mt-lg {
padding-top: 80px;
}
.mt-xl {
padding-top: 160px;
}
.mr-0 {
padding-right: 0;
}
.mr-xs {
padding-right: 5px;
}
.mr-md {
padding-right: 20px;
}
.mr-lg {
padding-right: 40px;
}
.mr-xl {
padding-right: 80px;
}
.mb-0 {
padding-bottom: 0;
}
.mb-xs {
padding-bottom: 10px;
}
.mb-md {
padding-bottom: 40px;
}
.mb-lg {
padding-bottom: 80px;
}
.mb-xl {
padding-bottom: 160px;
}
.ml-0 {
padding-left: 0;
}
.ml-xs {
padding-left: 5px;
}
.ml-md {
padding-left: 20px;
}
.ml-lg {
padding-left: 40px;
}
.ml-xl {
padding-left: 80px;
}
@media (max-width: 767px) {
.mt-0-sm {
margin-top: 0;
}
.mt-xs-sm {
margin-top: 10px;
}
.mt-md-sm {
margin-top: 40px;
}
.mt-lg-sm {
margin-top: 80px;
}
.mt-xl-sm {
margin-top: 160px;
}
.mr-0-sm {
margin-right: 0;
}
.mr-xs-sm {
margin-right: 5px;
}
.mr-md-sm {
margin-right: 20px;
}
.mr-lg-sm {
margin-right: 40px;
}
.mr-xl-sm {
margin-right: 80px;
}
.mb-0-sm {
margin-bottom: 0;
}
.mb-xs-sm {
margin-bottom: 10px;
}
.mb-md-sm {
margin-bottom: 40px;
}
.mb-lg-sm {
margin-bottom: 80px;
}
.mb-xl-sm {
margin-bottom: 160px;
}
.ml-0-sm {
margin-left: 0;
}
.ml-xs-sm {
margin-left: 5px;
}
.ml-md-sm {
margin-left: 20px;
}
.ml-lg-sm {
margin-left: 40px;
}
.ml-xl-sm {
margin-left: 80px;
}
.mt-0-sm {
padding-top: 0;
}
.mt-xs-sm {
padding-top: 10px;
}
.mt-md-sm {
padding-top: 40px;
}
.mt-lg-sm {
padding-top: 80px;
}
.mt-xl-sm {
padding-top: 160px;
}
.mr-0-sm {
padding-right: 0;
}
.mr-xs-sm {
padding-right: 5px;
}
.mr-md-sm {
padding-right: 20px;
}
.mr-lg-sm {
padding-right: 40px;
}
.mr-xl-sm {
padding-right: 80px;
}
.mb-0-sm {
padding-bottom: 0;
}
.mb-xs-sm {
padding-bottom: 10px;
}
.mb-md-sm {
padding-bottom: 40px;
}
.mb-lg-sm {
padding-bottom: 80px;
}
.mb-xl-sm {
padding-bottom: 160px;
}
.ml-0-sm {
padding-left: 0;
}
.ml-xs-sm {
padding-left: 5px;
}
.ml-md-sm {
padding-left: 20px;
}
.ml-lg-sm {
padding-left: 40px;
}
.ml-xl-sm {
padding-left: 80px;
}
}
@media (max-width: 767px) {
.mt-0-md {
margin-top: 0;
}
.mt-xs-md {
margin-top: 10px;
}
.mt-md-md {
margin-top: 40px;
}
.mt-lg-md {
margin-top: 80px;
}
.mt-xl-md {
margin-top: 160px;
}
.mr-0-md {
margin-right: 0;
}
.mr-xs-md {
margin-right: 5px;
}
.mr-md-md {
margin-right: 20px;
}
.mr-lg-md {
margin-right: 40px;
}
.mr-xl-md {
margin-right: 80px;
}
.mb-0-md {
margin-bottom: 0;
}
.mb-xs-md {
margin-bottom: 10px;
}
.mb-md-md {
margin-bottom: 40px;
}
.mb-lg-md {
margin-bottom: 80px;
}
.mb-xl-md {
margin-bottom: 160px;
}
.ml-0-md {
margin-left: 0;
}
.ml-xs-md {
margin-left: 5px;
}
.ml-md-md {
margin-left: 20px;
}
.ml-lg-md {
margin-left: 40px;
}
.ml-xl-md {
margin-left: 80px;
}
.mt-0-md {
padding-top: 0;
}
.mt-xs-md {
padding-top: 10px;
}
.mt-md-md {
padding-top: 40px;
}
.mt-lg-md {
padding-top: 80px;
}
.mt-xl-md {
padding-top: 160px;
}
.mr-0-md {
padding-right: 0;
}
.mr-xs-md {
padding-right: 5px;
}
.mr-md-md {
padding-right: 20px;
}
.mr-lg-md {
padding-right: 40px;
}
.mr-xl-md {
padding-right: 80px;
}
.mb-0-md {
padding-bottom: 0;
}
.mb-xs-md {
padding-bottom: 10px;
}
.mb-md-md {
padding-bottom: 40px;
}
.mb-lg-md {
padding-bottom: 80px;
}
.mb-xl-md {
padding-bottom: 160px;
}
.ml-0-md {
padding-left: 0;
}
.ml-xs-md {
padding-left: 5px;
}
.ml-md-md {
padding-left: 20px;
}
.ml-lg-md {
padding-left: 40px;
}
.ml-xl-md {
padding-left: 80px;
}
}
关于css - Bootstrap 响应式边距和 Padding Less/CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31571924/
我在尝试使用 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:
我是一名优秀的程序员,十分优秀!