HTML Introduction | Basic HTML Elements | HTML Tables
A table is a structure that organizes and displays several types of data in an easy-to-read form. In HTML, you can create these tables with the <table> tag.
The <table> tag consists of the following tags.
- The
<tr>tag separates rows in a table. - The
<th>tag represents the title of each column, and all content is automatically bold and centered. - The
<td>tag divides table rows into individual cells.
<table>
<tr>
<th>분류</th>
<th>항목</th>
</tr>
<tr>
<td>과일</td>
<td>사과</td>
</tr>
<tr>
<td>채소</td>
<td>당근</td>
</tr>
</table>
Table style
You can use the CSS border property to display borders on a table. If you do not explicitly specify a border property value, that table always displays an empty border.
<style>
table, th, td { border: 1px solid black }
</style>
In the example above, the table border appears as double lines because the <table>, <th>, and <td> tags each have their own border.
To make a border displayed as two lines appear as a single line, use the border-collapse property. If you set the border-collapse property value to collapse, you can display the table border as one line.
<style>
table, th, td { border: 1px solid black; border-collapse: collapse }
</style>
Merging table columns
You can merge table columns by using the colspan attribute.
<table>
<tr>
<th>분류</th>
<th colspan="2">항목</th>
</tr>
<tr>
<td>과일</td>
<td>사과</td>
<td>배</td>
</tr>
<tr>
<td>채소</td>
<td>당근</td>
<td>감자</td>
</tr>
</table>
Merging table rows
You can merge table rows by using the rowspan attribute.
<table>
<tr>
<th>분류</th>
<th>항목</th>
</tr>
<tr>
<td rowspan="2">과일</td>
<td>사과</td>
</tr>
<tr>
<td>배</td>
</tr>
<tr>
<td>채소</td>
<td>감자</td>
</tr>
</table>
Merging table columns and rows
By using the colspan and rowspan attributes together, you can express more complex tables.
<table>
<tr>
<th>분류</th>
<th colspan="2">항목</th>
</tr>
<tr>
<td rowspan="2">과일</td>
<td>사과</td>
<td>배</td>
</tr>
<tr>
<td>포도</td>
<td rowspan="2">토마토 감자</td>
</tr>
<tr>
<td>채소</td>
<td>당근</td>
</tr>
</table>
Setting a table caption
You can use the <caption> tag to attach a title or short description to the top of a table.
<table style="width:100%">
<caption>장바구니</caption>
<tr>
<th>분류</th>
<th>항목</th>
</tr>
<tr>
<td>과일</td>
<td>사과</td>
</tr>
<tr>
<td>채소</td>
<td>당근</td>
</tr>
</table>