คุณสมบัติ CSS ที่เกี่ยวข้องกับตาราง
บทความนี้อธิบายคุณสมบัติ CSS ที่เกี่ยวข้องกับตาราง
คุณสามารถเรียนรู้วิธีใช้และเขียนคุณสมบัติสำหรับรูปแบบตาราง, เส้นขอบ, ตำแหน่งคำบรรยาย และอื่นๆ
YouTube Video
HTML สำหรับการแสดงตัวอย่าง
css-table.html
1<!DOCTYPE html>
2<html lang="en">
3<head>
4 <meta charset="UTF-8">
5 <meta name="viewport" content="width=device-width, initial-scale=1.0">
6 <title>CSS Properties Example</title>
7 <link rel="stylesheet" href="css-base.css">
8 <link rel="stylesheet" href="css-table.css">
9</head>
10<body>
11 <!-- Header -->
12 <header>
13 <h1>Table Related CSS Properties</h1>
14 </header>
15
16 <!-- Main content -->
17 <main>
18 <header>
19 <h2>Table Related Properties</h2>
20 </header>
21 <article>
22 <h3>table-layout</h3>
23 <section>
24 <header><h4>table-layout: auto</h4></header>
25 <section class="sample-view">
26 <table class="table-layout-auto">
27 <thead>
28 <tr>
29 <th>Column 1</th>
30 <th>Column 2</th>
31 <th>Column 3</th>
32 </tr>
33 </thead>
34 <tbody>
35 <tr>
36 <td>Short</td>
37 <td>Some longer content here</td>
38 <td>Content</td>
39 </tr>
40 <tr>
41 <td>Another short one</td>
42 <td>This is a much longer piece of text to demonstrate the auto layout</td>
43 <td>Short</td>
44 </tr>
45 </tbody>
46 </table>
47 </section>
48 </section>
49 <section>
50 <header><h4>table-layout: fixed</h4></header>
51 <section class="sample-view">
52 <table class="table-layout-fixed">
53 <thead>
54 <tr>
55 <th>Column 1</th>
56 <th>Column 2</th>
57 <th>Column 3</th>
58 </tr>
59 </thead>
60 <tbody>
61 <tr>
62 <td>Short</td>
63 <td>Some longer content here</td>
64 <td>Content</td>
65 </tr>
66 <tr>
67 <td>Another short one</td>
68 <td>This is a much longer piece of text to demonstrate the fixed layout</td>
69 <td>Short</td>
70 </tr>
71 </tbody>
72 </table>
73 </section>
74 </section>
75 </article>
76 <article>
77 <h3>border-collapse</h3>
78 <section>
79 <header><h4>border-collapse: separate</h4></header>
80 <section class="sample-view">
81 <table class="border-collapse-separate">
82 <thead>
83 <tr>
84 <th>Header 1</th>
85 <th>Header 2</th>
86 <th>Header 3</th>
87 </tr>
88 </thead>
89 <tbody>
90 <tr>
91 <td>Cell 1</td>
92 <td>Cell 2</td>
93 <td>Cell 3</td>
94 </tr>
95 <tr>
96 <td>Cell 4</td>
97 <td>Cell 5</td>
98 <td>Cell 6</td>
99 </tr>
100 </tbody>
101 </table>
102 </section>
103 </section>
104 <section>
105 <header><h4>border-collapse: collapse</h4></header>
106 <section class="sample-view">
107 <table class="border-collapse-collapse">
108 <thead>
109 <tr>
110 <th>Header 1</th>
111 <th>Header 2</th>
112 <th>Header 3</th>
113 </tr>
114 </thead>
115 <tbody>
116 <tr>
117 <td>Cell 1</td>
118 <td>Cell 2</td>
119 <td>Cell 3</td>
120 </tr>
121 <tr>
122 <td>Cell 4</td>
123 <td>Cell 5</td>
124 <td>Cell 6</td>
125 </tr>
126 </tbody>
127 </table>
128 </section>
129 </section>
130 </article>
131 <article>
132 <h3>border-spacing</h3>
133 <section>
134 <header><h4>border-spacing: 5px 20px</h4></header>
135 <section class="sample-view">
136 <table class="border-spacing-example">
137 <thead>
138 <tr>
139 <th>Header 1</th>
140 <th>Header 2</th>
141 <th>Header 3</th>
142 </tr>
143 </thead>
144 <tbody>
145 <tr>
146 <td>Cell 1</td>
147 <td>Cell 2</td>
148 <td>Cell 3</td>
149 </tr>
150 <tr>
151 <td>Cell 4</td>
152 <td>Cell 5</td>
153 <td>Cell 6</td>
154 </tr>
155 </tbody>
156 </table>
157 </section>
158 </section>
159 </article>
160 <article>
161 <h3>caption-side</h3>
162 <section>
163 <header><h4>caption-side: top</h4></header>
164 <section class="sample-view">
165 <table>
166 <caption class="caption-side-top">
167 Table Caption on Top
168 </caption>
169 <thead>
170 <tr>
171 <th>Header 1</th>
172 <th>Header 2</th>
173 <th>Header 3</th>
174 </tr>
175 </thead>
176 <tbody>
177 <tr>
178 <td>Data 1</td>
179 <td>Data 2</td>
180 <td>Data 3</td>
181 </tr>
182 <tr>
183 <td>Data 4</td>
184 <td>Data 5</td>
185 <td>Data 6</td>
186 </tr>
187 </tbody>
188 </table>
189 </section>
190 </section>
191 <section>
192 <header><h4>caption-side: bottom</h4></header>
193 <section class="sample-view">
194 <table>
195 <caption class="caption-side-bottom">
196 Table Caption on Bottom
197 </caption>
198 <thead>
199 <tr>
200 <th>Header 1</th>
201 <th>Header 2</th>
202 <th>Header 3</th>
203 </tr>
204 </thead>
205 <tbody>
206 <tr>
207 <td>Data 1</td>
208 <td>Data 2</td>
209 <td>Data 3</td>
210 </tr>
211 <tr>
212 <td>Data 4</td>
213 <td>Data 5</td>
214 <td>Data 6</td>
215 </tr>
216 </tbody>
217 </table>
218 </section>
219 </section>
220 </article>
221 <article>
222 <h3>empty-cells</h3>
223 <section>
224 <header><h4>empty-cells: show</h4></header>
225 <section class="sample-view">
226 <table class="empty-cells-show">
227 <tr>
228 <th>Header 1</th>
229 <th>Header 2</th>
230 <th>Header 3</th>
231 </tr>
232 <tr>
233 <td>Data 1</td>
234 <td></td> <!-- Empty cell -->
235 <td>Data 3</td>
236 </tr>
237 <tr>
238 <td>Data 4</td>
239 <td></td> <!-- Empty cell -->
240 <td>Data 6</td>
241 </tr>
242 </table>
243 </section>
244 </section>
245 <section>
246 <header><h4>empty-cells: hide</h4></header>
247 <section class="sample-view">
248 <table class="empty-cells-hide">
249 <tr>
250 <th>Header 1</th>
251 <th>Header 2</th>
252 <th>Header 3</th>
253 </tr>
254 <tr>
255 <td>Data 1</td>
256 <td></td> <!-- Empty cell -->
257 <td>Data 3</td>
258 </tr>
259 <tr>
260 <td>Data 4</td>
261 <td></td> <!-- Empty cell -->
262 <td>Data 6</td>
263 </tr>
264 </table>
265 </section>
266 </section>
267 </article>
268 </main>
269</body>
270</html>
รูปแบบตาราง
คุณสมบัติ table-layout
1table {
2 width: 100%;
3}
4
5th, td {
6 border: 1px solid black;
7 padding: 10px;
8}
9
10table.table-layout-auto {
11 border-collapse: collapse;
12 table-layout: auto;
13}
14
15table.table-layout-fixed {
16 border-collapse: collapse;
17 table-layout: fixed;
18}
19
20table.table-layout-fixed th {
21 width: 33.33%; /* Distribute width equally among 3 columns */
22}
table-layout
เป็นคุณสมบัติ CSS ที่ควบคุมวิธีการจัดรูปแบบของตาราง HTML คุณสมบัตินี้ช่วยให้คุณกำหนดวิธีคำนวณความกว้างของคอลัมน์ในตาราง
-
ในคลาส
table-layout-auto
ความกว้างของคอลัมน์ถูกกำหนดตามเนื้อหาในแต่ละเซลล์ -
ความกว้างของคอลัมน์จะแตกต่างกันไปตามเนื้อหา และเซลล์ที่มีเนื้อหายาวจะขยายความกว้างของคอลัมน์
-
คลาส
table-layout-fixed
ใช้table-layout: fixed
-
ความกว้างของแต่ละคอลัมน์ถูกกำหนดให้เท่ากัน และความกว้างจะไม่เปลี่ยนแปลงแม้ว่าเนื้อหาจะยาว
ค่าต่างๆ ของ table-layout
1table {
2 table-layout: auto;
3}
4
5table {
6 table-layout: fixed;
7}
table-layout
มีค่าหลักสองค่า: auto
และ fixed
-
auto
(ค่าเริ่มต้น):- การระบุค่า
auto
จะปรับความกว้างของคอลัมน์ในตารางโดยอัตโนมัติตามเนื้อหา - เบราว์เซอร์จะอ่านเนื้อหาทั้งหมดของแต่ละเซลล์และกำหนดความกว้างของแต่ละคอลัมน์ตามเนื้อหานั้น
- วิธีนี้อาจส่งผลให้การแสดงผลตารางทั้งหมดช้าลง
- การระบุค่า
-
fixed
:- การระบุค่า
fixed
หมายความว่าความกว้างของแต่ละคอลัมน์ถูกกำหนดโดยความกว้างของเซลล์ในแถวแรกหรือโดยแอตทริบิวต์width
- ตารางทั้งหมดจะถูกวาดทันที และความกว้างของคอลัมน์ถูกกำหนดโดยไม่ต้องอ่านเนื้อหาทั้งหมด
- ประสิทธิภาพจะดีขึ้นแม้ในกรณีที่มีข้อมูลจำนวนมากในตาราง
- การระบุค่า
ความแตกต่างระหว่าง table-layout: auto
และ table-layout: fixed
- เมื่อใช้
auto
ความกว้างของตารางจะปรับโดยอัตโนมัติตามเนื้อหา สามารถใช้งานเลย์เอาต์ที่ยืดหยุ่นมากขึ้นได้ แต่ประสิทธิภาพอาจลดลงเมื่อมีการใช้งานตารางที่ซับซ้อน - เมื่อใช้ค่า
fixed
ความกว้างของคอลัมน์จะถูกกำหนดแน่นอน โดยเลย์เอาต์จะคงที่ไม่ว่าข้อมูลในตารางจะเป็นเช่นใด แม้ประสิทธิภาพจะดีขึ้น แต่ข้อมูลอาจถูกตัดออกหากขนาดไม่พอดีกับความกว้าง
คุณสมบัติ border-collapse
1table.border-collapse-separate {
2 border-collapse: separate;
3 border-spacing: 10px; /* Set space between cells */
4}
5
6table.border-collapse-collapse {
7 border-collapse: collapse;
8}
border-collapse
เป็นคุณสมบัติ CSS ที่กำหนดวิธีการแสดงขอบระหว่างเซลล์ในตาราง ใช้เพื่อกำหนดวิธีการแสดงขอบระหว่างเซลล์ในตาราง
-
เมื่อใช้
border-collapse: separate
จะมีขอบที่แยกจากกันและมีช่องว่างระหว่างเซลล์ ช่องว่างระหว่างเซลล์สามารถปรับได้ด้วยคุณสมบัติborder-spacing
ในตัวอย่างนี้ ตั้งค่าช่องว่างระหว่างแต่ละเซลล์ให้เป็น 10px -
เมื่อใช้
border-collapse: collapse
ขอบของเซลล์ที่ติดกันจะรวมกันและแสดงเป็นขอบเดียว ผลที่ได้คือตารางมีการออกแบบที่เป็นอันหนึ่งอันเดียวกันมากขึ้น
ดังตัวอย่างนี้ คุณสมบัติ border-collapse
มีค่าหลักๆ สองค่าได้แก่ separate
และ collapse
-
separate
(ค่าดีฟอลต์):- เมื่อระบุค่า
separate
ขอบของแต่ละเซลล์จะถูกวาดแยกจากกัน และมีช่องว่างระหว่างเซลล์ - ใช้เมื่อคุณต้องการแยกความแตกต่างของขอบอย่างชัดเจน เนื่องจากขอบของเซลล์จะแสดงแยกจากกัน
- สามารถปรับช่องว่างระหว่างเซลล์ได้ด้วยคุณสมบัติ
border-spacing
- เมื่อระบุค่า
-
collapse
:- เมื่อระบุค่า
collapse
ขอบของเซลล์ที่ติดกันจะรวมเป็นขอบเดียวและแสดงผลเช่นนั้น - เนื่องจากขอบมีการทับซ้อนกัน ตารางทั้งหมดจึงมีลักษณะที่เป็นอันหนึ่งอันเดียวกัน
- แถวและคอลัมน์ของตารางจะแสดงอย่างเรียบร้อย เหมาะสำหรับการออกแบบที่เรียบง่ายกว่า
- เมื่อระบุค่า
คุณสมบัติ border-spacing
1table.border-spacing-example {
2 border-collapse: separate;
3
4 /* Set 5px spacing between columns and 20px spacing between rows */
5 border-spacing: 5px 20px;
6}
คุณสมบัติ border-spacing
เป็นคุณสมบัติ CSS สำหรับกำหนดช่องว่างระหว่างเซลล์ในตาราง HTML ช่วยปรับช่องว่างระหว่างเซลล์ในตารางและควบคุมระยะห่างในการแสดงผล ในตัวอย่างนี้ มีการเพิ่มช่องว่างระหว่างเซลล์ในแนวนอนเป็น 5px
และในแนวตั้งเป็น 20px
คุณสมบัติ border-spacing
จะมีผลเฉพาะตารางที่ระบุ border-collapse: separate;
เท่านั้น หากระบุ border-collapse: collapse;
คุณสมบัตินี้จะไม่มีผล
การใช้งาน
ค่าของคุณสมบัติ border-spacing
ถูกกำหนดเป็นพิกเซลหรือหน่วยความยาวอื่น ๆ สามารถกำหนดในสองรูปแบบต่อไปนี้:
- ค่าเดียว
1table {
2 border-spacing: 10px;
3}
- เมื่อระบุตัวเลขค่าเดียว การเว้นระยะห่างระหว่างแถว (ด้านบนและล่าง) และคอลัมน์ (ด้านซ้ายและขวา) จะถูกตั้งค่าเท่ากัน
ในตัวอย่างนี้ มีการเพิ่มระยะห่าง 10px
ระหว่างทุกเซลล์
- สองค่า
1table {
2 border-spacing: 10px 20px;
3}
- เมื่อระบุสองค่า ค่าแรกใช้กำหนดระยะห่างในแนวนอนระหว่างคอลัมน์ และค่าที่สองใช้กำหนดระยะห่างในแนวตั้งระหว่างแถว
ในตัวอย่างนี้ ระยะห่างระหว่างคอลัมน์คือ 10px
และระหว่างแถวคือ 20px
คุณสมบัติ caption-side
1caption.caption-side-top {
2 caption-side: top; /* Display the caption at the top of the table */
3 font-weight: bold;
4 font-size: 1.2em;
5 margin-bottom: 10px;
6 background-color: #ccc;
7}
8
9caption.caption-side-bottom {
10 caption-side: bottom; /* Display the caption at the bottom of the table */
11 font-weight: bold;
12 font-size: 1.2em;
13 margin-top: 10px;
14 background-color: #ccc;
15}
คุณสมบัติ caption-side
เป็นคุณสมบัติ CSS ที่ใช้ระบุตำแหน่งที่จะแสดงคำบรรยาย (<caption>
element) ของตาราง HTML
-
การระบุ
top
จะแสดงคำบรรยายที่ด้านบนของตาราง- นี่คือการตั้งค่าเริ่มต้น และมักใช้คำบรรยายเป็นชื่อหรือคำอธิบายของตาราง
-
การระบุ
bottom
จะแสดงคำบรรยายที่ด้านล่างของตาราง- ใช้สิ่งนี้เมื่อคุณต้องการวางคำอธิบายไว้ใต้ตาราง
คุณสมบัติ empty-cells
1table.empty-cells-show {
2 empty-cells: show; /* Display empty cells */
3}
4
5table.empty-cells-hide {
6 empty-cells: hide; /* Display empty cells */
7}
คุณสมบัติ empty-cells
เป็นคุณสมบัติ CSS ที่ใช้ควบคุมว่าจะแสดงเซลล์ว่างในตาราง HTML หรือไม่ ตามค่าเริ่มต้น เซลล์ว่างในตารางจะแสดงเส้นขอบด้วย แต่คุณสามารถใช้คุณสมบัตินี้เพื่อควบคุมการแสดงผลได้
- การระบุ
show
จะทำให้เซลล์ว่างแสดงผลด้วย นี่คือพฤติกรรมเริ่มต้นที่กำหนดไว้ - การระบุ
hide
จะทำให้เซลล์ว่างมองไม่เห็น เส้นขอบและพื้นหลังของเซลล์จะไม่ถูกแสดง
คุณสามารถติดตามบทความข้างต้นโดยใช้ Visual Studio Code บนช่อง YouTube ของเรา กรุณาตรวจสอบช่อง YouTube ด้วย