为什么要模块化?

  • 分治和复用
  • 封装,不污染全局,不被全局污染。
  • 继承

BEM(block:块,Element:元素,Modifier:修饰符)策略

.block__Element--Modifier
.块__元素--修饰

封装靠人为约定。

继承靠层叠特性。

基础组件样式

分治性强,复用性强。

.Button { /* Button通用样式 */ }
.Button--disabled { }
.Button--error { }
.Button--in-progress { }
<button class="Button Button--in-progress">Processing...</button>

缺点

人为约定总是不靠谱,有人覆盖了就呵呵哒。

业务组件样式

分治性强,复用性弱。

.product-details {}
.product-details__price {}
.product-details__price__num {}
.product-details__price__icon {} .product-details__price--sale {}
<!-- product/details.html -->
<div name="price" class="product-details__price"></div>

缺点

需求肯定会变,万一目录(或者DOM上下级结构)调整了就嘿嘿嘿。

我想product/intro.html也要用到.product-details__price__icon{}样式咋办。

还有,代码冗余了。

Less的策略

跨文件继承

使用@import引入文件,再调用变量或者mixins。

缺点

变量命名冲突

css in js策略(限于React)

  • 独一无二的命名,保证了封装。
  • js映射文件,保证了开发的便利。
  • 继承方式,还是利用层叠特性。

封装和继承

css原文件

.common { /* font-sizes, padding, border-radius */ }
.normal { composes: common; /* blue color, light blue background */ }
.error { composes: common; /* red color, light red background */ }

html原文件

<button class="normal">
Submit
</button>

生成的的css文件

.components_submit_button__common__abc5436 { /* font-sizes, padding, border-radius */ }
.components_submit_button__normal__def6547 { /* blue color, light blue background */ }
.components_submit_button__error__1638bcd { /* red color, light red background */ }

生成的js映射文件

styles: {
common: "components_submit_button__common__abc5436",
normal: "components_submit_button__common__abc5436 components_submit_button__normal__def6547",
error: "components_submit_button__common__abc5436 components_submit_button__error__1638bcd"
}

编译后的html文件

/*html*/
<button class="components_submit_button__common__abc5436 components_submit_button__normal__def6547">
Submit
</button>

跨文件继承

colors.css

.primary {
color: #720;
}
.secondary {
color: #777;
}

submit-button.css

.common { /* font-sizes, padding, border-radius */ }
.normal {
composes: common;
composes: primary from "./colors.css";
}

编译后的colors.css

.shared_colors__primary__fca929 {
color: #720;
}
.shared_colors__secondary__acf292 {
color: #777;
}

编译后的submit-button.css

.components_submit_button__common__abc5436 { /* font-sizes, padding, border-radius */ }
.components_submit_button__normal__def6547 {}
<button class="shared_colors__primary__fca929
components_submit_button__common__abc5436
components_submit_button__normal__def6547">
Submit
</button>

总结

规范是人工约定的模块化,CSS in js是机器制定的模块化。

css模块化策略的更多相关文章

  1. 谈 CSS 模块化

    以前看过模块化的相关资料以及解释,对模块化有了一个表皮的了解,自己也做了一些相关的实践,由于接触到的项目交小,所以也没能更好的去体现和理解模块化,但总体还是有那么一些感悟,但是如果要说怎么才能算是好的 ...

  2. css模块化思想(一)--------命名是个技术活

    引子: 女孩子都喜欢买衣服,而我也不例外,奈何钱包太瘦,买不起高大上的定制,只能买撞衫率极高的休闲衣,不过对于我来说,我还是开心的,毕竟买衣服买的不仅是衣服,更是一种心情.在web前端的世界里,css ...

  3. 谈CSS模块化【封装-继承-多态】

    第一次听到“CSS模块化”这个词是在WebReBuild的第四届“重构人生”年会上,当时我还想,“哈,CSS也有模块化,我没听错吧?”事实上,我没听错,你也没看错,早就有CSS模块化这个概念了.之所以 ...

  4. 浅谈CSS模块化

    为什么要CSS模块化? 你是否为class命名而感到苦恼? 你是否有怕跟别人使用同样class名而感到担忧? 你是否因层级结构不清晰而感到烦躁? 你是否因代码难以复用而感到不爽? 你是否因为commo ...

  5. css模块化及CSS Modules使用详解

    什么是css模块化? 为了理解css模块化思想,我们首先了解下,什么是模块化,在百度百科上的解释是,在系统的结构中,模块是可组合.分解和更换的单元.模块化是一种处理复杂系统分解成为更好的可管理模块的方 ...

  6. React(八)样式及CSS模块化

    (1)内联样式 注:样式要采用驼峰命令发,如果非要使用原生css样式写法,需加引号 缺点:一些动画,伪类不能使用 class App extends Component { constructor(p ...

  7. Vue中CSS模块化最佳实践

    Vue风格指南中介绍了单文件组件中的Style是必须要有作用域的,否则组件之间可能相互影响,造成难以调试. 在Vue Loader Scope CSS和Vue Loader CSS Modules两节 ...

  8. css模块化思想(一)

    什么是css模块化思想?(what) 为了理解css模块化思想,我们首先了解下,什么是模块化,在百度百科上的解释是,在系统的结构中,模块是可组合.分解和更换的单元.模块化是一种 处理复杂系统分解成为更 ...

  9. CSS模块化思想-----命名是个技术活

    CSS模块化思想(一)--------命名是个技术活 引子: 女孩子都喜欢买衣服,而我也不例外,奈何钱包太瘦,买不起高大上的定制,只能买撞衫率极高的休闲衣,不过对于我来说,我还是开心的,毕竟买衣服买的 ...

随机推荐

  1. 【iCore3 双核心板】例程十六:USB_HID实验——双向数据传输

    实验指导书及代码包下载: http://pan.baidu.com/s/1bojcVoV iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...

  2. what's the difference between dim as and dim as new?

    what's the difference between dim as and dim as new? There is no difference with value types (Intege ...

  3. Thinking in Java——笔记(9)

    Polymorphism Abstract classes and methods If you have an abstract class, objects of that specific cl ...

  4. block(代码块)的介绍以及使用方法和变量之间的关系

    http://blog.csdn.net/menxu_work/article/details/8762848 block(代码块)的介绍以及使用方法和变量之间的关系 block(代码块)的介绍以及使 ...

  5. 关于Matrix的深入理解(对应值的功能)

    matrix是css3里面transform的一个集成表达式,它是以一个矩阵的方式来计算 transform:translate(apx,bpx)位移 transform: scale(a,b) 放缩 ...

  6. iOS 深浅拷贝

    -(void)copyDemo { // 在非集合类对象中:对immutable对象进行copy操作,是指针复制,mutableCopy操作时内容复制:对mutable对象进行copy和mutable ...

  7. html2canvaces用法,js截屏并且下载

    1.首先自己下载引入html2canvaces和jquery(我的demo路径是自己本地的) 2.点击截屏按钮后,跳出层的其他部分是取消保存,点击截取获得的图片区域,表示保存 <!DOCTYPE ...

  8. Java基础之在窗口中绘图——绘制星星(StarApplet 1)

    Applet程序. 可以把更复杂的几何形状定义为GeneralPath类型的对象.GeneralPath可以是直线.Quad2D曲线和Cubic2D曲线的结合体,甚至可以包含其他GeneralPath ...

  9. SQL*Plus命令行工具连接Oracle数据库

    1.在命令行中输入"sqlplus /nolog"即可启动该工具. 2.连接到Oracle服务器  conn 用户名/密码@服务器连接字符串 as 连接身份 客户端工具根据&quo ...

  10. js 格式化数字保留2位小数

    function toDecimal2(x) { var f = parseFloat(x); if (isNaN(f)) { return false; } var f = Math.round(x ...