编码规范总结 源自bootstrap
2015-01-17 15:22
197 查看
属性顺序
HTML 属性应当按照以下给出的顺序依次排列,确保代码的易读性。class
id,
name
data-*
src,
for,
type,
href
title,
alt
aria-*,
role
class 用于标识高度可复用组件,因此应该排在首位。id 用于标识具体组件,应当谨慎使用(例如,页面内的书签),因此排在第二位。
声明顺序
相关的属性声明应当归为一组,并按照下面的顺序排列:Positioning
Box model
Typographic
Visual
由于定位(positioning)可以从正常的文档流中移除元素,并且还能覆盖盒模型(box model)相关的样式,因此排在首位。盒模型排在第二位,因为它决定了组件的尺寸和位置。
其他属性只是影响组件的内部(inside)或者是不影响前两组属性,因此排在后面。
.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;
带前缀的属性
当使用特定厂商的带有前缀的属性时,通过缩进的方式,让每个属性的值在垂直方向对齐,这样便于多行编辑。在 Textmate 中,使用 [b]Text → Edit Each Line in Selection(⌃⌘A)。在 Sublime Text 2 中,使用 Selection → Add Previous Line (⌃⇧↑) 和 Selection → Add Next Line (⌃⇧↓)。
/* Prefixed properties */ .selector { -webkit-box-shadow: 0 1px 2px rgba(0,0,0,.15); box-shadow: 0 1px 2px rgba(0,0,0,.15); }
Less 和 Sass 中的嵌套
避免非必要的嵌套。这是因为虽然你可以使用嵌套,但是并不意味着应该使用嵌套。只有在必须将样式限制在父元素内(也就是后代选择器),并且存在多个需要嵌套的元素时才使用嵌套。// Without nesting .table > thead > tr > th { … } .table > thead > tr > td { … } // With nesting .table > thead > tr { > th { … } > td { … } }
class 命名
class 名称中只能出现小写字符和破折号(dashe)(不是下划线,也不是驼峰命名法)。破折号应当用于相关 class 的命名(类似于命名空间)(例如,.btn和
.btn-danger)。
避免过度任意的简写。
.btn代表 button,但是
.s不能表达任何意思。
class 名称应当尽可能短,并且意义明确。
使用有意义的名称。使用有组织的或目的明确的名称,不要使用表现形式(presentational)的名称。
基于最近的父 class 或基本(base) class 作为新 class 的前缀。
使用
.js-*class 来标识行为(与样式相对),并且不要将这些 class 包含到 CSS 文件中。
在为 Sass 和 Less 变量命名是也可以参考上面列出的各项规范。
/* Bad example */ .t { ... } .red { ... } .header { ... } /* Good example */ .tweet { ... } .important { ... } .tweet-header { ... }
[/b]
相关文章推荐
- bootstrap使用总结
- 最通俗PLC教程—源自Koyo光洋PLC自学总结(1)
- bootStrap总结
- BootStrap 常用控件总结
- bootstrap使用总结
- bootstrap的总结
- [布局] bootstrap基本标签总结
- bootstrap中的栅格样式总结
- 栋栋晓08:Bootstrap学习总结:栅格系统
- 最通俗PLC教程—源自Koyo光洋PLC自学总结(2)
- NodeJS+Express+mongoose+Jade+bootstrap+jquery+inspector(调试工具)开发网站demo总结
- bootstrap使用总结
- 30多年程序员生涯经验总结(成功源自于失败中的学习;失败则是因为容忍错误的横行)
- BootStrap 常用控件总结
- 最通俗PLC教程—源自Koyo光洋PLC自学总结(3) 推荐
- Bootstrap启动代码分析总结二
- 阅读bootstrap2 中的bootstrap-responsive.css源文件总结的知识点
- [布局]bootstrap基本标签总结2
- **bootstrap常见常用样式总结
- bootstrap总结