关于一些小细节
- 在提交代码之前,把代码中的打印输出之类删除,比如:console.log()
一、目录结构
-
二、HTML
HTML属性应该按照特定的顺序出现以保证易读性
id class name data-xxx src, for, type, href title, alt aria-xxx, role value style
-
二、CSS
一行代码结束,在末尾添加上分号 ```css / bad / div { color: red }
/ good / div { color: red; }
2. 流。尽量不要改变元素默认行为。保持默认的文本流。比如,```css/* 移出一个图片下面的一个白块,不影响原本的显示:*//* bad */img {display: block;}/* good */img {vertical-align: middle;}/* 类似的,尽量不要改变浮动方式。 *//* bad */div {width: 100px;position: absolute;right: 0;}/* good */div {width: 100px;margin-left: auto;}
- 选择器 ```css / 紧密耦合DOM选择器,三个层级以上建议加class: / / bad / div:first-of-type :last-child > p ~ *
/ good / div:first-of-type .info
/ 紧密耦合DOM选择器,三个层级以上建议加class: / / bad / img[src$=svg], ul > li:first-child { opacity: 0; }
/ good / [src$=svg], ul > :first-child { opacity: 0; }
4. 指明```css/* 不要让代码难于重写,让选择器更精确,减少ID、避免使用!important *//* bad */.bar {color: green !important;}.foo {color: red;}/* good */.foo.bar {color: green;}.foo {color: red;}
- 覆盖 ```css / 覆盖样式会使维护和调试更困难,所以要尽量避免。 / / bad / li { visibility: hidden; } li:first-child { visibility: visible; }
/ good / li + li { visibility: hidden; }
6. 继承```css/* 不要把可继承的样式重复声明: *//* bad */div h1, div p {text-shadow: 0 1px 0 #fff;}/* good */div {text-shadow: 0 1px 0 #fff;}
- 简洁性 ```css / 保持代码的简洁。使用属性缩写。不必要的值不用写。 / / bad / div { transition: all 1s; top: 50%; margin-top: -10px; padding-top: 5px; padding-right: 10px; padding-bottom: 20px; padding-left: 10px; }
/ good / div { transition: 1s; top: calc(50% - 10px); padding: 5px 10px 20px; }
8. 语言```css/* 能用英文的时候不用数字。 *//* bad */:nth-child(2n + 1) {transform: rotate(360deg);}/* good */:nth-child(odd) {transform: rotate(1turn);}
- 动画 ```css / 除了变形和改变透明度用animation,其他尽量使用transition。 / / bad / div:hover { animation: move 1s forwards; } @keyframes move { 100% { margin-left: 100px; } }
/ good / div:hover { transition: 1s; transform: translateX(100px); }
10. 单位。这个先占个位```css/* 可以不用单位时就不用。建议用rem。时间单位用s比ms好。 */
- 颜色 ```css / 需要做透明效果是用rgba,否则都用16进制表示: / / bad / div { color: hsl(103, 54%, 43%); }
/ good / div { color: #5a3; } ```
