初级程序员在编写Html和CSS时在高级语言上总是犯错误,原因可能是粗心大意或缺乏足够的经验。开发者长期书写干净、整洁的代码更有助于提高其未来的开发技能,同时代码的可读性能保证以后修改编辑原程序更加方便。下面是一些初级开发者在进行网页设计时常见的错误:
1、忘记关闭标签
这种现象非常普遍,尤其对于初学者。一些标签必须关闭,例如div、strong和links等等。其他标签需要以斜线结束,如IMG标签。
2、不正确的DOCTYPE(文档类型)
DOCTYPE是用来说明你用的XHTML或者HTML是什么版本,要建立符合标准的网页,你必须在网页代码开始处书写正确的DOCTYPE声明。 它需要放在其它任何代码之前,在网页文档开始处就告诉程序你的HTML是什么类型。
正确示例:
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
3、标签不合理嵌套
用正确的顺序打开和关闭标签是非常重要的。一旦一个标签(例如div)被打开,它就必须在与之配对的位置上关闭。
4、标签大写
这是一个错误的编写方法,尽管它不会导致你的程序运行失败。你应该总是使用小写的div、links和img等标签。如:div
5、忘记打开或关闭引号
很多初级网页开发者编程时总是出现不完整的代码,致使编译时总是异常中断。Html使用双引号时需要正确地打开和关闭,不能出现只打开不关闭或只关闭不打开等情况。
6、使用内联样式
这是另一种非常不好的做法。内联样式尽管不会造成错误,但是在对整个网页做修改的时候,原来仅仅只需要修改外部风格就可以达到目标,而现在却要一步一步修改内部样式才能达到整体修改的目的。
7、不编码特殊字符
如字符“?”和“&”应该进行正确的Html编码。 下面是一些需要编码的字符列表,以及这些字符正确的Html使用规则。
"
"
Double quote
&
&
Ampersand ("and" sign)
<
<
Less-than
>
>
Greater-than
un-linebreak-able space
¡
?
Upside-down !
¢
¢
Cent sign (c crossed out)
£
£
Pound sign: the currency symbol
not the tic-tac-toe telephone symbol, which is incorrectly called "pound".
¤
¤
circle with dashes at NE, SE, SW, and NW
¥
¥
Y crossed out
¦
|
Vertical line, maybe with gap in middle
§
§
Section sign (like hurricane symbol on weather maps)
¨
¨
Two dots up in the air
©
?
Copyright sign (C in a circle)
ª
a
lower case "a" up in the air
«
?
Two small less-than signs: the German open-quote
¬
?
Not sign from classical logic
"Soft" hyphen: a dash
®
?
Registered sign (R in a circle)
¯
ˉ
Macron (horizontal line up in the air)
°
°
Degree sign
±
±
Plus-or-minus sign
²
2
2 up in the air
³
3
3 up in the air
´
′
Little dash pointing to North-East
µ
μ
Micro sign, lower case Greek Mu
¶
?
Paragraph sign (mirror image capital P with two legs and a black eye)
·
·
Decimal Point (English style, mid-level)
¸
?
Small sickle shape, low down
¹
1
1 up in the air
º
o
lower case "o" up in the air
»
?
Two small greater-than signs: the German close-quote
¼
?
One quarter
½
?
One half
¾
?
Three quarters
¿
?
Upside-down ?
×
×
Times sign: narrow x without serifs
÷
÷
Division sign: a colon : with a dash through it
Ð
D
Old English voiced "Th", "D" with dash through upright
ð
e
Old Englished voiced "th": bendy "d" with dash through tail
Þ
T
Old English unvoiced "Th": "P" but loop has slipped down
þ
t
Old English unvoiced "th": smaller version of the above
Æ
?
"A" and "E" in a ligature, as in ENCYCLOPAEDIA
æ
?
"a" and "e" in a ligature, as in "encyclopaedia"
Œ
?
"O" and "E" in a ligature, as in MANOEUVRE
œ
?
"o" and "e" in a ligature, as in "manoeuvre"
Å
?
"A" with a little circle above: Angstrom sign
Ø
?
"O" with diagonal line through: Empty set sign
Ç
?
"C" with cedilla (sickle shape) underneath
ç
?
"c" with cedilla (sickle shape) underneath
ß
?
German "sz" ligature, like a lower case Greek Beta
Ñ
?
N with wiggle on top
ñ
?
n with wiggle on top
8、混淆类和ID
类可以用在一个页面中的多个项目上。它以链接形式存在,以便你在一个页面中调用多次,但是它不能指定整体网页样式。ID只能调用一次,例如网页头部div。 类和ID往往过度使用或用在不必要的地方。
9、不使用全局样式
例如图片、头部样式和链接样式等许多标签应该使用全局样式,这可以大大减少未知的错误,也将大大缩减网页样式表的代码大小。
10、ID和类的名称不唯一
这是非常关键的。唯一的名称能保证以后对网页修改更加容易,因为在你的样式表中能立即识别需要修改的元素。你的div的名称可以用 #home-left-column 而不是 #left.。
11、不使用速记法
速记代码可以有效的压缩你的样式表大小,有利于加快网页的加载速度。与其每一次都添加上,右,下和左的样式,不如一次性全部添加:padding: 5px 10px 0 10px;速记代码可用于包括许多声明:填充,边缘,边界和字体等。
12、不使用缩短颜色声明
十六进制数字,像 #ffffff 和 #000000 重复可以缩写为 #fff 与 #000。 这是另一种压缩代码保证代码可阅读性的好方法。
13、验证
验证你的 HTML 和 CSS 文件将有助于减少错误,找出其中的问题。一些常见的HTML和CSS错误会被忽略,所以您的网站仍然能够正常运作,但它并不是最有效的代码,因此它会导致你的网站运行缓慢。校验器将有助于你确定这些问题。