LESS mixins vs classes

核能气质少年 提交于 2019-12-11 05:51:33

问题


I'm looking into LESS because I definitely see some of their benefits. For instance colour declaration.

One thing I don't understand tho, and maybe I'm not getting the flow right is - why use the following LESS snippet

.radius {
  -webkit-border-radius:5px;
  -moz-border-radius:5px;
  border-radius:5px;  
}
.btn-red{
  background-color:red;
  .radius;     
}
.btn-green{
  background-color:green;
  .radius;
}
...

When we can use the .radius class in the html file right away. I'm left with the impression that LESS will add a ton of duplicate code once it gets compiled.

I'm using the following, which makes more sense. Same with font-size, margins, etc... Aren't classes used in such cases?

<div class="btn-red radius">Cancel</div>
<div class="btn-green radius">Go</div>

回答1:


The snippet above does not benefit from SASS/LESS capabilities that much. Lets have a closer look and check this SCSS snippet.

// Abstract placeholder.
%radius {
  border-radius: 5px;
}

// Put your global styling here.
// I'm assuming that you can alter the markup and have button.btn.btn-green
.btn {

  // Color modifier.
  &-red {
    @extend %radius;
    background-color: red;
  }
  &-green {
    @extend %radius;
    background-color: green;
  }
}

The CSS output will be:

.btn-red, .btn-green {
  border-radius: 5px;
}

.btn-red {
  background-color: red;
}
.btn-green {
  background-color: green;
}

And then you have to pick up Autoprefixer and vendor-prefixes issue is solved once and for all.




回答2:


Because now, you can just specify the class btn_red or btn_green and all the buttons will automatically have a radius.

Your HTML should contain only the semantics, and styling or classes referring to styling should not be part of it.

That applies to the other classes as well. If for instance, you would rename btn_red to btn_cancel, you have a meaningful classname that you can apply to any kind of cancel button. And in the CSS you can specify that a cancel button is red and a 'Go' button is green, and both have a radius, without needing to modify the HTML at all.

So, the ultimate goal is to have the HTML describe the structure and the CSS describe how that structure should look. And a CSS preprocessor is only their to make a bulky spaghetti-like CSS file more structured.




回答3:


There are several benefits.

  1. You can use more semantic class names. Rather than encoding style information directly in your class names, (btn-red, radius) you could use a single class that conveys the usage of the style, rather than its contents.

  2. You can avoid repeating yourself. @radius-size: 5px; -webkit-border-radius:@radius-size; -moz-border-radius:@radius-size; border-radius:@radius-size;

  3. You can parameterize it so that you'd be able to use different radiuses (radii?) in different contexts. .radius(@radius-size) { ... }




回答4:


Because there are cases that developer has-no-access or don't-want to change the markup. and the only solution is to include all props from a predefined class.

for example:

you have bootstrap loaded (then you already have .has-success and .has-error classes) and if you want to use HTML5's native form validation using input's :valid and :invalid states, you have to use JavaScript to add/remove success/error classes based on input's states. but with this feature of LESS you can include all props of success/error class inside input's states. the code for this example could be something like this:

#myinput {
    &:valid { .has-success; }
    &:invalid { .has-error; }
}


来源:https://stackoverflow.com/questions/31389198/less-mixins-vs-classes

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