html Sass:定义健壮的CSS模块/组件#sass的方法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了html Sass:定义健壮的CSS模块/组件#sass的方法相关的知识,希望对你有一定的参考价值。

<ul class="horizontal-list">
  <li class="horizontal-list\item">Item</li>
  <li class="horizontal-list\item">Item</li>
  <li class="horizontal-list\item">Item</li>
</ul>
.horizontal-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.horizontal-list\\item {
  display: inline-block;
  margin: 0;
  padding: 0;
}

.horizontal-list > * + * {
  margin-left: 0.5em;
}
// ----
// Sass (v3.4.7)
// Compass (v1.0.1)
// ----

// Sass: The Way to Define Robust CSS Module/Component
//
// Using back-slash as the namespace separator in HTML/CSS
// https://gist.github.com/whizark/ea2ba0ff3f47956fda0f
//
// Other ideas
// https://github.com/whizark/xass#ideas 

// You may use single back-slash as the namespace
// for Mixin/Function/Placeholder selector in Sass,
// but you must use double back-slash in CSS.

// The reason I use placeholder for base definitions,
// wrap it with mixin
// and use mixin in other modules/client code is
// that extending is fragile and a bad practice for extension.
// Mixin encapsulates inner extending in this case.

// Module Definitions
// ========================
// Defines base styles
// for Horizontal List.
%vendor\\horizontal-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

// Wraps base styles and extends them
// for Horizontal List.
@mixin vendor\\horizontal-list($spacing: .5em) {
  // Wraping placeholder, it reduces duplicated code
  // and it's opened for extension. 
  @extend %vendor\\horizontal-list;

  // Lobotomized Owl Selector
  // http://alistapart.com/article/axiomatic-css-and-lobotomized-owls
  > * + * {
    margin-left: $spacing;
  }
}

// Defines base styles
// for Horizontal List Item.
%vendor\\horizontal-list\\item {
  display: inline-block;
  margin: 0;
  padding: 0;
}

// Wraps base styles and extends them
// for Horizontal List Item.
@mixin vendor\\horizontal-list\\item() {
  @extend %vendor\\horizontal-list\\item;
}

// Client Code: Concrete Classes
// =====================================================
// Using mixin, it may accept arguments
// and it's closed for modification.
.horizontal-list {
  @include vendor\\horizontal-list();
}

.horizontal-list\\item {
  @include vendor\\horizontal-list\\item();
}
<ul class="horizontal-list">
  <li class="horizontal-list\item">Item</li>
  <li class="horizontal-list\item">Item</li>
  <li class="horizontal-list\item">Item</li>
</ul>

以上是关于html Sass:定义健壮的CSS模块/组件#sass的方法的主要内容,如果未能解决你的问题,请参考以下文章

Sass 与 CSS 模块和 Webpack

Angular 2/4:为每个模块加载 sass 变量

未加载 VueJs 组件节点模块 css/js

将 sass/css 文件导入所有 Angular 组件

Sass 愿景

html OOSASS:编写类似OOP的CSS组件(工厂,访问器,依赖注入等)#sass