HTML基本标签
任何文件都以标题开始,你可以为标题使用不同的大小,HTML也有六个级别的标题,分别是标签<h1>、<h2>、<h3>、<h4>、<h5>和<h6>。在显示任何标题时,浏览器在该标题前后各加一行。
<!DOCTYPE html> <html> <head> <title>Heading Example</title> </head> <body> <h1>This is heading 1</h1> <h2>This is heading 2</h2> <h3>This is heading 3</h3> <h4>This is heading 4</h4> <h5>This is heading 5</h5> <h6>This is heading 6</h6> </body> </html>
这将产生以下结果:
This is heading 1 This is heading 2 This is heading 3 This is heading 4 This is heading 5 This is heading 6
段落标签
<p>标签提供了一种将你的文本结构成不同段落的方法。每一段文字都应该放在开头的<p>和结尾的</p>标签之间,如下例所示:
<!DOCTYPE html> <html> <head> <title>Paragraph Example</title> </head> <body> <p>Here is a first paragraph of text.</p> <p>Here is a second paragraph of text.</p> <p>Here is a third paragraph of text.</p> </body> </html>
这将产生以下结果:
Here is a first paragraph of text. Here is a second paragraph of text. Here is a third paragraph of text.
换行标签
每当你使用<br />元素时,它后面的东西都从下一行开始。这个标签是一个空元素的例子,你不需要打开和关闭标签,因为它们之间没有任何东西可供使用。
<br />标签在字符br和正斜杠之间有一个空格。如果你省略了这个空格,旧的浏览器将难以呈现断行,而如果你错过了正斜线字符而只使用<br>,在XHTML中是无效的。
<!DOCTYPE html> <html> <head> <title>Line Break Example</title> </head> <body> <p>Hello<br /> You delivered your assignment ontime.<br /> Thanks<br /> Mahnaz</p> </body> </html>
这将产生以下结果:
Hello You delivered your assignment on time. Thanks Mahnaz
居中内容
可以使用<center>标签把任何内容放在页面的中心或任何表格单元。
<!DOCTYPE html> <html> <head> <title>Centring Content Example</title> </head> <body> <p>This text is not in the center.</p> <center> <p>This text is in the center.</p> </center> </body> </html>
这将产生以下结果:
This text is not in the center. This text is in the center.
水平线
水平线是用来在视觉上分割文档的各个部分的,<hr>标签从文档中的当前位置到右边缘创建一条线,并相应地断开该线。
例如,你可能想在两个段落之间划一条线,就像下面的例子一样:
<!DOCTYPE html> <html> <head> <title>Horizontal Line Example</title> </head> <body> <p>This is paragraph one and should be on top</p> <hr /> <p>This is paragraph two and should be at bottom</p> </body> </html>
这将产生以下结果:
This is paragraph one and should be on top ---------------------------------------------------- This is paragraph two and should be at bottom
同样,<hr />标签是一个空元素的例子,在这里你不需要打开和关闭标签,因为它们之间没有任何东西可供选择。
<hr />元素的字符hr和正斜杠之间有一个空格。如果你省略了这个空格,旧的浏览器将难以呈现水平线,而如果你错过了正斜杠字符而只使用<hr>,在XHTML中是无效的。
保留格式
有时,你希望你的文本完全按照HTML文档中的格式来写。在这些情况下,你可以使用预格式化标签<pre>。
在开头的<pre>标签和结尾的</pre>标签之间的任何文本将保留源文档的格式。
<!DOCTYPE html> <html> <head> <title>Preserve Formatting Example</title> </head> <body> <pre> function testFunction( strText ){ alert (strText) } </pre> </body> </html>
这将产生以下结果:
function testFunction( strText ){ alert (strText) }
试着使用相同的代码,而不把它放在<pre>...</pre>标签内。
不间断空格
假设想使用短语“ 12 Angry Men.”,在这里,你不希望浏览器将“12 Angry”和“Men”分为两行:
An example of this technique appears in the movie "12 Angry Men."
在某些情况下,如果你不希望客户端浏览器中断文本,则应使用不间断的空格实体&nbsp;而不是正常的空间。例如,在一个段落中编码“ 12个愤怒的人”时,你应该使用类似于以下代码的东西:
<!DOCTYPE html> <html> <head> <title>Nonbreaking Spaces Example</title> </head> <body> <p>An example of this technique appears in the movie "12 Angry Men."</p> </body> </html>
这将产生以下结果:
An example of this technique appears in the movie "12 Angry Men."