最新消息:前端博客、web前端博客、Angularjs、javascript、jQuery、HTML5、CSS3

最全面的前端开发指南

CSS&HTML AZ 1699浏览 0评论

HTML
语义
HTML5为我们提供了很多旨在精确描述内容的语义元素。确保你可以从它丰富的词汇中获益。

01

02

03

04

05

Blog post

06

Published: 21st Feb, 2015

07

08

09

10

11

12

13

14

15

16

Blog post

17

Published:

18

19

20

21

你需要理解你正在使用的元素的语义。用一种错误的方式使用语义元素比保持中立更糟糕。

01

02

03

04
Company
05

06

07

08

09

10
Company
11

回到顶部
简洁
保持代码的简洁。忘记原来的XHTML习惯。

01

02
<!doctype html>
03
04
05

06
Contact
07
08
09
10

Contact me

11

15

16

17

01

02
<!doctype html>
03
04 05 Contact 06 07

08

Contact me

09

13

14

回到顶部
可访问性
可访问性不应该是以后再想的事情。提高网站不需要你成为一个WCAG专家,你完全可以通过修复一些小问题,从而造成一个巨大的变化,例如:

学习正确使用alt 属性
确保链接和按钮被同样地标记(不允许


不专门依靠颜色来传递信息
明确标注表单控件
1

2

Logo

3

4

5

My Company, Inc.

回到顶部
语言
当定义语言和字符编码是可选择的时候,总是建议在文档级别同时声明,即使它们在你的HTTP标头已经详细说明。比任何其他字符编码更偏爱UTF-8。

01

02
<!doctype html>
03
Hello, world.
04

05

06
<!doctype html>
07
08 09 Hello, world. 10
回到顶部
性能
除非有正当理由才能在内容前加载脚本,不要阻塞页面的渲染。如果你的样式表很重,开头就孤立那些绝对需要得样式,并在一个单独的样式表中推迟二次声明的加载。两个HTTP请求显然比一个慢,但是感知速度是最重要的因素。

01

02
<!doctype html>
03

04

05
Hello, world.
06

07

08

09
<!doctype html>
10

11
Hello, world.
12

13

CSS
回到顶部
分号
虽然分号在技术上是CSS一个分隔符,但应该始终把它作为一个终止符。

1
/* bad */
2
div {
3
color: red
4
}
5

6
/* good */
7
div {
8
color: red;
9
}
回到顶部
盒子模型
盒子模型对于整个文档而言最好是相同的。全局性的* { box-sizing: border-box; }就非常不错,但是不要改变默认盒子模型的特定元素,如果可以避免的话。

01
/* bad */
02
div {
03
width: 100%;
04
padding: 10px;
05
box-sizing: border-box;
06
}
07

08
/* good */
09
div {
10
padding: 10px;
11
}
回到顶部

不要更改元素的默认行为,如果可以避免的话。元素尽可能地保持在自然的文档流中。例如,删除图像下方的空格而不改变其默认显示:

1
/* bad */
2
img {
3
display: block;
4
}
5

6
/* good */
7
img {
8
vertical-align: middle;
9
}
同样,如果可以避免的话,不要关闭元素流。

01
/* bad */
02
div {
03
width: 100px;
04
position: absolute;
05
right: 0;
06
}
07

08
/* good */
09
div {
10
width: 100px;
11
margin-left: auto;
12
}
回到顶部
定位
在CSS中有许多定位元素的方法,但应该尽量限制以下属性/值。按优先顺序排列:

display: block;
display: flex;
position: relative;
position: sticky;
position: absolute;
position: fixed;
回到顶部
选择器
最小化紧密耦合到DOM的选择器。当选择器有多于3个结构伪类,后代或兄弟选择器的时候,考虑添加一个类到你想匹配的元素。

1
/* bad */
2
div:first-of-type :last-child > p ~ *
3

4
/* good */
5
div:first-of-type .info
当你不需要的时候避免过载选择器。

1
/* bad */
2
img[src$=svg], ul > li:first-child {
3
opacity: 0;
4
}
5

6
/* good */
7
[src$=svg], ul > :first-child {
8
opacity: 0;
9
}
回到顶部
特异性
不要让值和选择器难以覆盖。尽量少用id,并避免!important。

01
/* bad */
02
.bar {
03
color: green !important;
04
}
05
.foo {
06
color: red;
07
}
08

09
/* good */
10
.foo.bar {
11
color: green;
12
}
13
.foo {
14
color: red;
15
}
回到顶部
覆盖
覆盖样式使得选择器和调试变得困难。如果可能的话,避免覆盖样式。

01
/* bad */
02
li {
03
visibility: hidden;
04
}
05
li:first-child {
06
visibility: visible;
07
}
08

09
/* good */
10
li + li {
11
visibility: hidden;
12
}
回到顶部
继承
不要重复可以继承的样式声明。

1
/* bad */
2
div h1, div p {
3
text-shadow: 0 1px 0 #fff;
4
}
5

6
/* good */
7
div {
8
text-shadow: 0 1px 0 #fff;
9
}
回到顶部
简洁
保持代码的简洁。使用简写属性,没有必要的话,要避免使用多个属性。

01
/* bad */
02
div {
03
transition: all 1s;
04
top: 50%;
05
margin-top: -10px;
06
padding-top: 5px;
07
padding-right: 10px;
08
padding-bottom: 20px;
09
padding-left: 10px;
10
}
11

12
/* good */
13
div {
14
transition: 1s;
15
top: calc(50% – 10px);
16
padding: 5px 10px 20px;
17
}
回到顶部
语言
英语表达优于数学公式。

1
/* bad */
2
:nth-child(2n + 1) {
3
transform: rotate(360deg);
4
}
5

6
/* good */
7
:nth-child(odd) {
8
transform: rotate(1turn);
9
}
回到顶部
浏览器引擎前缀
果断地删除过时的浏览器引擎前缀。如果需要使用的话,可以在标准属性前插入它们。

01
/* bad */
02
div {
03
transform: scale(2);
04
-webkit-transform: scale(2);
05
-moz-transform: scale(2);
06
-ms-transform: scale(2);
07
transition: 1s;
08
-webkit-transition: 1s;
09
-moz-transition: 1s;
10
-ms-transition: 1s;
11
}
12

13
/* good */
14
div {
15
-webkit-transform: scale(2);
16
transform: scale(2);
17
transition: 1s;
18
}
回到顶部
动画
视图转换优于动画。除了opacity 和transform,避免动画其他属性。

01
/* bad */
02
div:hover {
03
animation: move 1s forwards;
04
}
05
@keyframes move {
06
100% {
07
margin-left: 100px;
08
}
09
}
10

11
/* good */
12
div:hover {
13
transition: 1s;
14
transform: translateX(100px);
15
}
回到顶部
单位
可以的话,使用无单位的值。如果使用相对单位,那就用rem 。秒优于毫秒。

01
/* bad */
02
div {
03
margin: 0px;
04
font-size: .9em;
05
line-height: 22px;
06
transition: 500ms;
07
}
08

09
/* good */
10
div {
11
margin: 0;
12
font-size: .9rem;
13
line-height: 1.5;
14
transition: .5s;
15
}
回到顶部
颜色
如果你需要透明度,使用rgba。另外,始终使用十六进制格式。

1
/* bad */
2
div {
3
color: hsl(103, 54%, 43%);
4
}
5

6
/* good */
7
div {
8
color: #5a3;
9
}
回到顶部
绘画
当资源很容易用CSS复制的时候,避免HTTP请求。

01
/* bad */
02
div::before {
03
content: url(white-circle.svg);
04
}
05

06
/* good */
07
div::before {
08
content: “”;
09
display: block;
10
width: 20px;
11
height: 20px;
12
border-radius: 50%;
13
background: #fff;
14
}
回到顶部
Hacks
不要使用Hacks。

01
/* bad */
02
div {
03
// position: relative;
04
transform: translateZ(0);
05
}
06

07
/* good */
08
div {
09
/* position: relative; */
10
will-change: transform;
11
}
JavaScript
回到顶部
性能
可读性,正确性和可表达性优于性能。JavaScript基本上永远不会是你的性能瓶颈。图像压缩,网络接入和DOM重排来代替优化。如果从本文中你只能记住一个指导原则,那么毫无疑问就是这一条。

01
// bad (albeit way faster)
02
const arr = [1, 2, 3, 4];
03
const len = arr.length;
04
var i = -1;
05
var result = [];
06
while (++i < len) { 07 var n = arr[i]; 08 if (n % 2 > 0) continue;
09
result.push(n * n);
10
}
11

12
// good
13
const arr = [1, 2, 3, 4];
14
const isEven = n => n % 2 == 0;
15
const square = n => n * n;
16

17
const result = arr.filter(isEven).map(square);
回到顶部
无状态
尽量保持函数纯洁。理论上,所有函数都不会产生副作用,不会使用外部数据,并且会返回新对象,而不是改变现有的对象。

1
// bad
2
const merge = (target, …sources) => Object.assign(target, …sources);
3
merge({ foo: “foo” }, { bar: “bar” }); // => { foo: “foo”, bar: “bar” }
4

5
// good
6
const merge = (…sources) => Object.assign({}, …sources);
7
merge({ foo: “foo” }, { bar: “bar” }); // => { foo: “foo”, bar: “bar” }
回到顶部
本地化
尽可能地依赖本地方法。

1
// bad
2
const toArray = obj => [].slice.call(obj);
3

4
// good
5
const toArray = (() =>
6
Array.from ? Array.from : obj => [].slice.call(obj)
7
)();
回到顶部
强制性
如果强制有意义,那么就使用隐式强制。否则就应该避免强制。

1
// bad
2
if (x === undefined || x === null) { … }
3

4
// good
5
if (x == undefined) { … }
回到顶部
循环
不要使用循环,因为它们会强迫你使用可变对象。依靠array.prototype 方法。

01
// bad
02
const sum = arr => {
03
var sum = 0;
04
var i = -1;
05
for (;arr[++i];) {
06
sum += arr[i];
07
}
08
return sum;
09
};
10

11
sum([1, 2, 3]); // => 6
12

13
// good
14
const sum = arr =>
15
arr.reduce((x, y) => x + y);
16

17
sum([1, 2, 3]); // => 6
如果不能避免,或使用array.prototype 方法滥用了,那就使用递归。

01
// bad
02
const createDivs = howMany => {
03
while (howMany–) {
04
document.body.insertAdjacentHTML(“beforeend”, ”

“);
05
}
06
};
07
createDivs(5);
08

09
// bad
10
const createDivs = howMany =>
11
[…Array(howMany)].forEach(() =>
12
document.body.insertAdjacentHTML(“beforeend”, ”

“)
13
);
14
createDivs(5);
15

16
// good
17
const createDivs = howMany => {
18
if (!howMany) return;
19
document.body.insertAdjacentHTML(“beforeend”, ”

“);
20
return createDivs(howMany – 1);
21
};
22
createDivs(5);
这里有一个通用的循环功能,可以让递归更容易使用。

回到顶部
参数
忘记arguments 对象。余下的参数往往是一个更好的选择,这是因为:

你可以从它的命名中更好地了解函数需要什么样的参数

真实数组,更易于使用。

1
// bad
2
const sortNumbers = () =>
3
Array.prototype.slice.call(arguments).sort();
4

5
// good
6
const sortNumbers = (…numbers) => numbers.sort();
回到顶部
应用
忘掉apply()。使用操作符。

1
const greet = (first, last) => `Hi ${first} ${last}`;
2
const person = [“John”, “Doe”];
3

4
// bad
5
greet.apply(null, person);
6

7
// good
8
greet(…person);
回到顶部
绑定
当有更惯用的做法时,就不要用bind() 。

1
// bad
2
[“foo”, “bar”].forEach(func.bind(this));
3

4
// good
5
[“foo”, “bar”].forEach(func, this);
01
// bad
02
const person = {
03
first: “John”,
04
last: “Doe”,
05
greet() {
06
const full = function() {
07
return `${this.first} ${this.last}`;
08
}.bind(this);
09
return `Hello ${full()}`;
10
}
11
}
12

13
// good
14
const person = {
15
first: “John”,
16
last: “Doe”,
17
greet() {
18
const full = () => `${this.first} ${this.last}`;
19
return `Hello ${full()}`;
20
}
21
}
回到顶部
函数嵌套
没有必要的话,就不要嵌套函数。

1
// bad
2
[1, 2, 3].map(num => String(num));
3

4
// good
5
[1, 2, 3].map(String);
回到顶部
合成函数
避免调用多重嵌套函数。使用合成函数来替代。

01
const plus1 = a => a + 1;
02
const mult2 = a => a * 2;
03

04
// bad
05
mult2(plus1(5)); // => 12
06

07
// good
08
const pipeline = (…funcs) => val => funcs.reduce((a, b) => b(a), val);
09
const addThenMult = pipeline(plus1, mult2);
10
addThenMult(5); // => 12
回到顶部
缓存
缓存功能测试,大数据结构和任何奢侈的操作。

01
// bad
02
const contains = (arr, value) =>
03
Array.prototype.includes
04
? arr.includes(value)
05
: arr.some(el => el === value);
06
contains([“foo”, “bar”], “baz”); // => false
07

08
// good
09
const contains = (() =>
10
Array.prototype.includes
11
? (arr, value) => arr.includes(value)
12
: (arr, value) => arr.some(el => el === value)
13
)();
14
contains([“foo”, “bar”], “baz”); // => false
回到顶部
变量
const 优于let ,let 优于var。

1
// bad
2
var me = new Map();
3
me.set(“name”, “Ben”).set(“country”, “Belgium”);
4

5
// good
6
const me = new Map();
7
me.set(“name”, “Ben”).set(“country”, “Belgium”);
回到顶部
条件
IIFE 和return 语句优于if, else if,else和switch语句。

01
// bad
02
var grade;
03
if (result < 50)
04
grade = “bad”;
05
else if (result < 90) 06 grade = “good”; 07 else 08 grade = “excellent”; 09 10 // good 11 const grade = (() => {
12
if (result < 50)
13
return “bad”;
14
if (result < 90) 15 return “good”; 16 return “excellent”; 17 })(); 回到顶部 对象迭代 如果可以的话,避免for…in。 01 const shared = { foo: “foo” }; 02 const obj = Object.create(shared, { 03 bar: { 04 value: “bar”, 05 enumerable: true 06 } 07 }); 08 09 // bad 10 for (var prop in obj) { 11 if (obj.hasOwnProperty(prop)) 12 console.log(prop); 13 } 14 15 // good 16 Object.keys(obj).forEach(prop => console.log(prop));
回到顶部
map对象
在对象有合法用例的情况下,map通常是一个更好,更强大的选择。

01
// bad
02
const me = {
03
name: “Ben”,
04
age: 30
05
};
06
var meSize = Object.keys(me).length;
07
meSize; // => 2
08
me.country = “Belgium”;
09
meSize++;
10
meSize; // => 3
11

12
// good
13
const me = new Map();
14
me.set(“name”, “Ben”);
15
me.set(“age”, 30);
16
me.size; // => 2
17
me.set(“country”, “Belgium”);
18
me.size; // => 3
回到顶部
Curry
Curry虽然功能强大,但对于许多开发人员来说是一个外来的范式。不要滥用,因为其视情况而定的用例相当不寻常。

1
// bad
2
const sum = a => b => a + b;
3
sum(5)(3); // => 8
4

5
// good
6
const sum = (a, b) => a + b;
7
sum(5, 3); // => 8
回到顶部
可读性
不要用看似聪明的伎俩混淆代码的意图。

1
// bad
2
foo || doSomething();
3

4
// good
5
if (!foo) doSomething();
1
// bad
2
void function() { /* IIFE */ }();
3

4
// good
5
(function() { /* IIFE */ }());
1
// bad
2
const n = ~~3.14;
3

4
// good
5
const n = Math.floor(3.14);
回到顶部
代码重用
不要害怕创建小型的,高度可组合的,可重复使用的函数。

1
// bad
2
arr[arr.length – 1];
3

4
// good
5
const first = arr => arr[0];
6
const last = arr => first(arr.slice(-1));
7
last(arr);
1
// bad
2
const product = (a, b) => a * b;
3
const triple = n => n * 3;
4

5
// good
6
const product = (a, b) => a * b;
7
const triple = product.bind(null, 3);
回到顶部
依赖性
最小化依赖性。第三方是你不知道的代码。不要只是因为几个可轻易复制的方法而加载整个库:

view sourceprint?
01
// bad
02
var _ = require(“underscore”);
03
_.compact([“foo”, 0]));
04
_.unique([“foo”, “foo”]);
05
_.union([“foo”], [“bar”], [“foo”]);
06

07
// good
08
const compact = arr => arr.filter(el => el);
09
const unique = arr => […Set(arr)];
10
const union = (…arr) => unique([].concat(…arr));
11

12
compact([“foo”, 0]);
13
unique([“foo”, “foo”]);
14
union([“foo”], [“bar”], [“foo”]);
译文链接:https://www.codeceo.com/article/full-frontend-guidelines.html
英文原文:Frontend Guidelines

转载请注明:TUTERM.COM » 最全面的前端开发指南

如果您觉得本文的内容对您的学习有所帮助,您可以支付宝(左)或微信(右):
alipay weichat

您必须 登录 才能发表评论!