Web開發——HTML基礎(HTML塊 <div>/<span> )
可以通過 <div> 和 <span> 將 HTML 元素組合起來。
1、HTML 塊元素
大多數 HTML 元素被定義為塊級元素或內聯元素。
編者註:“塊級元素”譯為 block level element,“內聯元素”譯為 inline element。
塊級元素在瀏覽器顯示時,通常會以新行來開始(和結束)。
例子:<h1>, <p>, <ul>, <table>
2、HTML 內聯元素
內聯元素在顯示時通常不會以新行開始。
例子:<b>, <td>, <a>, <img>
3、HTML <div> 元素
HTML <div> 元素是塊級元素,它是可用於組合其他 HTML 元素的容器。
<div> 元素沒有特定的含義。除此之外,由於它屬於塊級元素,瀏覽器會在其前後顯示折行。
如果與 CSS 一同使用,<div> 元素可用於對大的內容塊設置樣式屬性。
<div> 元素的另一個常見的用途是文檔布局。它取代了使用表格定義布局的老式方法。使用 <table> 元素進行文檔布局不是表格的正確用法。<table> 元素的作用是顯示表格化的數據。
4、HTML <span> 元素
HTML <span> 元素是內聯元素,可用作文本的容器。
<span> 元素也沒有特定的含義。
當與 CSS 一同使用時,<span> 元素可用於為部分文本設置樣式屬性。
5、HTML 分組標簽
標簽 | 描述 |
---|---|
<div> | 定義文檔中的分區或節(division/section)。 |
<span> | 定義 span,用來組合文檔中的行內元素。 |
6、HTML 類
對 HTML 進行分類(設置類),使我們能夠為元素的類定義 CSS 樣式。
為相同的類設置相同的樣式,或者為不同的類設置不同的樣式。
舉例:
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>My test page</title> 6 7 <style>.cities { 8 background-color:black; 9 color:white; 10 margin:20px; 11 padding:20px; 12 } 13 </style> 14 </head> 15 16 <body> 17 <!--<div>容器,設置class屬性是為了在<head>中配置css樣式--> 18 <div class="cities"> 19 <h2>London</h2> 20 <p> 21 London is the capital city of England. 22 It is the most populous city in the United Kingdom, 23 with a metropolitan area of over 13 million inhabitants. 24 </p> 25 </div> 26 27 </body> 28 </html>
輸出結果:
6.1 分類塊級元素
HTML <div> 元素是塊級元素。它能夠用作其他 HTML 元素的容器。
設置 <div> 元素的類,使我們能夠為相同的 <div> 元素設置相同的類:
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>My test page</title> 6 7 <style>.cities { 8 background-color:black; 9 color:white; 10 margin:20px; 11 padding:20px; 12 } 13 </style> 14 </head> 15 16 <body> 17 <!--<div>容器,設置class屬性是為了在<head>中配置css樣式--> 18 <div class="cities"> 19 <h2>London</h2> 20 <p> 21 London is the capital city of England. 22 It is the most populous city in the United Kingdom, 23 with a metropolitan area of over 13 million inhabitants. 24 </p> 25 </div> 26 27 <div class="cities"> 28 <h2>Paris</h2> 29 <p>Paris is the capital and most populous city of France.</p> 30 </div> 31 32 <div class="cities"> 33 <h2>Tokyo</h2> 34 <p>Tokyo is the capital of Japan, the center of the Greater Tokyo Area, 35 and the most populous metropolitan area in the world.</p> 36 </div> 37 38 </body> 39 </html>
輸出結果:
6.2 分類行內元素
HTML <span> 元素是行內元素,能夠用作文本的容器。
設置 <span> 元素的類,能夠為相同的 <span> 元素設置相同的樣式。
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>My test page</title> 6 7 <style> 8 span.red{ 9 color: red 10 } 11 </style> 12 </head> 13 14 <body> 15 16 <h1>My <span class="red">Important</span> Heading</h1> 17 18 </body> 19 </html>
輸出結果:
Web開發——HTML基礎(HTML塊 <div>/<span> )