手册
目录
任何超过 1000 行的 CSS 代码,你都曾经历过这样的体验:
1.这个 class 到底是什么意思呢?
2.这个 class 在哪里被使用呢?
3.如果我创建一个 xxoo class,会造成冲突吗?
Reasonable System for CSS Stylesheet Structure 的目标就是解决以上问题,它不是一个框架,而是通过规范,让你构建更健壮和可维护的 CSS 代码。

从 Components 的角度思考,将网站的模块都作为一个独立的 Components。
Components 最少以两个单词命名,通过 - 分离,例如:
.like-button).search-form).article-card)
Elements 是 Components 中的元素
Elements 的类名应尽可能仅有一个单词。
.search-form {
> .field { /* ... */ }
> .action { /* ... */ }
}对于倘若需要两个或以上单词表达的 Elements 类名,不应使用中划线和下划线连接,应直接连接。
.profile-box {
> .firstname { /* ... */ }
> .lastname { /* ... */ }
> .avatar { /* ... */ }
}任何时候尽可能使用 classnames。标签选择器在使用上没有问题,但是其性能上稍弱,并且表意不明确。
.article-card {
> h3 { /* ✗ avoid */ }
> .name { /* ✓ better */ }
}
Components 和 Elements 可能都会拥有 Variants。
Variants 的 classname 应带有前缀中划线 -
.like-button {
&.-wide { /* ... */ }
&.-short { /* ... */ }
&.-disabled { /* ... */ }
} .shopping-card {
> .title { /* ... */ }
> .title.-small { /* ... */ }
}为什么使用中划线作为变体的前缀?
Elements_ 或 - 开头
Components 应该在不同的上下文中都可以复用,所以应避免设置以下属性:
Positioning (position, top, left, right, bottom)
Floats (float, clear)
Margins (margin)
Dimensions (width, height) *
头像和 logos 这些元素应该设置固定尺寸(宽度,高度...)。
倘若你需要为组件设置定位,应将在组件的上下文(父元素)中进行处理,比如以下例子中,将 widths和 floats 应用在 list component(.article-list) 当中,而不是 component(.article-card) 自身。
.article-list {
& {
@include clearfix;
}
> .article-card {
width: 33.3%; float: left;
}
}
.article-card {
& { /* ... */ }
> .image { /* ... */ }
> .title { /* ... */ }
> .category { /* ... */ }
}当出现多个嵌套的时候容易失去控制,应保持不超过一个嵌套。
/* ✗ Avoid: 3 levels of nesting */
.image-frame {
> .description { /* ... */
> .icon { /* ... */
}
}
} /* ✓ Better: 2 levels */
.image-frame {
> .description { /* ... */ }
> .description > .icon { /* ... */ }
}中划线-是一坨糟糕的玩意:其实你可以选择性的使用,只要将 Components, Elements, Variants记在心上即可。
我有时候想不出两个单词唉:有些组件的确使用一个单词就能表意,比如 aleter 。但其实你可以使用后缀,使其意识更加明确。
比如块级元素:
.alert-box
.alert-card
.alert-block
或行内级元素
.link-button
.link-span
RSCSS 与其他 CSS 模块组织系统相似的概念

以 Components 的角度思考,以两个单词命名(.screenshot-image)
Components 中的 Elements,以一个单词命名(.blog-post .title)
Variants,以中划线-作为前缀(.shop-banner.-with-icon)
Components 可以互相嵌套
记住,你可以通过继承让事情变得更简单
相关
视频
RELATED VIDEOS
科技资讯
1
2
3
4
5
6
7
8
9
精选课程
共5课时
17.2万人学习
共49课时
77万人学习
共29课时
61.7万人学习
共25课时
39.3万人学习
共43课时
70.9万人学习
共25课时
61.6万人学习
共22课时
23万人学习
共28课时
33.9万人学习
共89课时
125万人学习