HTML系列(八):表格

时间:2021-09-05 21:19:10

一、基本表格:

表格标记<table>,行标记<tr>,单元格标记<td>

基本语法:

<table>
<tr>
<td>单元格内文字</td>
<td>单元格内文字</td>
......
</tr>
<tr>
<td>单元格内文字</td>
<td>单元格内文字</td>
......
</tr>
......
</table>

示例代码:

 <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<title>第9章</title>
</head>
<style type="text/css">
body {
font: normal 11px auto "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;
color: #4f6b72;
background: #E6EAE9;
} a {
color: #c75f3e;
} #mytable {
width: 700px;
padding: 0;
margin: 0;
} caption {
padding: 0 0 5px 0;
width: 700px;
font: italic 11px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;
text-align: right;
} th {
font: bold 11px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;
color: #4f6b72;
border-right: 1px solid #C1DAD7;
border-bottom: 1px solid #C1DAD7;
border-top: 1px solid #C1DAD7;
letter-spacing: 2px;
text-transform: uppercase;
text-align: left;
padding: 6px 6px 6px 12px;
background: #CAE8EA url(images/bg_header.jpg) no-repeat;
} th.nobg {
border-top: 0;
border-left: 0;
border-right: 1px solid #C1DAD7;
background: none;
} td {
border-right: 1px solid #C1DAD7;
border-bottom: 1px solid #C1DAD7;
background: #fff;
font-size: 11px;
padding: 6px 6px 6px 12px;
color: #4f6b72;
} td.alt {
background: #F5FAFA;
color: #797268;
} th.spec {
border-left: 1px solid #C1DAD7;
border-top: 0;
background: #fff url(images/bullet1.gif) no-repeat;
font: bold 10px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;
} th.specalt {
border-left: 1px solid #C1DAD7;
border-top: 0;
background: #f5fafa url(images/bullet2.gif) no-repeat;
font: bold 10px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;
color: #797268;
}
</style>
<body>
<table id="mytable" cellspacing="0" summary="The technical specifications of the Apple PowerMac G5 series">
<caption>The technical specifications of the Apple PowerMac G5 series</caption>
<tr>
<th scope="col" abbr="Configurations">设置</th>
<th scope="col" abbr="Dual 1.8">1.8GHz</th>
<th scope="col" abbr="Dual 2">2GHz</th>
<th scope="col" abbr="Dual 2.5">2.5GHz</th>
</tr>
<tr>
<th scope="row" abbr="Model" class="spec">lipeng</th>
<td>M9454LL/A</td>
<td>M9455LL/A</td>
<td>M9457LL/A</td>
</tr>
<tr>
<th scope="row" abbr="G5 Processor" class="specalt">mapabc</th>
<td class="alt">Dual 1.8GHz PowerPC G5</td>
<td class="alt">Dual 2GHz PowerPC G5</td>
<td class="alt">Dual 2.5GHz PowerPC G5</td>
</tr>
<tr>
<th scope="row" abbr="Frontside bus" class="spec">Lennvo</th>
<td>900MHz per processor</td>
<td>1GHz per processor</td>
<td>1.25GHz per processor</td>
</tr>
<tr>
<th scope="row" abbr="L2 Cache" class="specalt">Black</th>
<td class="alt">512K per processor</td>
<td class="alt">512K per processor</td>
<td class="alt">512K per processor</td>
</tr>
</table>
</body>
</html>

二、让表格没有凹凸感

没有样式的情况下,表格边框是凹凸的,可以使用cellspacing和cellpadding来取消凹凸感。cellspacing是td与td之间的距离,而cellpadding是单元格内部内容与单元格边界之间的空白距离的大小。

例如:

 <table border="1" cellpadding="0" cellspacing="0">
<tr>
<th>单元格内的标题</th>
<th>单元格内的标题</th>
</tr>
<tr>
<td>单元格内的文字</td>
<td>单元格内的文字</td>
</tr>
<tr>
<td>单元格内的文字</td>
<td>单元格内的文字</td>
</tr>
</table>

三、添加表头th

 <!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport" />
<meta content="yes" name="apple-mobile-web-app-capable" />
<meta content="black" name="apple-mobile-web-app-status-bar-style" />
<meta name="format-detection" content="telephone=no" />
<title>第9章</title>
</head>
<body>
<table cellspacing="0">
<tr>
<th>序号</th>
<th>歌曲名</th>
<th>演唱</th>
</tr>
<tr>
<th>01</th>
<td>小苹果</td>
<td>筷子兄弟</td>
</tr>
<tr>
<th>02</th>
<td>匆匆那年</td>
<td>王菲</td>
</tr>
<tr>
<th>03</th>
<td>喜欢你</td>
<td>G.E.M.邓紫棋</td>
</tr>
<tr>
<th>04</th>
<td>当你老了</td>
<td>莫文蔚</td>
</tr>
</table>
<body>
</body>
</html>

为了更进一步区分表头与内容,对表格进行样式设计,顺便添加<thead>,<tbody>,<tfoot>标签为表格完善结构,更进一步区别不同部分:

 <!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport" />
<meta content="yes" name="apple-mobile-web-app-capable" />
<meta content="black" name="apple-mobile-web-app-status-bar-style" />
<meta name="format-detection" content="telephone=no" />
<title>第9章</title>
<style type="text/css">
th {
font: bold 11px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;
color: #4f6b72;
border-right: 1px solid #C1DAD7;
border-bottom: 1px solid #C1DAD7;
border-top: 1px solid #C1DAD7;
letter-spacing: 2px;
text-transform: uppercase;
text-align: left;
padding: 6px 6px 6px 12px;
background: #CAE8EA url(images/bg_header.jpg) no-repeat;
} td {
border-right: 1px solid #C1DAD7;
border-bottom: 1px solid #C1DAD7;
background: #fff;
font-size: 11px;
padding: 6px 6px 6px 12px;
color: #4f6b72;
}
thead th {
color: red;
}
tfoot th {
color: blue;
}
</style>
</head>
<body>
<table cellspacing="0">
<thead>
<tr>
<th>序号</th>
<th>歌曲名</th>
<th>演唱</th>
</tr>
</thead>
<tbody>
<tr>
<th>01</th>
<td>小苹果</td>
<td>筷子兄弟</td>
</tr>
<tr>
<th>02</th>
<td>匆匆那年</td>
<td>王菲</td>
</tr>
<tr>
<th>03</th>
<td>喜欢你</td>
<td>G.E.M.邓紫棋</td>
</tr>
<tr>
<th>04</th>
<td>当你老了</td>
<td>莫文蔚</td>
</tr>
</tbody>
<tfoot>
<tr>
<th>序号</th>
<th>歌曲名</th>
<th>演唱</th>
</tr>
</tfoot>
</table>
<body>
</body>
</html>

四、不规则表格

colspan 属性规定单元格可横跨的列数。rowspan 属性规定单元格可横跨的行数。

 <!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport" />
<meta content="yes" name="apple-mobile-web-app-capable" />
<meta content="black" name="apple-mobile-web-app-status-bar-style" />
<meta name="format-detection" content="telephone=no" />
<title>第9章</title>
<style type="text/css">
th {
font: bold 11px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;
color: #4f6b72;
border-right: 1px solid #C1DAD7;
border-bottom: 1px solid #C1DAD7;
border-top: 1px solid #C1DAD7;
letter-spacing: 2px;
text-transform: uppercase;
text-align: left;
padding: 6px 6px 6px 12px;
background: #CAE8EA url(images/bg_header.jpg) no-repeat;
} td {
border-right: 1px solid #C1DAD7;
border-bottom: 1px solid #C1DAD7;
background: #fff;
font-size: 11px;
padding: 6px 6px 6px 12px;
color: #4f6b72;
}
</style>
</head>
<body>
<table cellspacing="0">
<thead>
<tr>
<th>序号</th>
<th>歌曲名</th>
<th>演唱</th>
</tr>
</thead>
<tbody>
<tr>
<th>01</th>
<td>小苹果</td>
<td>筷子兄弟</td>
</tr>
<tr>
<th>02</th>
<td>匆匆那年</td>
<td colspan="1" rowspan="2">王菲</td>
</tr>
<tr>
<th>03</th>
<td>致青春</td>
</tr>
<tr>
<th>04</th>
<td>喜欢你</td>
<td>G.E.M.邓紫棋</td>
</tr>
<tr>
<th>05</th>
<td>当你老了</td>
<td>莫文蔚</td>
</tr>
<tr>
<th>06</th>
<td colspan="2" rowspan="1">群星演唱最炫小苹果</td>
</tr>
</tbody>
</table>
<body>
</body>
</html>

五、几种常见表格设计

1、圆角表格

 <!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport" />
<meta content="yes" name="apple-mobile-web-app-capable" />
<meta content="black" name="apple-mobile-web-app-status-bar-style" />
<meta name="format-detection" content="telephone=no" />
<title>第9章</title> </head>
<body>
<style> body {
width: 600px;
margin: 40px auto;
font-family: 'trebuchet MS', 'Lucida sans', Arial;
font-size: 14px;
color: #444;
} table {
*border-collapse: collapse; /* IE7 and lower */
border-spacing: 0;
width: 100%;
} .bordered {
border: solid #ccc 1px;
-moz-border-radius: 6px;
-webkit-border-radius: 6px;
border-radius: 6px;
-webkit-box-shadow: 0 1px 1px #ccc;
-moz-box-shadow: 0 1px 1px #ccc;
box-shadow: 0 1px 1px #ccc;
} .bordered tr:hover {
background: #fbf8e9;
-o-transition: all 0.1s ease-in-out;
-webkit-transition: all 0.1s ease-in-out;
-moz-transition: all 0.1s ease-in-out;
-ms-transition: all 0.1s ease-in-out;
transition: all 0.1s ease-in-out;
} .bordered td, .bordered th {
border-left: 1px solid #ccc;
border-top: 1px solid #ccc;
padding: 10px;
text-align: left;
} .bordered th {
background-color: #dce9f9;
background-image: -webkit-gradient(linear, left top, left bottom, from(#ebf3fc), to(#dce9f9));
background-image: -webkit-linear-gradient(top, #ebf3fc, #dce9f9);
background-image: -moz-linear-gradient(top, #ebf3fc, #dce9f9);
background-image: -ms-linear-gradient(top, #ebf3fc, #dce9f9);
background-image: -o-linear-gradient(top, #ebf3fc, #dce9f9);
background-image: linear-gradient(top, #ebf3fc, #dce9f9);
-webkit-box-shadow: 0 1px 0 rgba(255,255,255,.8) inset;
-moz-box-shadow:0 1px 0 rgba(255,255,255,.8) inset;
box-shadow: 0 1px 0 rgba(255,255,255,.8) inset;
border-top: none;
text-shadow: 0 1px 0 rgba(255,255,255,.5);
} .bordered td:first-child, .bordered th:first-child {
border-left: none;
} .bordered th:first-child {
-moz-border-radius: 6px 0 0 0;
-webkit-border-radius: 6px 0 0 0;
border-radius: 6px 0 0 0;
} .bordered th:last-child {
-moz-border-radius: 0 6px 0 0;
-webkit-border-radius: 0 6px 0 0;
border-radius: 0 6px 0 0;
} .bordered th:only-child{
-moz-border-radius: 6px 6px 0 0;
-webkit-border-radius: 6px 6px 0 0;
border-radius: 6px 6px 0 0;
} .bordered tr:last-child td:first-child {
-moz-border-radius: 0 0 0 6px;
-webkit-border-radius: 0 0 0 6px;
border-radius: 0 0 0 6px;
} .bordered tr:last-child td:last-child {
-moz-border-radius: 0 0 6px 0;
-webkit-border-radius: 0 0 6px 0;
border-radius: 0 0 6px 0;
} </style>
<table class="bordered">
<caption>金曲排行</caption>
<thead>
<tr>
<th>序号</th>
<th>歌曲名</th>
<th>演唱</th>
<th>人气</th>
</tr>
</thead>
<tbody>
<tr>
<th>01</th>
<td>小苹果</td>
<td>筷子兄弟</td>
<td>120093</td>
</tr>
<tr>
<th>02</th>
<td>匆匆那年</td>
<td colspan="1" rowspan="2">王菲</td>
<td colspan="1" rowspan="2">38490</td>
</tr>
<tr>
<th>03</th>
<td>致青春</td>
</tr>
<tr>
<th>04</th>
<td>喜欢你</td>
<td>G.E.M.邓紫棋</td>
<td>37449</td>
</tr>
<tr>
<th>05</th>
<td>当你老了</td>
<td>莫文蔚</td>
<td>93947</td>
</tr>
<tr>
<th>06</th>
<td colspan="2" rowspan="1">群星演唱最炫小苹果</td>
<td>93984</td>
</tr>
</tbody>
</table>
<body>
</body>
</html>

2、条纹表格

 <!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport" />
<meta content="yes" name="apple-mobile-web-app-capable" />
<meta content="black" name="apple-mobile-web-app-status-bar-style" />
<meta name="format-detection" content="telephone=no" />
<title>第9章</title> </head>
<body>
<style>
body {
width: 600px;
margin: 40px auto;
font-family: 'trebuchet MS', 'Lucida sans', Arial;
font-size: 14px;
color: #444;
} table {
*border-collapse: collapse; /* IE7 and lower */
border-spacing: 0;
width: 100%;
} .zebra td, .zebra th {
padding: 10px;
border-bottom: 1px solid #f2f2f2;
} .zebra tbody tr:nth-child(even) {
background: #f5f5f5;
-webkit-box-shadow: 0 1px 0 rgba(255,255,255,.8) inset;
-moz-box-shadow:0 1px 0 rgba(255,255,255,.8) inset;
box-shadow: 0 1px 0 rgba(255,255,255,.8) inset;
} .zebra th {
text-align: left;
text-shadow: 0 1px 0 rgba(255,255,255,.5);
border-bottom: 1px solid #ccc;
background-color: #eee;
background-image: -webkit-gradient(linear, left top, left bottom, from(#f5f5f5), to(#eee));
background-image: -webkit-linear-gradient(top, #f5f5f5, #eee);
background-image: -moz-linear-gradient(top, #f5f5f5, #eee);
background-image: -ms-linear-gradient(top, #f5f5f5, #eee);
background-image: -o-linear-gradient(top, #f5f5f5, #eee);
background-image: linear-gradient(top, #f5f5f5, #eee);
} .zebra th:first-child {
-moz-border-radius: 6px 0 0 0;
-webkit-border-radius: 6px 0 0 0;
border-radius: 6px 0 0 0;
} .zebra th:last-child {
-moz-border-radius: 0 6px 0 0;
-webkit-border-radius: 0 6px 0 0;
border-radius: 0 6px 0 0;
} .zebra th:only-child{
-moz-border-radius: 6px 6px 0 0;
-webkit-border-radius: 6px 6px 0 0;
border-radius: 6px 6px 0 0;
} .zebra tfoot td {
border-bottom: 0;
border-top: 1px solid #fff;
background-color: #f1f1f1;
} .zebra tfoot td:first-child {
-moz-border-radius: 0 0 0 6px;
-webkit-border-radius: 0 0 0 6px;
border-radius: 0 0 0 6px;
} .zebra tfoot td:last-child {
-moz-border-radius: 0 0 6px 0;
-webkit-border-radius: 0 0 6px 0;
border-radius: 0 0 6px 0;
} .zebra tfoot td:only-child{
-moz-border-radius: 0 0 6px 6px;
-webkit-border-radius: 0 0 6px 6px
border-radius: 0 0 6px 6px
} </style>
<table class="zebra">
<caption>金曲排行</caption>
<thead>
<tr>
<th>序号</th>
<th>歌曲名</th>
<th>演唱</th>
<th>人气</th>
</tr>
</thead>
<tfoot>
<tr>
<td>&nbsp;</td>
<td></td>
<td></td>
<td></td>
</tr>
</tfoot>
<tbody>
<tr>
<td>01</td>
<td>小苹果</td>
<td>筷子兄弟</td>
<td>1200903</td>
</tr>
<tr>
<td>02</td>
<td>匆匆那年</td>
<td>王菲</td>
<td>138490</td>
</tr>
<tr>
<td>03</td>
<td>致青春</td>
<td>王菲</td>
<td>138489</td>
</tr>
<tr>
<td>04</td>
<td>喜欢你</td>
<td>G.E.M.邓紫棋</td>
<td>137449</td>
</tr>
<tr>
<td>05</td>
<td>当你老了</td>
<td>莫文蔚</td>
<td>93947</td>
</tr>
<tr>
<td>06</td>
<td colspan="2" rowspan="1">群星演唱最炫小苹果</td>
<td>93984</td>
</tr>
</tbody>
</table>
<body>
</body>
</html>