溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

CSS如何設(shè)置表格的外觀樣式

發(fā)布時間:2022-02-22 10:38:17 來源:億速云 閱讀:211 作者:小新 欄目:開發(fā)技術(shù)

小編給大家分享一下CSS如何設(shè)置表格的外觀樣式,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

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

表格邊框
如需在 CSS 中設(shè)置表格邊框,請使用 border 屬性。如為 table、th 以及 td 設(shè)置藍(lán)色邊框:

table, th, td {border: 1px solid blue;}

注釋:本實(shí)例中的表格具有雙線條邊框。這是由于 table、th 以及 td 元素都有獨(dú)立的邊框。
提示:如果需要把表格顯示為單線條邊框,請使用 border-collapse 屬性。

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

table {border-collapse: collapse;}
table,th, td {border: 1px solid black;}


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

table {width: 100%;}
th {height:50px;}

注釋:本實(shí)例將表格寬度設(shè)置為 100%,同時將 th 元素的高度設(shè)置為 50px。

表格文本對齊
text-align 和 vertical-align 屬性設(shè)置表格中文本的對齊方式。
text-align 屬性設(shè)置水平對齊方式,比如左對齊、右對齊或者居中:

td {text-align: right;}

vertical-align 屬性設(shè)置垂直對齊方式,比如頂部對齊、底部對齊或居中對齊:

td {height: 50px; vertical-align: bottom;}


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

td {padding: 15px;}


表格顏色
下面的例子設(shè)置邊框的顏色,以及 th 元素的文本和背景顏色:

table, td, th {border: 1px solid green;}
th {background-color: green; color: white;}


表格實(shí)例

<html>
<head>
<style type="text/css">
    #customers {
    font-family: "Trebuchet MS", Arial, Helvetica, 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;
    color: #ffffff;
    }
    #customers tr.alt td {
    color: #000000;
    background-color: #EAF2D3;
    }
</style>
</head>
<body>
<table id="customers">
    <tr>
        <th>Company</th>
        <th>Contact</th>
        <th>Country</th>
    </tr>
    <tr>
        <td>Apple</td>
        <td>Steven Jobs</td>
        <td>USA</td>
    </tr>
    <tr class="alt">
        <td>Baidu</td>
        <td>Li YanHong</td>
        <td>China</td>
    </tr>
    <tr>
        <td>Google</td>
        <td>Larry Page</td>
        <td>USA</td>
    </tr>
    <tr class="alt">
        <td>Lenovo</td>
        <td>Liu Chuanzhi</td>
        <td>China</td>
    </tr>
    <tr>
        <td>Microsoft</td>
        <td>Bill Gates</td>
        <td>USA</td>
    </tr>
    <tr class="alt">
        <td>Nokia</td>
        <td>Stephen Elop</td>
        <td>Finland</td>
    </tr>
</table>
</body>
</html>


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

以上是“CSS如何設(shè)置表格的外觀樣式”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

css
AI