天天看點

CSS表格設定CSS表格設定

CSS表格設定

css表格屬性可以極大的改善表格的外觀

表格邊框

  • 在css中設定表格邊框,使用

    border

    屬性。
  • table

    th

    以及

    td

    設定藍色邊框👇
<html>
	<head>
		<style type="text/css">
			table,th,td{
				border:1px solid blue;
			}
		</style>
	</head>
	<body>
		<table>
			<tr>
				<th>男性</th>
				<th>女性</th>
			</tr>
			<tr>
				<td>藍拳</td>
				<td>芙蕾雅</td>
			</tr>
			<tr>
				<td>狂戰士</td>
				<td>花花</td>
			</tr>
		</table>
	</body>
</html>
           

👇運作結果👇

CSS表格設定CSS表格設定
  • 上例中的表格具有雙線條邊框。這是由于

    table

    th

    以及

    td

    元素都有獨立的邊框。
  • 如果需要把表格顯示為單線條,則需要使用

    border-collapse

    屬性。

折疊邊框

  • border-collapse

    屬性設定是否将表格邊框折疊為單一邊框
<!DOCTYPE html>
<html>
	<head>
		<style type="text/css">
			table{
				border-collapse:collapse;
  			}
  			
			table, td, th{
  				border:1px solid blue;
  			}
		</style>
	</head>
	<body>
		<table>
			<tr>
				<th>男性</th>
				<th>女性</th>
			</tr>
			<tr>
				<td>藍拳</td>
				<td>芙蕾雅</td>
			</tr>
			<tr>
				<td>狂戰士</td>
				<td>花花</td>
			</tr>
		</table>
	</body>
</html>
           

👇運作結果👇

CSS表格設定CSS表格設定
border-collapse
  • JavaScript文法👇
描述

spearate

預設值。邊框會被分開。不會忽略

border-spacing

empty-cells

屬性

collapse

如果可能,邊框會合并為一個單一的邊框。會忽略

border-spacing

empty-cells

屬性

inherit

規定應該從父元素繼承

border-collapse

屬性的值

表格寬度和高度

  • 通過

    width

    height

    屬性定義表格的寬度和高度。
table{
  width:100%;
}

th{
  height:50px;
}
           

表格文本對齊

  • text-align

    vertical-align

    屬性設定表格中文本的對齊方式
    • text-align

      屬性設定水準文本對齊方式,比如左對齊,右對齊或者居中對齊
    • vertical-align

      屬性設定垂直對齊方式,比如頂部對齊,底部對齊或者居中對齊

表格内邊距

  • 如需控制表格中内容與邊框的距離,請為

    td

    th

    元素設定

    padding

    屬性。

表格顔色

  • 如需控制表格的顔色請分别為

    table

    td

    th

    設定

    border

    (邊框顔色),

    background-color

    (背景顔色)和

    color

    (文字顔色)
屬性 描述

border-collapse

設定是否把表格邊框合并為單一的邊框

separ

——預設。邊框會被分開

collapse

——邊框會被合并為一個單一的邊框

border-spacing

設定分隔單元格邊框的距離

(僅用于“分離邊框”模式)

兩個長度——前者對應水準間距,後者對用垂直

一個長度——定義水準和垂直的間距

caption-side

設定表格标題的位置

top

——預設值。把标題定位在表格之上

bottom

——把表格定位在表格之下

empty-cells

設定是否顯示表格中的空單元格

僅用于“分離邊框”模式

hide

——不在空單元格周圍繪制邊框

show

——預設值。在空單元格周圍繪制邊框

table-layout

設定顯示單元,行和列的規則

automatic

——預設值。列寬度由單元格内容設定

fixed

——規定由表格寬度,列寬度等規定設定