<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CSS Table(表格)</title>
<style>
table.a,th,td{
border: 1px solid red;
}
table.b {
border-collapse: collapse;
}
table.b,th,td {
border: 1px solid black;
}
table.c,td,th{
border: 1px solid black;
}
table.c{
width: 100%;
}
table.c th{
height: 100px;
}
table.d{
border-collapse: collapse;
}
table.d,th,td{
border: 1px solid black;
}
table.d th,td{
width: 100px;
height: 50px;
text-align: right;
}
table.e,td,th{
border: 1px solid green;
}
table.e caption{
caption-side: bottom;
}
table.e th{
background-color: green;
color: white;
}
table.e td{
padding: 50px;
}
#customers{
font-family: "Trebuchet MS",Arial,sans-serif;
width: 100%;
border-collapse: collapse;
}
#customers td,#customers th{
font-size: 1em;
border: 1px solid #98bf21;
padding: 3px 7px 2px 7px;
}
#customers th{
font-size: 1.1em;
text-align: left;
padding-top: 5px;
padding-bottom: 4px;
background-color: #A7C942;
}
#customers tr.alt td{
color: #000000;
background-color: #EAF2D3;
}
</style>
</head>
<body>
<p>实例1</p>
<table class="a">
<tr>
<th>FirstName</th>
<th>LastName</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
</tr>
</table>
<p>实例2</p>
<table class="b">
<tr>
<th>FirstName</th>
<th>LastName</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
</tr>
</table>
<p><b>注意:</b> 如果没有指定 !DOCTYPE border-collapse 属性在 IE8 及更早 IE 版本中是不起作用的。</p>
<p>实例3</p>
<table class="c">
<tr>
<th>FirstName</th>
<th>LastName</th>
<th>Tel</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
<td>15711275623</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
<td>15071290124</td>
</tr>
<tr>
<td>Joe</td>
<td>Smith</td>
<td>13525697852</td>
</tr>
</table>
<p>实例4</p>
<table class="d">
<tr>
<th>FirstName</th>
<th>LastName</th>
<th>Savings</th>
</tr>
<tr>
<td>Lois</td>
<td>Smith</td>
<td>$100</td>
</tr>
<tr>
<td>Peter</td>
<td>Smith</td>
<td>$150</td>
</tr>
<tr>
<td>Joe</td>
<td>Griffin</td>
<td>$200</td>
</tr>
</table>
<p>实例5</p>
<table class="e">
<caption>Table 1.1 Customers</caption>
<tr>
<th>FirstName</th>
<th>LastName</th>
<th>Savings</th>
</tr>
<tr>
<td>Lois</td>
<td>Smith</td>
<td>$50</td>
</tr>
<tr>
<td>Joe</td>
<td>Smith</td>
<td>$120</td>
</tr>
<tr>
<td>Peter</td>
<td>Smith</td>
<td>$75</td>
</tr>
</table>
<p>实例6</p>
<table id="customers">
<tr>
<th>Company</th>
<th>Contact</th>
<th>Country</th>
</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td>Maria Anders</td>
<td>Germany</td>
</tr>
<tr class="alt">
<td>Berglunds snabbköp</td>
<td>Christina Berglund</td>
<td>Sweden</td>
</tr>
<tr>
<td>Centro comercial Moctezuma</td>
<td>Francisco Chang</td>
<td>Mexico</td>
</tr>
<tr class="alt">
<td>Ernst Handel</td>
<td>Roland Mendel</td>
<td>Austria</td>
</tr>
<tr>
<td>Island Trading</td>
<td>Helen Bennett</td>
<td>UK</td>
</tr>
<tr class="alt">
<td>Königlich Essen</td>
<td>Philip Cramer</td>
<td>Germany</td>
</tr>
<tr>
<td>Laughing Bacchus Winecellars</td>
<td>Yoshi Tannamuri</td>
<td>Canada</td>
</tr>
<tr class="alt">
<td>Magazzini Alimentari Riuniti</td>
<td>Giovanni Rovelli</td>
<td>Italy</td>
</tr>
<tr>
<td>North/South</td>
<td>Simon Crowther</td>
<td>UK</td>
</tr>
<tr class="alt">
<td>Paris spécialités</td>
<td>Marie Bertrand</td>
<td>France</td>
</tr>
</table>
</body>
</html>
<!--
表格边框
指定CSS表格边框,使用border属性。
下面的例子指定了一个表格的Th和TD元素的黑色边框:
实例
table, th, td
{
border: 1px solid black;
}
请注意,在上面的例子中的表格有双边框。这是因为表和th/ td元素有独立的边界。
为了显示一个表的单个边框,使用 border-collapse属性
-->
<!--
折叠边框
border-collapse 属性设置表格的边框是否被折叠成一个单一的边框或隔开:
实例
table
{
border-collapse:collapse;
}
table,th, td
{
border: 1px solid black;
}
-->
<!--
表格宽度和高度
Width和height属性定义表格的宽度和高度。
下面的例子是设置100%的宽度,50像素的th元素的高度的表格:
实例
table
{
width:100%;
}
th
{
height:50px;
}
-->
<!--
表格文字对齐
表格中的文本对齐和垂直对齐属性。
text-align属性设置水平对齐方式,向左,右,或中心:
实例
td
{
text-align:right;
}
垂直对齐属性设置垂直对齐,比如顶部,底部或中间:
实例
td
{
height:50px;
vertical-align:bottom;
}
-->
<!--
表格颜色
下面的例子指定边框的颜色,和th元素的文本和背景颜色:
实例
table, td, th
{
border:1px solid green;
}
th
{
background-color:green;
color:white;
}
-->
网友评论