日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > CSS >内容正文

CSS

6、CSS表格

發布時間:2025/3/20 CSS 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 6、CSS表格 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

CSS 表格屬性可以極大地改善表格的外觀。

CSS Table 屬性

屬性描述
border-collapse設置是否把表格邊框合并為單一的邊框。
border-spacing設置分隔單元格邊框的距離。
caption-side設置表格標題的位置。
empty-cells設置是否顯示表格中的空單元格。
table-layout設置顯示單元、行和列的算法。

1、 表格邊框border

如需在 CSS 中設置表格邊框,請使用 border 屬性。

下面的例子為 table、th 以及 td 設置了藍色邊框:

table, th, td{border: 1px solid blue;} <html> <head> <style type="text/css"> table,th,td { border:1px solid blue; } </style> </head><body> <table> <tr> <th>Firstname</th> <th>Lastname</th> </tr> <tr> <td>Bill</td> <td>Gates</td> </tr> <tr> <td>Steven</td> <td>Jobs</td> </tr> </table> </body> </html>


上例中的表格具有雙線條邊框。這是由于 table、th 以及 td 元素都有獨立的邊框。

如果需要把表格顯示為單線條邊框,請使用 border-collapse 屬性。

2、折疊邊框border-collapse

border-collapse 屬性設置是否將表格邊框折疊為單一邊框:

table{border-collapse:collapse;}table,th, td{border: 1px solid black;} <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <style type="text/css"> table{border-collapse:collapse;}table, td, th{border:1px solid black;} </style> </head><body> <table> <tr> <th>Firstname</th> <th>Lastname</th> </tr> <tr> <td>Bill</td> <td>Gates</td> </tr> <tr> <td>Steven</td> <td>Jobs</td> </tr> </table> <p><b>注釋:</b>如果沒有規定 !DOCTYPE,border-collapse 屬性可能會引起意想不到的錯誤。</p> </body> </html>

3、表格寬度和高度

通過 widthheight 屬性定義表格的寬度和高度。

下面的例子將表格寬度設置為 100%,同時將 th 元素的高度設置為 50px:

table{width:100%;}th{height:50px;} <html> <head> <style type="text/css"> table,td,th{border:1px solid black;}table{width:100%;}th{height:50px;} </style> </head><body> <table> <tr> <th>Firstname</th> <th>Lastname</th> </tr> <tr> <td>Bill</td> <td>Gates</td> </tr> <tr> <td>Steven</td> <td>Jobs</td> </tr> </table> </body> </html>

4、表格文本對齊text-align,vertical-align

text-alignvertical-align 屬性設置表格中文本的對齊方式。

  • text-align 屬性設置水平對齊方式,比如左對齊、右對齊或者居中:
td{text-align:right;} <html> <head> <style type="text/css"> table,td,th { border:1px solid black; } td { text-align:right; } </style> </head><body> <table> <tr> <th>Firstname</th> <th>Lastname</th> </tr> <tr> <td>Bill</td> <td>Gates</td> </tr> <tr> <td>Steven</td> <td>Jobs</td> </tr> </table> </body> </html>

  • vertical-align 屬性設置垂直對齊方式,比如頂部對齊、底部對齊或居中對齊:
td{height:50px;vertical-align:bottom;} <html> <head> <style type="text/css"> table, td, th{border:1px solid black;} ? td{height:50px;vertical-align:bottom;} </style> </head> ? <body> <table> <tr> <th>Firstname</th> <th>Lastname</th> </tr> <tr> <td>Bill</td> <td>Gates</td> </tr> <tr> <td>Steven</td> <td>Jobs</td> </tr> </table> </body> </html> ?

5、表格內邊距 padding

如需控制表格中內容與邊框的距離,請為 td 和 th 元素設置 padding 屬性:

td{padding:15px;} <html> <head> <style type="text/css"> table, td, th{border:1px solid black;}td{padding:15px;} </style> </head><body> <table> <tr> <th>Firstname</th> <th>Lastname</th> </tr> <tr> <td>Bill</td> <td>Gates</td> </tr> <tr> <td>Steven</td> <td>Jobs</td> </tr> </table> </body> </html>

6、表格顏色

下面的例子設置邊框的顏色,以及 th 元素的文本和背景顏色:

table, td, th{border:1px solid green;}th{background-color:green;color:white;} <html> <head> <style type="text/css"> table, td, th{border:1px solid green;}th{background-color:green;color:white;} </style> </head><body> <table> <tr> <th>Firstname</th> <th>Lastname</th> </tr> <tr> <td>Bill</td> <td>Gates</td> </tr> <tr> <td>Steven</td> <td>Jobs</td> </tr> </table> </body> </html>

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的6、CSS表格的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。