<!doctype html> <htmllang="en"> <head> <metacharset="utf-8"> <title>Good Example</title> <linkrel="stylesheet"href="style.css"> <scriptsrc="/jquery.js"></script> </head> <body> <imgclass="sample-image"src="assets/img/img.png"alt="Sample Image"> <p>This is a sample image</p> </body> </html>
HTML语义
永远恰当地使用HTML元素。
减少封装
永远需要避免多余的父元素结点。
1 2 3 4 5 6 7
<!-- Bad HTML --> <spanclass="avatar"> <imgsrc="assets/img/img.png"alt="Jane Doe"> </span> <!-- Good HTML --> <imgclass="avatar"src="assets/img/img.png"alt="Jane Doe">
<!-- Bad Example --> <!doctype html> <htmllang="en"> <head> <metacharset="utf-8"> <title>Sample Page</title> <scriptsrc="script.js"></script> </head> <body> <h1style="font-size:1em;line-height:2em;">This is a heading.</h1> <pstyle="text-decoration:underline;font-size:.5em;line-height:1em;">This is an underlined paragraph.</p> </body> </html> <!-- Good Example --> <!doctype html> <htmllang="en"> <head> <metacharset="utf-8"> <title>Sample Page</title> <linkrel="stylesheet"href="style.css"> <scriptsrc="script.js"></script> </head> <body> <h1>This is a heading.</h1> <p>This is an underlined paragraph.</p> </body> </html>
<!-- Bad Example --> <label><inputtype="radio"name="input"value="first"> First</label> <!-- Good Example --> <inputtype="radio"name="input"id="input-1"value="first"> <labelfor="input-1">First</label>
对于标记不要使用placeholder属性。切记使用label元素。
1 2 3 4 5 6
<!-- Bad Example --> <inputtype="text"id="name"placeholder="Enter your name"> <!-- Good Example --> <labelfor="name">Name</label> <inputid="name"type="text"placeholder="Jane Doe">