Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
TML 表格實(shí)例:
First Name | Last Name | Points |
---|---|---|
Jill | Smith | 50 |
Eve | Jackson | 94 |
John | Doe | 80 |
Adam | Johnson | 67 |
在線實(shí)例
表格
這個(gè)例子演示如何在 HTML 文檔中創(chuàng)建表格。
HTML 表格
表格由 <table> 標(biāo)簽來定義。每個(gè)表格均有若干行(由 <tr> 標(biāo)簽定義),每行被分割為若干單元格(由 <td> 標(biāo)簽定義)。字母 td 指表格數(shù)據(jù)(table data),即數(shù)據(jù)單元格的內(nèi)容。數(shù)據(jù)單元格可以包含文本、圖片、列表、段落、表單、水平線、表格等等。
表格實(shí)例
<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 |
HTML 表格和邊框?qū)傩?/p>
如果不定義邊框?qū)傩裕砀駥⒉伙@示邊框。有時(shí)這很有用,但是大多數(shù)時(shí)候,我們希望顯示邊框。
使用邊框?qū)傩詠盹@示一個(gè)帶有邊框的表格:
<table border="1">
<tr>
<td>Row 1, cell 1</td>
<td>Row 1, cell 2</td>
</tr>
</table>
HTML 表格表頭
表格的表頭使用 <th> 標(biāo)簽進(jìn)行定義。
大多數(shù)瀏覽器會(huì)把表頭顯示為粗體居中的文本:
<table border="1">
<tr>
<th>Header 1</th>
<th>Header 2</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>
在瀏覽器顯示如下:
Header 1 | Header 2 |
---|---|
row 1, cell 1 | row 1, cell 2 |
row 2, cell 1 | row 2, cell 2 |
更多實(shí)例
沒有邊框的表格
本例演示一個(gè)沒有邊框的表格。
表格中的表頭(Heading)
本例演示如何顯示表格表頭。
帶有標(biāo)題的表格
本例演示一個(gè)帶標(biāo)題 (caption) 的表格
跨行或跨列的表格單元格
本例演示如何定義跨行或跨列的表格單元格。
表格內(nèi)的標(biāo)簽
本例演示如何顯示在不同的元素內(nèi)顯示元素。
單元格邊距(Cell padding)
本例演示如何使用 Cell padding 來創(chuàng)建單元格內(nèi)容與其邊框之間的空白。
單元格間距(Cell spacing)
本例演示如何使用 Cell spacing 增加單元格之間的距離。
HTML 表格標(biāo)簽
標(biāo)簽 | 描述 |
---|---|
<table> | 定義表格 |
<th> | 定義表格的表頭 |
<tr> | 定義表格的行 |
<td> | 定義表格單元 |
<caption> | 定義表格標(biāo)題 |
<colgroup> | 定義表格列的組 |
<col> | 定義用于表格列的屬性 |
<thead> | 定義表格的頁眉 |
<tbody> | 定義表格的主體 |
<tfoot> | 定義表格的頁腳 |
如您還有不明白的可以在下面與我留言或是與我探討QQ群308855039,我們一起飛!
接到很web前端項(xiàng)目中,常常看到表格table,做表格的樣式,在本文下面,列舉了四種表格css樣式,代碼也在下面:
1.單像素邊框CSS表格
這是一個(gè)很常用的表格樣式。
<!-- CSS goes in the document HEAD or added to your external stylesheet -->
<style type="text/css">
table.gridtable {
font-family: verdana,arial,sans-serif;
font-size:11px;
color:#333333;
border-width: 1px;
border-color: #666666;
border-collapse: collapse;
}
table.gridtable th {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #666666;
background-color: #dedede;
}
table.gridtable td {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #666666;
background-color: #ffffff;
}
</style>
<!-- Table goes in the document BODY -->
<table class="gridtable">
<tr>
<th>Info Header 1</th><th>Info Header 2</th><th>Info Header 3</th>
</tr>
<tr>
<td>Text 1A</td><td>Text 1B</td><td>Text 1C</td>
</tr>
<tr>
<td>Text 2A</td><td>Text 2B</td><td>Text 2C</td>
</tr>
</table>
2. 帶背景圖的CSS樣式表格
和上面差不多,不過每個(gè)格子里多了背景圖。
1. 下載上面兩張圖,命名為cell-blue.jpg和cell-grey.jpg
2. 拷貝下面的代碼到你想要的地方,記得修改圖片url
<!-- CSS goes in the document HEAD or added to your external stylesheet -->
<style type="text/css">
table.imagetable {
font-family: verdana,arial,sans-serif;
font-size:11px;
color:#333333;
border-width: 1px;
border-color: #999999;
border-collapse: collapse;
}
table.imagetable th {
background:#b5cfd2 url('cell-blue.jpg');
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #999999;
}
table.imagetable td {
background:#dcddc0 url('cell-grey.jpg');
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #999999;
}
</style>
<!-- Table goes in the document BODY -->
<table class="imagetable">
<tr>
<th>Info Header 1</th><th>Info Header 2</th><th>Info Header 3</th>
</tr>
<tr>
<td>Text 1A</td><td>Text 1B</td><td>Text 1C</td>
</tr>
<tr>
<td>Text 2A</td><td>Text 2B</td><td>Text 2C</td>
</tr>
</table>
3. 自動(dòng)換整行顏色的CSS樣式表格(需要用到JS)
這個(gè)CSS樣式表格自動(dòng)切換每一行的顏色,在我們需要頻繁更新一個(gè)大表格的時(shí)候很有用。
<!-- Javascript goes in the document HEAD -->
<script type="text/javascript">
function altRows(id){
if(document.getElementsByTagName){
var table = document.getElementById(id);
var rows = table.getElementsByTagName("tr");
for(i = 0; i < rows.length; i++){
if(i % 2 == 0){
rows[i].className = "evenrowcolor";
}else{
rows[i].className = "oddrowcolor";
}
}
}
}
window.onload=function(){
altRows('alternatecolor');
}
</script>
<!-- CSS goes in the document HEAD or added to your external stylesheet -->
<style type="text/css">
table.altrowstable {
font-family: verdana,arial,sans-serif;
font-size:11px;
color:#333333;
border-width: 1px;
border-color: #a9c6c9;
border-collapse: collapse;
}
table.altrowstable th {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #a9c6c9;
}
table.altrowstable td {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #a9c6c9;
}
.oddrowcolor{
background-color:#d4e3e5;
}
.evenrowcolor{
background-color:#c3dde0;
}
</style>
<!-- Table goes in the document BODY -->
<table class="altrowstable" id="alternatecolor">
<tr>
<th>Info Header 1</th><th>Info Header 2</th><th>Info Header 3</th>
</tr>
<tr>
<td>Text 1A</td><td>Text 1B</td><td>Text 1C</td>
</tr>
<tr>
<td>Text 2A</td><td>Text 2B</td><td>Text 2C</td>
</tr>
</tr>
<tr>
<td>Text 3A</td><td>Text 3B</td><td>Text 3C</td>
</tr>
<tr>
<td>Text 4A</td><td>Text 4B</td><td>Text 4C</td>
</tr>
<tr>
<td>Text 5A</td><td>Text 5B</td><td>Text 5C</td>
</tr>
</table>
<!-- The table code can be found here: http://www.textfixer/resources/css-tables.php#css-table03 -->
4. 鼠標(biāo)懸停高亮的CSS樣式表格 (需要JS)
純CSS顯示表格高亮在IE中顯示有問題,所以這邊使用了JS來做高亮(由于csdn博客限制了js的使用,我會(huì)在近期將博客遷移放到自己的web主機(jī)上)。
<!-- CSS goes in the document HEAD or added to your external stylesheet -->
<style type="text/css">
table.hovertable {
font-family: verdana,arial,sans-serif;
font-size:11px;
color:#333333;
border-width: 1px;
border-color: #999999;
border-collapse: collapse;
}
table.hovertable th {
background-color:#c3dde0;
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #a9c6c9;
}
table.hovertable tr {
background-color:#d4e3e5;
}
table.hovertable td {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #a9c6c9;
}
</style>
<!-- Table goes in the document BODY -->
<table class="hovertable">
<tr>
<th>Info Header 1</th><th>Info Header 2</th><th>Info Header 3</th>
</tr>
<tr onmouseover="this.style.backgroundColor='#ffff66';"
onmouseout="this.style.backgroundColor='#d4e3e5';">
<td>Item 1A</td><td>Item 1B</td><td>Item 1C</td>
</tr>
<tr onmouseover="this.style.backgroundColor='#ffff66';"
onmouseout="this.style.backgroundColor='#d4e3e5';">
<td>Item 2A</td><td>Item 2B</td><td>Item 2C</td>
</tr>
<tr onmouseover="this.style.backgroundColor='#ffff66';"
onmouseout="this.style.backgroundColor='#d4e3e5';">
<td>Item 3A</td><td>Item 3B</td><td>Item 3C</td>
</tr>
<tr onmouseover="this.style.backgroundColor='#ffff66';"
onmouseout="this.style.backgroundColor='#d4e3e5';">
<td>Item 4A</td><td>Item 4B</td><td>Item 4C</td>
</tr>
<tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">
<td>Item 5A</td><td>Item 5B</td><td>Item 5C</td>
</tr>
</table>
文/丁向明
做一個(gè)有博客的web前端自媒體人,專注web前端開發(fā),關(guān)注用戶體驗(yàn),加我qq/微信交流:6135833
http://dingxiangming.com
現(xiàn)代網(wǎng)頁設(shè)計(jì)中,表格依然扮演著不可或缺的角色。無論是數(shù)據(jù)展示、報(bào)表制作還是復(fù)雜布局,合理運(yùn)用HTML中的<table>標(biāo)簽可以極大地提升網(wǎng)頁的信息結(jié)構(gòu)和用戶體驗(yàn)。本文將詳細(xì)解析HTML表格的高級(jí)技巧和創(chuàng)新應(yīng)用,幫助開發(fā)者和設(shè)計(jì)師精確掌握使用HTML表格的最佳實(shí)踐。
HTML表格由<table>標(biāo)簽創(chuàng)建,基本結(jié)構(gòu)包括<thead>、<tbody>、<tfoot>和<tr>(表格行),以及<th>(表頭單元格)和<td>(表格單元格)。
<table>
<thead>
<tr>
<th>編號(hào)</th>
<th>姓名</th>
<th>年齡</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>張三</td>
<td>25</td>
</tr>
<tr>
<td>2</td>
<td>李四</td>
<td>30</td>
</tr>
</tbody>
</table>
這個(gè)例子展示了一個(gè)包含標(biāo)題和兩行數(shù)據(jù)的基本表格。
為表格添加CSS樣式可以提升其視覺效果。例如,可以通過以下CSS代碼增加邊框、調(diào)整文字對(duì)齊方式,以及改善表格的顏色和間隔。
table {
width: 100%;
border-collapse: collapse;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
thead {
background-color: #f2f2f2;
}
在移動(dòng)設(shè)備上查看時(shí),表格應(yīng)能自動(dòng)調(diào)整以適應(yīng)不同的屏幕尺寸。可以使用CSS的媒體查詢來實(shí)現(xiàn)響應(yīng)式表格,或者利用JavaScript進(jìn)行更復(fù)雜的操作。
使用rowspan和colspan屬性可以合并行或列,創(chuàng)建跨多個(gè)行或列的單元格,這對(duì)于匯總信息特別有用。
<tr>
<td rowspan="2">合并行</td>
<td>數(shù)據(jù)1</td>
<td>數(shù)據(jù)2</td>
</tr>
<tr>
<td>數(shù)據(jù)3</td>
<td>數(shù)據(jù)4</td>
</tr>
通過JavaScript和AJAX,可以實(shí)現(xiàn)表格的動(dòng)態(tài)數(shù)據(jù)加載和更新,這對(duì)于需要實(shí)時(shí)數(shù)據(jù)顯示的應(yīng)用尤為重要。
掌握HTML表格的使用和優(yōu)化不僅能提升網(wǎng)頁的功能性和美觀,還能改善用戶的瀏覽體驗(yàn)。隨著技術(shù)的不斷進(jìn)步,我們預(yù)見表格在網(wǎng)頁設(shè)計(jì)中的應(yīng)用將更加靈活和強(qiáng)大。
結(jié)尾部分:
希望本文能為你在使用HTML表格時(shí)提供新的視角和方法。記得實(shí)踐是檢驗(yàn)真理的唯一標(biāo)準(zhǔn),不斷嘗試和優(yōu)化,是每個(gè)網(wǎng)頁設(shè)計(jì)師和開發(fā)者成長(zhǎng)的必經(jīng)之路。
*請(qǐng)認(rèn)真填寫需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。