.<module>[-<component>][-<state>] {}
这种体现模块和组件的命名方式CSS 属性声明顺序:
.declaration-order {
/* Positioning */
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
z-index: 100;
/* Box-model */
display: block;
float: right;
width: 100px;
height: 100px;
/* Typography */
font: normal 13px "Helvetica Neue", sans-serif;
line-height: 1.5;
color: #333;
text-align: center;
/* Visual */
background-color: #f5f5f5;
border: 1px solid #e5e5e5;
border-radius: 3px;
/* Misc */
opacity: 1;
}
app-Component-class
。>
限定。all: initial
,Shadow DOM,<iframe>
。最少两个字符,用-
分隔。
.like-button
).search-form
).article-card
).rico-custom-header
)有的组件可以用一个单词就表示,这时可以加后缀使它更清晰。
block-level element:
.alert-box
.alert-card
.alert-block
Or for inlines:
.link-button
.link-span
避免标签选择器。
尽量使用>
子元素选择器,而不是后代选择器。
尽量一个单词,多个单词不要用横线和下划线链接,例如:
.profile-box {
> h3 { /* ? avoid */ }
.title { /* okay */ }
> .author { /* ? better */ }
> .firstname { /* ... */ }
> .lastname { /* ... */ }
> .avatar { /* ... */ }
}
变体由横线开头。
.like-button {
&.-wide { /* ... */ }
&.-short { /* ... */ }
&.-disabled { /* ... */ }
}
.shopping-card {
> .title { /* ... */ }
> .title.-small { /* ... */ }
}
横线开头的好处:
-
和_
开头gcc -O2 -Wall -emit-last
)不要进入组件修改样式,而是使用变体。
不要这样:
.article-header {
> .vote-box > .up { /* ? avoid this */ }
}
要这样:
<div class='article-header'>
<div class='vote-box -highlight'>
...
</div>
...
</div>
.vote-box {
&.-highlight > .up { /* ... */ }
}
推荐使用继承进行简化。
不推荐:
<div class='search-form'>
<input class='input' type='text'>
<button class='search-button -red -large'></button>
</div>
推荐:
<div class='search-form'>
<input class='input' type='text'>
<button class='submit'></button>
</div>
.search-form {
> .submit {
@extend .search-button;
@extend .search-button.-red;
@extend .search-button.-large;
}
}
因为组件会用在各种上下文,因此要避免使用:
position
, top
, left
, right
, bottom
)float
, clear
)margin
)width
, height
) *上述 CSS 属性应该设置在父元素,例如:
.article-list {
& {
@include clearfix;
}
> .article-card {
width: 33.3%;
float: left;
}
}
.article-card {
& { /* ... */ }
> .image { /* ... */ }
> .title { /* ... */ }
> .category { /* ... */ }
}
下划线开头,用来覆盖某些属性。不建议多用!
._unmargin { margin: 0 !important; }
._center { text-align: center !important; }
._pull-left { float: left !important; }
._pull-right { float: right !important; }
一个文件一个组件,可以 @import ‘components/*‘;
这样引入全部组件的样式。
最多使用一层嵌套,这样可读性好。
/* ? Avoid: 3 levels of nesting */
.image-frame {
> .description {
/* ... */
> .icon {
/* ... */
}
}
}
/* ? Better: 2 levels */
.image-frame {
> .description { /* ... */ }
> .description > .icon { /* ... */ }
}
RSCSS | BEM | SMACSS |
---|---|---|
Component | Block | Module |
Element | Element | Sub-Component |
Layout | ? | Layout |
Variant | Modifier | Sub-Module & State |
ITCSS 代表_Inverted Triangle CSS_,它可以帮助您组织项目 CSS 文件,从而可以更好地处理(但并不总是易于处理)CSS 细节,例如全局命名空间,层叠和选择器专一性。
ITCSS 的主要原则之一是将 CSS 代码库分为几个部分(称为_层_),这些部分采用倒三角形的形式:
倒三角形还显示了选择器代表的样式在结果 CSS 中的排序方式:从通用样式到显式样式,从低特定选择器到高特定的选择器(也不是很特定,因为不允许使用 ID),以及从广泛到局部。
ITCSS 与BEMIT 命名约定结合使用,使您可以将更多精力放在解决前端挑战上,而不用考虑名称和样式的位置。这是Xfive.co
的main.scss
@import "settings.colors";
@import "settings.global";
@import "tools.mixins";
@import "normalize-scss/normalize.scss";
@import "generic.reset";
@import "generic.box-sizing";
@import "generic.shared";
@import "elements.headings";
@import "elements.hr";
@import "elements.forms";
@import "elements.links";
@import "elements.lists";
@import "elements.page";
@import "elements.quotes";
@import "elements.tables";
@import "objects.animations";
@import "objects.drawer";
@import "objects.list-bare";
@import "objects.media";
@import "objects.layout";
@import "objects.overlays";
@import "components.404";
@import "components.about";
@import "components.archive";
@import "components.avatars";
@import "components.blog-post";
@import "components.buttons";
@import "components.callout";
@import "components.clients";
@import "components.comments";
@import "components.contact";
@import "components.cta";
@import "components.faq";
@import "components.features";
@import "components.footer";
@import "components.forms";
@import "components.header";
@import "components.headings";
@import "components.hero";
@import "components.jobs";
@import "components.legal-nav";
@import "components.main-cta";
@import "components.main-nav";
@import "components.newsletter";
@import "components.page-title";
@import "components.pagination";
@import "components.post-teaser";
@import "components.process";
@import "components.quote-banner";
@import "components.offices";
@import "components.sec-nav";
@import "components.services";
@import "components.share-buttons";
@import "components.social-media";
@import "components.team";
@import "components.testimonials";
@import "components.topbar";
@import "components.reasons";
@import "components.wordpress";
@import "components.work-list";
@import "components.work-detail";
@import "vendor.prism";
@import "trumps.clearfix";
@import "trumps.utilities";
@import "healthcheck";
原文:https://www.cnblogs.com/jffun-blog/p/12046199.html