目录

CSS - Tables

本教程将教您如何使用CSS设置HTML表的不同属性。 您可以设置表的以下属性 -

  • border-collapse指定浏览器是否应控制相互接触的外观,或者每个单元格是否应保持其样式。

  • border-spacing指定表格单元格之间应出现的宽度。

  • caption-side标题显示在“标题”元素中。 默认情况下,它们呈现在文档中的表格上方。 您可以使用caption-side属性来控制表格标题的位置。

  • empty-cells指定在单元格为空时是否应显示边框。

  • table-layout允许浏览器通过使用它为列的其余部分提供的第一个宽度属性来加速table-layout ,而不必在呈现之前加载整个表。

现在,我们将看到如何将这些属性与示例一起使用。

边界崩溃的财产

此属性可以有两个值collapseseparate 。 以下示例使用两个值 -

<html>
   <head>
      <style type = "text/css">
         table.one {border-collapse:collapse;}
         table.two {border-collapse:separate;}
         td.a {
            border-style:dotted; 
            border-width:3px; 
            border-color:#000000; 
            padding: 10px;
         }
         td.b {
            border-style:solid; 
            border-width:3px; 
            border-color:#333333; 
            padding:10px;
         }
      </style>
   </head>
   <body>
      <table class = "one">
         <caption>Collapse Border Example</caption>
         <tr><td class = "a"> Cell A Collapse Example</td></tr>
         <tr><td class = "b"> Cell B Collapse Example</td></tr>
      </table>
      <br />
      <table class = "two">
         <caption>Separate Border Example</caption>
         <tr><td class = "a"> Cell A Separate Example</td></tr>
         <tr><td class = "b"> Cell B Separate Example</td></tr>
      </table>
   </body>
</html>

它会产生以下结果 -

新页面打开

border-spacing属性

border-spacing属性指定分隔相邻单元格的距离。 边界。 它可以采用一个或两个值; 这些应该是长度单位。

如果提供一个值,它将应用于垂直和水平边框。 或者您可以指定两个值,在这种情况下,第一个引用水平间距,第二个引用垂直间距 -

NOTE - 不幸的是,此属性在Netscape 7或IE 6中不起作用。

<style type="text/css">
   /* If you provide one value */
   table.example {border-spacing:10px;}
   /* This is how you can provide two values */
   table.example {border-spacing:10px; 15px;}
</style>

现在让我们修改前面的例子,看看效果 -

<html>
   <head>
      <style type = "text/css">
         table.one {
            border-collapse:separate;
            width:400px;
            border-spacing:10px;
         }
         table.two {
            border-collapse:separate;
            width:400px;
            border-spacing:10px 50px;
         }
      </style>
   </head>
   <body>
      <table class = "one" border = "1">
         <caption>Separate Border Example with border-spacing</caption>
         <tr><td> Cell A Collapse Example</td></tr>
         <tr><td> Cell B Collapse Example</td></tr>
      </table>
      <br />
      <table class = "two" border = "1">
         <caption>Separate Border Example with border-spacing</caption>
         <tr><td> Cell A Separate Example</td></tr>
         <tr><td> Cell B Separate Example</td></tr>
      </table>
   </body>
</html> 

它会产生以下结果 -

新页面打开

标题侧属性

标题侧属性允许您指定

元素的内容应放置在与表的关系中的位置。 下表列出了可能的值。

此属性可以具有top, bottom, leftright四个值中的一个。 以下示例使用每个值。

NOTE - 这些属性可能不适用于您的IE浏览器。

<html>
   <head>
      <style type = "text/css">
         caption.top {caption-side:top}
         caption.bottom {caption-side:bottom}
         caption.left {caption-side:left}
         caption.right {caption-side:right}
      </style>
   </head>
   <body>
      <table style = "width:400px; border:1px solid black;">
         <caption class = "top">
            This caption will appear at the top
         </caption>
         <tr><td > Cell A</td></tr>
         <tr><td > Cell B</td></tr>
      </table>
      <br />
      <table style = "width:400px; border:1px solid black;">
         <caption class = "bottom">
            This caption will appear at the bottom
         </caption>
         <tr><td > Cell A</td></tr>
         <tr><td > Cell B</td></tr>
      </table>
      <br />
      <table style = "width:400px; border:1px solid black;">
         <caption class = "left">
            This caption will appear at the left
         </caption>
         <tr><td > Cell A</td></tr>
         <tr><td > Cell B</td></tr>
      </table>
      <br />
      <table style = "width:400px; border:1px solid black;">
         <caption class = "right">
            This caption will appear at the right
         </caption>
         <tr><td > Cell A</td></tr>
         <tr><td > Cell B</td></tr>
      </table>
   </body>
</html>

它会产生以下结果 -

新页面打开

空单元格属性

empty-cells属性指示没有任何内容的单元格是否应显示边框。

此属性可以具有三个值之一 - show, hideinherit

以下是用于隐藏

元素中空单元格边框的empty-cells属性。
<html>
   <head>
      <style type = "text/css">
         table.empty {
            width:350px;
            border-collapse:separate;
            empty-cells:hide;
         }
         td.empty {
            padding:5px;
            border-style:solid;
            border-width:1px;
            border-color:#999999;
         }
      </style>
   </head>
   <body>
      <table class = "empty">
         <tr>
            <th></th>
            <th>Title one</th>
            <th>Title two</th>
         </tr>
         <tr>
            <th>Row Title</th>
            <td class = "empty">value</td>
            <td class = "empty">value</td>
         </tr>
         <tr>
            <th>Row Title</th>
            <td class = "empty">value</td>
            <td class = "empty"></td>
         </tr>
      </table>
   </body>
</html> 

它会产生以下结果 -

新页面打开

表格布局属性

table-layout属性可以帮助您控制浏览器呈现或布置表的方式。

此属性可以具有以下三个值之一: fixed, autoinherit

以下示例显示了这些属性之间的差异。

NOTE - 许多浏览器不支持此属性,因此不要依赖此属性。

<html>
   <head>
      <style type = "text/css">
         table.auto {
            table-layout: auto
         }
         table.fixed {
            table-layout: fixed
         }
      </style>
   </head>
   <body>
      <table class = "auto" border = "1" width = "100%">
         <tr>
            <td width = "20%">1000000000000000000000000000</td>
            <td width = "40%">10000000</td>
            <td width = "40%">100</td>
         </tr>
      </table>
      <br />
      <table class = "fixed" border = "1" width = "100%">
         <tr>
            <td width = "20%">1000000000000000000000000000</td>
            <td width = "40%">10000000</td>
            <td width = "40%">100</td>
         </tr>
      </table>
   </body>
</html> 

它会产生以下结果 -

新页面打开
<上一篇.CSS - Links
CSS - Borders.下一篇>
↑回到顶部↑
WIKI教程 @2018