表用于显示表格数据。下面是各种表元素的快速概述。
表允许您以一种很好的结构化方式呈现表格数据。表在网格中显示数据 — 包含行和列。文章源自你的网络首码项目网-https://www.youranweb.com/101.html
基本表元素
在 HTML 中,您可以使用 <table>
元素以及 <tr>
和 <td>
元素创建表。文章源自你的网络首码项目网-https://www.youranweb.com/101.html
每组 <tr>
标记(开始标记和结束标记)表示表中嵌套它们的一行。每组 <td>
标记都表示嵌套在其中的行中的一个表数据单元格。文章源自你的网络首码项目网-https://www.youranweb.com/101.html
您还可以使用<th>
元素添加表标题。文章源自你的网络首码项目网-https://www.youranweb.com/101.html
<table> <tr> <th>Table Header</th> <th>Table Header</th> <th>Table Header</th> </tr> <tr> <td>Table cell</td> <td>Table cell</td> <td>Table cell</td> </tr> <tr> <td>Table cell</td> <td>Table cell</td> <td>Table cell</td> </tr> </table>
表格边框
可以使用 CSS 向整个表格或单个表格单元格(或两者)添加边框。仅将其添加到表格中将导致表格外部周围出现边框,但不会在每个单元格周围产生边框。所以你不会得到那种网格般的效果。文章源自你的网络首码项目网-https://www.youranweb.com/101.html
在大多数情况下,您可能希望向所有这些元素添加边框。文章源自你的网络首码项目网-https://www.youranweb.com/101.html
您可以使用内联样式,但这需要将 CSS 代码添加到每个 <td>
元素。文章源自你的网络首码项目网-https://www.youranweb.com/101.html
相反,在嵌入式或外部样式表中定义边框通常更有效。这样,您就可以将边框应用于单个声明中的所有表格单元格。文章源自你的网络首码项目网-https://www.youranweb.com/101.html
为此,只需将边框样式放在<style>
元素内即可。文章源自你的网络首码项目网-https://www.youranweb.com/101.html
喜欢这个:文章源自你的网络首码项目网-https://www.youranweb.com/101.html
<style> table, th, td { border: 1px solid orange; } </style>
因此,添加这些样式后,您的文档可能如下所示。文章源自你的网络首码项目网-https://www.youranweb.com/101.html
<!DOCTYPE html> <html> <head> <title>Table Example</title> <style> table, th, td { border: 1px solid orange; } </style> </head> <body> <table> <tr> <th>Table Header</th> <th>Table Header</th> <th>Table Header</th> </tr> <tr> <td>Table cell</td> <td>Table cell</td> <td>Table cell</td> </tr> <tr> <td>Table cell</td> <td>Table cell</td> <td>Table cell</td> </tr> </table> </body> </html>
HTML5 规范实际上包含表的属性,但对于大多数设计目的来说,它通常被认为是不够的。您可以用于无边框,也可以用于边框。但是,HTML 中没有用于设置边框样式的机制。border
border="0"
border="1"
文章源自你的网络首码项目网-https://www.youranweb.com/101.html
因此,大多数开发人员使用 CSS 向表格添加边框。他们通常不会打扰这个属性。border
文章源自你的网络首码项目网-https://www.youranweb.com/101.html
此外,该属性仅受 W3C 版本的 HTML 支持(但不支持 WHATWG 版本)。border
文章源自你的网络首码项目网-https://www.youranweb.com/101.html
折叠边框
默认情况下,相邻单元格将具有自己不同的边框。这将导致一种“双边框”外观。文章源自你的网络首码项目网-https://www.youranweb.com/101.html
如果您愿意,可以保留它。但是,大多数开发人员更喜欢将边界折叠成一个实心边框。文章源自你的网络首码项目网-https://www.youranweb.com/101.html
若要折叠边框,请将以下内容添加到样式表中。文章源自你的网络首码项目网-https://www.youranweb.com/101.html
table { border-collapse: collapse; }
如果您愿意,还可以从table元素中删除边框,因为单元格边框将连接在一起以提供单一边框外观。文章源自你的网络首码项目网-https://www.youranweb.com/101.html
下面是文档现在的样子。文章源自你的网络首码项目网-https://www.youranweb.com/101.html
<!DOCTYPE html> <html> <head> <title>Table Example</title> <style> table { border-collapse: collapse; } th, td { border: 1px solid orange; } </style> </head> <body> <table> <tr> <th>Table Header</th> <th>Table Header</th> <th>Table Header</th> </tr> <tr> <td>Table cell</td> <td>Table cell</td> <td>Table cell</td> </tr> <tr> <td>Table cell</td> <td>Table cell</td> <td>Table cell</td> </tr> </table> </body> </html>
如果您有兴趣,下面是有关表格边框的更多详细信息。文章源自你的网络首码项目网-https://www.youranweb.com/101.html
单元格padding
可以使用 CSS padding属性将padding应用于单元格。您可以指定所需的填充量。
例如,若要应用 10 像素的填充,请将以下内容添加到样式表中。
padding: 10px;
下面是文档现在的样子。
<!DOCTYPE html> <html> <head> <title>Table Example</title> <style> table { border-collapse: collapse; } th, td { border: 1px solid orange; padding: 10px; } </style> </head> <body> <table> <tr> <th>Table Header</th> <th>Table Header</th> <th>Table Header</th> </tr> <tr> <td>Table cell</td> <td>Table cell</td> <td>Table cell</td> </tr> <tr> <td>Table cell</td> <td>Table cell</td> <td>Table cell</td> </tr> </table> </body> </html>
工作台宽度
可以使用 CSS 宽度属性以像素或百分比为单位指定width
。通过以像素为单位指定宽度,可以指定确切的宽度。百分比允许表格“增大”或“缩小”,具体取决于页面上的其他内容以及浏览器的宽度。
下面是使用百分比的示例。
table { width: 100%; }
请注意,在这种情况下,我们仅使用选择器,因为我们只设置表格的宽度,而不是单个单元格。table
下面是文档现在的样子。
<!DOCTYPE html> <html> <head> <title>Table Example</title> <style> table { border-collapse: collapse; width: 100%; } th, td { border: 1px solid orange; padding: 10px; } </style> </head> <body> <table> <tr> <th>Table Header</th> <th>Table Header</th> <th>Table Header</th> </tr> <tr> <td>Table cell</td> <td>Table cell</td> <td>Table cell</td> </tr> <tr> <td>Table cell</td> <td>Table cell</td> <td>Table cell</td> </tr> </table> </body> </html>
交替的背景色
如前所述,可以使用 CSS 背景色属性将background-color添加到 HTML 元素中。
您可以将背景色应用于整个表格,也可以仅应用部分表格,例如表格单元格或某些行等。
现在,让我们使用一个小的 CSS 技巧将交替的颜色应用于表的行。因此,第一行将是颜色 A,第二行将是颜色 B,第三行将是颜色 A,第四行将是颜色 B,依此类推。
为此,请使用 CSS 伪类选择器以及 and 值来确定奇数行和偶数行的背景色。:nth-child
odd
even
table.alt tr:nth-child(even) { background-color: #eee; } table.alt tr:nth-child(odd) { background-color: #fff; }
请注意,在此示例中,我还创建了一个名为 的类并将其应用于表,然后将该类用作样式表中选择器的一部分。因此,如果文档中有多个表,则只有具有该类的表才会应用这些样式。alt
以下是使用这些样式的文档的外观。
<!DOCTYPE html> <html> <head> <title>Table Example</title> <style> table { border-collapse: collapse; width: 100%; } th, td { border: 1px solid #ccc; padding: 10px; } table.alt tr:nth-child(even) { background-color: #eee; } table.alt tr:nth-child(odd) { background-color: #fff; } </style> </head> <body> <table class="alt"> <tr> <th>Table Header</th> <th>Table Header</th> </tr> <tr> <td>Table cell</td> <td>Table cell</td> </tr> <tr> <td>Table cell</td> <td>Table cell</td> </tr> <tr> <td>Table cell</td> <td>Table cell</td> </tr> <tr> <td>Table cell</td> <td>Table cell</td> </tr> <tr> <td>Table cell</td> <td>Table cell</td> </tr> </table> </body> </html>
科尔斯潘
可以使用 该属性使单元格跨越多个列。colspan
要使用它,只需提供单元格应跨越的列数。
下面是一个跨越两列的表头示例。
<!DOCTYPE html> <html> <head> <title>My Example</title> <style> table { border-collapse: collapse; width: 100%; } th, td { border: 1px solid #ccc; padding: 10px; } </style> </head> <body> <table> <tr> <th colspan="2">Table Header</th> <th>Table Header</th> </tr> <tr> <td>Table cell</td> <td>Table cell</td> <td>Table cell</td> </tr> <tr> <td>Table cell</td> <td>Table cell</td> <td>Table cell</td> </tr> </table> </body> </html>
执行此操作时,您需要删除不必要的单元格。因此,在上面的示例中,即使有三列,表中也只定义了两个表标题。
行跨度
行跨度对于行就像colspan对于列一样(行跨度允许单元格跨越多行)。
<!DOCTYPE html> <html> <head> <title>My Example</title> <style> table { border-collapse: collapse; width: 100%; } th, td { border: 1px solid #ccc; padding: 10px; } </style> </head> <body> <table> <tr> <th rowspan="4">Table Header</th> </tr> <tr> <td>Table cell</td> <td>Table cell</td> </tr> <tr> <td>Table cell</td> <td>Table cell</td> </tr> <tr> <td>Table cell</td> <td>Table cell</td> </tr> </table> </body> </html>
在网页的早期,网页设计师经常使用表格进行布局。例如,左列是一个大的表格单元格,主内容区域是另一个单元格,依此类推。这是因为表格提供的网格结构。
但是,不建议使用此技术。CSS(和浏览器支持)现在正处于可以使用HTML与CSS结合使用实现高级布局的阶段。
应使用 HTML 来提供文档结构和含义。应使用 CSS 进行演示。

评论