CSS 规范

大约 4 分钟

CSS 规范

通用规范

  1. 统一使用"-"连字符
  2. 省略值为 0 时的单位
// bad

padding-bottom: 0px;
margin: 0em;

// good
padding-bottom: 0;
margin: 0;
  1. 如果 CSS 可以做到,就不要使用 JS

  2. 建议并适当缩写值,提高可读性,特殊情况除外
    “建议并适当”是因为缩写总是会包含一系列的值,而有时候我们并不希望设置某一值,反而造成了麻烦,那么这时候你可以不缩写,而是分开写。
    当然,在一切可以缩写的情况下,请务必缩写,它最大的好处就是节省了字节,便于维护,并使阅读更加一目了然.

// bad
.box {
  border-top-style: none;
  font-family: palatino, georgia, serif;
  font-size: 100%;
  line-height: 1.6;
  padding-bottom: 2em;
  padding-left: 1em;
  padding-right: 1em;
  padding-top: 0;
}

// good
.box {
  border-top: 0;
  font: 100%/1.6 palatino, georgia, serif;
  padding: 0 1em 2em;
}

声明应该按照下表的顺序

左到右,从上到下

显示属性自身属性文本属性和其他修饰
displaywidthfont
visibilityheighttext-align
positionmargintext-decoration
floatpaddingvertical-align
clearborderwhite-space
list-styleoverflowcolor
topmin-widthbackground
// bad

.box {
  font-family: 'Arial', sans-serif;
  border: 3px solid #ddd;
  left: 30%;
  position: absolute;
  text-transform: uppercase;
  background-color: #eee;
  right: 30%;
  isplay: block;
  font-size: 1.5rem;
  overflow: hidden;
  padding: 1em;
  margin: 1em;
}

// good

.box {
  display: block;
  position: absolute;
  left: 30%;
  right: 30%;
  overflow: hidden;
  margin: 1em;
  padding: 1em;
  background-color: #eee;
  border: 3px solid #ddd;
  font-family: 'Arial', sans-serif;
  font-size: 1.5rem;
  text-transform: uppercase;
}
  1. 元素选择器应该避免在 scoped 中出现

在 scoped 样式中,类选择器比元素选择器更好,因为大量使用元素选择器是很慢的。

  1. 分类的命名方法

使用单个字母加上"-"为前缀

布局(grid)(.g-);

模块(module)(.m-);

元件(unit)(.u-);

功能(function)(.f-);

皮肤(skin)(.s-);

状态(.z-)。

  1. 统一语义理解和命名
    布局(.g-)
语义命名简写
文档docdoc
头部headhd
主体bodybd
尾部footft
主栏mainmn
主栏子容器maincmnc
侧栏sidesd
侧栏子容器sidecsdc
盒容器wrap/boxwrap/box

模块(.m-)、元件(.u-)

语义命名简写
导航navnav
子导航subnavsnav
面包屑crumbcrm
菜单menumenu
选项卡tabtab
标题区head/titlehd/tt
内容区body/contentbd/ct
列表listlst
表格tabletb
表单formfm
热点hothot
排行toptop
登录loginlog
标志logologo
广告advertisead
搜索searchsch
幻灯slidesld
提示tipstips
帮助helphelp
新闻newsnews
下载downloaddld
注册registreg
投票votevote
版权copyrightcprt
结果resultrst
标题titlett
按钮buttonbtn
输入inputipt

功能(.f-)

语义命名简写
浮动清除clearbothcb
向左浮动floatleftfl
向右浮动floatrightfr
内联块级inlineblockib
文本居中textaligncentertac
文本居右textalignrighttar
文本居左textalignlefttal
垂直居中verticalalignmiddlevam
溢出隐藏overflowhiddenoh
完全消失displaynonedn
字体大小fontsizefs
字体粗细fontweightfw

皮肤(.s-)

语义命名简写
字体颜色fontcolorfc
背景backgroundbg
背景颜色backgroundcolorbgc
背景图片backgroundimagebgi
背景定位backgroundpositionbgp
边框颜色bordercolorbdc

状态(.z-)

语义命名简写
选中selectedsel
当前currentcrt
显示showshow
隐藏hidehide
打开openopen
关闭closeclose
出错errorerr
不可用disableddis

less 规范

  • 当使用 Less 的嵌套功能的时候,重要的是有一个明确的嵌套顺序,以下内容是一个 Less 块应具有的顺序。 1. 当前选择器的样式属性 2. 父级选择器的伪类选择器 (:first-letter, :hover, :active etc) 3. 伪类元素 (:before and :after) 4. 父级选择器的声明样式 (.selected, .active, .enlarged etc.) 5. 用 Sass 的上下文媒体查询 6. 子选择器作为最后的部分
.product-teaser {
  // 1. Style attributes
  display: inline-block;
  padding: 1rem;
  background-color: whitesmoke;
  color: grey;

  // 2. Pseudo selectors with parent selector
  &:hover {
    color: black;
  }

  // 3. Pseudo elements with parent selector

  &:before {
    content: '';
    display: block;
    border-top: 1px solid grey;
  }

  &:after {
    content: '';
    display: block;
    border-top: 1px solid grey;
  }

  // 4. State classes with parent selector
  &.active {
    background-color: pink;
    color: red;
    // 4.2. Pseuso selector in state class selector

    &:hover {
      color: darkred;
    }
  }

  // 5. Contextual media queries
  @media screen and (max-width: 640px) {
    display: block;
    font-size: 2em;
  }

  // 6. Sub selectors

  > .content > .title {
    font-size: 1.2em;

    // 6.5. Contextual media queries in sub selector

    @media screen and (max-width: 640px) {
      letter-spacing: 0.2em;
      text-transform: uppercase;
    }
  }
}

特殊规范

  • 对用页面级组件样式,应该是有作用域的
  • 对于公用组件或者全局组件库,我们应该更倾向于选用基于 class 的 BEM 策略
  <style lang='scss'></style> // bad

  <!-- 使用 scoped 作用域 -->

  <style lang='scss' scoped></style> // good

  <!-- 使用 BEM 约定 -->

  <style> // good

  .c-Button {
    border: none;
    border-radius: 2px;
  }

  .c-Button--close {
    background-color: red;
  }
  </style>
上次编辑于: