可以使用 HTML 創建表格。
實例
表格
[demo]
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<p>每個表格由 table 標簽開始。</p>
<p>每個表格行由 tr 標簽開始。</p>
<p>每個表格數據由 td 標簽開始。</p>
<h4>一列:</h4>
<table border="1">
<tr>
<td>100</td>
</tr>
</table>
<h4>一行三列:</h4>
<table border="1">
<tr>
<td>100</td>
<td>200</td>
<td>300</td>
</tr>
</table>
<h4>兩行三列:</h4>
<table border="1">
<tr>
<td>100</td>
<td>200</td>
<td>300</td>
</tr>
<tr>
<td>400</td>
<td>500</td>
<td>600</td>
</tr>
</table>
</body>
</html>
[/demo]
這個例子演示如何在 HTML 文檔中創建表格。
表格邊框
[demo]
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<h4>帶有普通的邊框:</h4>
<table border="1">
<tr>
<td>First</td>
<td>Row</td>
</tr>
<tr>
<td>Second</td>
<td>Row</td>
</tr>
</table>
<h4>帶有粗的邊框:</h4>
<table border="8">
<tr>
<td>First</td>
<td>Row</td>
</tr>
<tr>
<td>Second</td>
<td>Row</td>
</tr>
</table>
<h4>帶有很粗的邊框:</h4>
<table border="15">
<tr>
<td>First</td>
<td>Row</td>
</tr>
<tr>
<td>Second</td>
<td>Row</td>
</tr>
</table>
</body>
</html>
[/demo]
本例演示各種類型的表格邊框。
表格
表格由 <table> 標簽來定義。每個表格均有若干行(由 <tr> 標簽定義),每行被分割為若干單元格(由 <td> 標簽定義)。字母 td 指表格數據(table data),即數據單元格的內容。數據單元格可以包含文本、圖片、列表、段落、表單、水平線、表格等等。
<table border="1">
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
<tr>
<td>row 2, cell 1</td>
<td>row 2, cell 2</td>
</tr>
</table>
在瀏覽器顯示如下:
row 1, cell 1 row 1, cell 2
row 2, cell 1 row 2, cell 2
表格和邊框屬性
如果不定義邊框屬性,表格將不顯示邊框。有時這很有用,但是大多數時候,我們希望顯示邊框。
使用邊框屬性來顯示一個帶有邊框的表格:
<table border="1">
<tr>
<td>Row 1, cell 1</td>
<td>Row 1, cell 2</td>
</tr>
</table>
表格的表頭
表格的表頭使用 <th> 標簽進行定義。
大多數瀏覽器會把表頭顯示為粗體居中的文本:
<table border="1">
<tr>
<th>Heading</th>
<th>Another Heading</th>
</tr>
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
<tr>
<td>row 2, cell 1</td>
<td>row 2, cell 2</td>
</tr>
</table>
在瀏覽器顯示如下:
Heading Another Heading
row 1, cell 1 row 1, cell 2
row 2, cell 1 row 2, cell 2
表格中的空單元格
在一些瀏覽器中,沒有內容的表格單元顯示得不太好。如果某個單元格是空的(沒有內容),瀏覽器可能無法顯示出這個單元格的邊框。
<table border="1">
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
<tr>
<td></td>
<td>row 2, cell 2</td>
</tr>
</table>
表格中的空單元格
注意:這個空的單元格的邊框沒有被顯示出來。為了避免這種情況,在空單元格中添加一個空格占位符,就可以將邊框顯示出來。
<table border="1">
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
<tr>
<td> </td>
<td>row 2, cell 2</td>
</tr>
</table>
在瀏覽器中顯示如下:
row 1, cell 1 row 1, cell 2
row 2, cell 2
更多實例
沒有邊框的表格
[demo]
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<h4>這個表格沒有邊框:</h4>
<table>
<tr>
<td>100</td>
<td>200</td>
<td>300</td>
</tr>
<tr>
<td>400</td>
<td>500</td>
<td>600</td>
</tr>
</table>
<h4>這個表格也沒有邊框:</h4>
<table border="0">
<tr>
<td>100</td>
<td>200</td>
<td>300</td>
</tr>
<tr>
<td>400</td>
<td>500</td>
<td>600</td>
</tr>
</table>
</body>
</html>
[/demo]
本例演示一個沒有邊框的表格。
表格中的表頭(Heading)
[demo]
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<h4>表頭:</h4>
<table border="1">
<tr>
<th>姓名</th>
<th>電話</th>
<th>電話</th>
</tr>
<tr>
<td>Bill Gates</td>
<td>555 77 854</td>
<td>555 77 855</td>
</tr>
</table>
<h4>垂直的表頭:</h4>
<table border="1">
<tr>
<th>姓名</th>
<td>Bill Gates</td>
</tr>
<tr>
<th>電話</th>
<td>555 77 854</td>
</tr>
<tr>
<th>電話</th>
<td>555 77 855</td>
</tr>
</table>
</body>
</html>
[/demo]
本例演示如何顯示表格表頭。
空單元格
[demo]
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<table border="1">
<tr>
<td>Some text</td>
<td>Some text</td>
</tr>
<tr>
<td></td>
<td>Some text</td>
</tr>
</table>
<p>正如您看到的,其中一個單元沒有邊框。這是因為它是空的。在該單元中插入一個空格后,仍然沒有邊框。</p>
<p>我們的技巧是在單元中插入一個 no-breaking 空格。</p>
<p>no-breaking 空格是一個字符實體。如果您不清楚什么是字符實體,請閱讀關于字符實體的章節。</p>
<p>no-breaking 空格由和號開始 ("&"),然后是字符"nbsp",并以分號結尾(";")。</p>
</body>
</html>
[/demo]
本例展示如何使用 " " 處理沒有內容的單元格。
帶有標題的表格
[demo]
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<h4>這個表格有一個標題,以及粗邊框:</h4>
<table border="6">
<caption>我的標題</caption>
<tr>
<td>100</td>
<td>200</td>
<td>300</td>
</tr>
<tr>
<td>400</td>
<td>500</td>
<td>600</td>
</tr>
</table>
</body>
</html>
[/demo]
本例演示一個帶標題 (caption) 的表格
跨行或跨列的表格單元格
[demo]
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<h4>橫跨兩列的單元格:</h4>
<table border="1">
<tr>
<th>姓名</th>
<th colspan="2">電話</th>
</tr>
<tr>
<td>Bill Gates</td>
<td>555 77 854</td>
<td>555 77 855</td>
</tr>
</table>
<h4>橫跨兩行的單元格:</h4>
<table border="1">
<tr>
<th>姓名</th>
<td>Bill Gates</td>
</tr>
<tr>
<th rowspan="2">電話</th>
<td>555 77 854</td>
</tr>
<tr>
<td>555 77 855</td>
</tr>
</table>
</body>
</html>
[/demo]
本例演示如何定義跨行或跨列的表格單元格。
表格內的標簽
[demo]
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<table border="1">
<tr>
<td>
<p>這是一個段落。</p>
<p>這是另一個段落。</p>
</td>
<td>這個單元包含一個表格:
<table border="1">
<tr>
<td>A</td>
<td>B</td>
</tr>
<tr>
<td>C</td>
<td>D</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>這個單元包含一個列表:
<ul>
<li>蘋果</li>
<li>香蕉</li>
<li>菠蘿</li>
</ul>
</td>
<td>HELLO</td>
</tr>
</table>
</body>
</html>
[/demo]
本例演示如何顯示在不同的元素內顯示元素。
單元格邊距(Cell padding)
[demo]
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<h4>沒有 cellpadding:</h4>
<table border="1">
<tr>
<td>First</td>
<td>Row</td>
</tr>
<tr>
<td>Second</td>
<td>Row</td>
</tr>
</table>
<h4>帶有 cellpadding:</h4>
<table border="1"
cellpadding="10">
<tr>
<td>First</td>
<td>Row</td>
</tr>
<tr>
<td>Second</td>
<td>Row</td>
</tr>
</table>
</body>
</html>
[/demo]
本例演示如何使用 Cell padding 來創建單元格內容與其邊框之間的空白。
單元格間距(Cell spacing)
[demo]
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<h4>沒有 cellspacing:</h4>
<table border="1">
<tr>
<td>First</td>
<td>Row</td>
</tr>
<tr>
<td>Second</td>
<td>Row</td>
</tr>
</table>
<h4>帶有 cellspacing:</h4>
<table border="1"
cellspacing="10">
<tr>
<td>First</td>
<td>Row</td>
</tr>
<tr>
<td>Second</td>
<td>Row</td>
</tr>
</table>
</body>
</html>
[/demo]
本例演示如何使用 Cell spacing 增加單元格之間的距離。
向表格添加背景顏色或背景圖像
[demo]
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<h4>背景顏色:</h4>
<table border="1"
bgcolor="red">
<tr>
<td>First</td>
<td>Row</td>
</tr>
<tr>
<td>Second</td>
<td>Row</td>
</tr>
</table>
<h4>背景圖像:</h4>
<table border="1"
background="./imagecopy1234567890/test.gif">
<tr>
<td>First</td>
<td>Row</td>
</tr>
<tr>
<td>Second</td>
<td>Row</td>
</tr>
</table>
</body>
</html>
[/demo]
本例演示如何向表格添加背景。
向表格單元添加背景顏色或者背景圖像
[demo]
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<h4>單元背景:</h4>
<table border="1">
<tr>
<td bgcolor="red">First</td>
<td>Row</td>
</tr>
<tr>
<td
background="./imagecopy1234567890/test.gif">
Second</td>
<td>Row</td>
</tr>
</table>
</body>
</html>
[/demo]
本例演示如何向一個或者更多表格單元添加背景。
在表格單元中排列內容
[demo]
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<table width="400" border="1">
<tr>
<th align="left">消費項目....</th>
<th align="right">一月</th>
<th align="right">二月</th>
</tr>
<tr>
<td align="left">衣服</td>
<td align="right">1.10</td>
<td align="right">.20</td>
</tr>
<tr>
<td align="left">化妝品</td>
<td align="right">.00</td>
<td align="right">.45</td>
</tr>
<tr>
<td align="left">食物</td>
<td align="right">0.40</td>
<td align="right">0.00</td>
</tr>
<tr>
<th align="left">總計</th>
<th align="right">01.50</th>
<th align="right">4.65</th>
</tr>
</table>
</body>
</html>
[/demo]
本例演示如何使用 "align" 屬性排列單元格內容,以便創建一個美觀的表格。
框架(frame)屬性
[demo]
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<p><b>注釋:</b>frame 屬性無法在 Internet Explorer 中正確地顯示。</p>
<p>Table with frame="box":</p>
<table frame="box">
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td>January</td>
<td>0</td>
</tr>
</table>
<p>Table with frame="above":</p>
<table frame="above">
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td>January</td>
<td>0</td>
</tr>
</table>
<p>Table with frame="below":</p>
<table frame="below">
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td>January</td>
<td>0</td>
</tr>
</table>
<p>Table with frame="hsides":</p>
<table frame="hsides">
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td>January</td>
<td>0</td>
</tr>
</table>
<p>Table with frame="vsides":</p>
<table frame="vsides">
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td>January</td>
<td>0</td>
</tr>
</table>
</body>
</html>
[/demo]
本例演示如何使用 "frame" 屬性來控制圍繞表格的邊框。
表格標簽
表格 描述
<table> 定義表格
<caption> 定義表格標題。
<th> 定義表格的表頭。
<tr> 定義表格的行。
<td> 定義表格單元。
<thead> 定義表格的頁眉。
<tbody> 定義表格的主體。
<tfoot> 定義表格的頁腳。
<col> 定義用于表格列的屬性。
<colgroup> 定義表格列的組。
全棧攻城獅-每日更新原創IT編程技術及日常實用視頻。
主要內容:這是HTML課程的第六課,在這節課程中主要講解一下HTML中body的屬性,以及表格元素。希望大家根據這個教程可以學習一下。
上節回顧
在上節中主要講解了超鏈接和圖片的知識點。上節請戳->網頁前端開發基礎教程05-網頁中插入圖片和超鏈接,界面更絢麗
當然講解的并不是很全面,只是把常用的HTML屬性進行講解了出來,在后期會進行做項目,到時候,直接回顧一下,馬上就能搞懂了。這次直接講解一下HTML中的body屬性以及表格元素。
PS:其實HTML這個東西很簡單,只需要根據對應的元素,學會標簽指的是什么就可以了,就和諜戰片中中的密碼本一樣,按照密碼本書寫對應的格式,寫上去就能直接顯示了。只是學會這種標簽的樣式而已。自己學習HTML也是可以的。最重要的還是進行實踐。
body的屬性
body是整個頁面的主體元素,我們把內容寫入到body元素中,那應該如何設置主體的背景顏色、背景圖片呢?
下面就一一來講解一下。使用Sublime創建一個HTML文件。
設置背景顏色,屬性為bgcolor,值為RGB顏色或者顏色的英文單詞。
2.背景圖片設置,背景圖片照樣可以用路徑的方式寫入到HTML文件中。屬性為background例如:
當然body的屬性值,比較少,而且幾乎不太用,在以后的時候要使用CSS代替這些東西。
表格
表格在網頁中還是很常用的,比如在按照每行數據進行展示的時候,就需要用到表格啦。所以就需要學習一下表格應該如何寫。
OK,我們先建立一個最簡單的學生信息表:
其中table(表格)、tr(一行)、(一個單元格)。這三個時表格最基本的元素。并依次有個上下級嵌套的關系。
表格中的屬性
表格中有很多屬性,正是因為有了這些屬性,才讓表格無比強大。比如在老早的時候,風靡一時的Hao123就是使用表格進行制作的。OK在下節中咱們也會自己制作一個模仿的Hao123第一版,通過table制作。
border
border是邊框的意思,在以后的學習中你肯定會對這個單詞不陌生,因為這個單詞真的是太常用了。border有兩個值分別為0和1.0表示“沒有邊框”,1表示“有邊框”。
cellpadding:
這個表示的是表格的內邊距,也就是表格的邊框到里面的內容的距離。
數值越大,單元格的表框到內容的距離越大。
cellspacing
這是單元格和單元格之間的距離,這個值越大,單元格之間的距離越大。
align:
align表示的是單元格的對齊方式,對里面的內容進行水平的對齊,其值有三個分別為right(右)、center(居中)、left(左)。
colspan、rowspan:
這是兩個屬性,分別表示行的合并和列的合并。其值為數字,表示要合并的單元格:
還有其他幾個屬性例如:valign、bgcolor、width、height等在前課程中也講過了。
每天一個知識點,帶你邁向軟件編程大神,一起努力吧。下節課程,我們一起做一個Hao123的導航頁面。
實,在2021年的時候,我就分享過鴻蒙App開發的文章,只是當時是基于Java的,如今,時過境遷,鴻蒙接下來將不再支持用Java開發的apk安裝包,而是改為由Typescript語言為主導的開發模式,而Typescript是基于Javascript擴展而來,所以你完全有必要先學習JS。另外在開發鴻蒙App時,還會用到Html和CSS,故老陳推薦你先學習網頁三賤客。
干前端開發的都知道:網頁三劍客分別是Html、Css和Javascript。學好它們之后,再學習鴻蒙App(或微信小程序)開發,將事半功倍,加快學習效率,提升學習效果。以下是老陳之前就發布過的關于網頁三劍客的文章,整理如下:
第0天 | 16天搞定前端,什么是前端?
第1天 | 16天搞定前端,html頁面結構和元素(必看)
第2天|16天搞定前端,html頭部標簽,只是標簽?
第3天 | 16天搞定前端,html基礎標簽,一點都不基礎?
第4天 | 16天搞定前端,html文本格式,奇葩說?
第5天 | 16天搞定前端,html布局,表格和大塊頭
第6天 | 16天搞定前端,html表單,標簽界的杠把子?
第7天 | 16天搞定前端,html多媒體,圖聲視標簽
第8天 | 16天搞定前端,HTML的好基友,CSS來了
第9天 | 16天搞定前端,CSS的語法和選擇器(必看)
第10天 | 16天搞定前端,向CSS的文字之美,看齊
第11天|16天搞定前端,CSS的圓角邊框,讓人賞心悅目
第12天 | 16天搞定前端,CSS的邊距,內外有別?
第13天 | 16天搞定前端,CSS的動畫效果,酷
第14天|16天搞定前端,Javascript,要你何用?
第15天|16天搞定前端,javascript語法篇(干貨)
第16天|16天搞定前端,因javascript不足,故改優
#HarmonyOS##鴻蒙##華為#
*請認真填寫需求信息,我們會在24小時內與您取得聯系。