표(table)
1. table 태그
표를 만들 때 쓰는 태그이고, 표를 이루는 다른 태그를 모두 감싸는 바깥 틀입니다.
<table></table>

그림에서 표 위의 제목이 <caption>, 맨 윗줄(머리글 행)이 <thead>, 가운데 데이터 줄들이 <tbody>, 맨 아래 합계 줄이 <tfoot>입니다. 줄 하나는 <tr>이고, 줄 안의 칸 하나는 <td>(머리글 칸이면 <th>)입니다. 위의 트리 그림은 이 포함 관계를 글자로 옮긴 것입니다.
1.1 table 사전실습
아래 그림처럼 <h1> 아래에 학생 성적표를 만드는 것이 목표입니다. 아래 코드를 index.html로 저장하고 <h1> 아래에 표를 써 넣으면 됩니다. <style>은 9장에서 배우니 지금은 복사만 해 두세요. 표에 쓰는 태그(tr·td·th·caption·thead·tbody·tfoot)는 이 절 아래에서 하나씩 배우니, 끝까지 읽은 뒤 이 자리로 돌아와 채워 보세요. 답은 5번 항목 끝에 접어 두었습니다.

<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>HTML Table 태그 예제</title>
<style>
table {
width: 100%;
border-collapse: collapse;
margin-bottom: 20px;
}
th,
td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
caption {
caption-side: top;
font-weight: bold;
font-size: 1.2em;
margin-bottom: 10px;
}
</style>
</head>
<body>
<h1>HTML Table 태그 예제</h1>
</body>
</html>
2. tr, td 태그
| 태그 | 설명 |
|---|---|
<tr> | table row. 표의 한 줄(행) |
<td> | table data. 줄 안의 칸(셀) 하나 |
가장 작은 표는 <tr> 안에 <td>를 칸 수만큼 넣은 것입니다. 아래는 2줄 3칸 표입니다.
<table>
<tr>
<td>라이캣</td>
<td>100</td>
<td>90</td>
</tr>
<tr>
<td>로지</td>
<td>80</td>
<td>90</td>
</tr>
</table>
table {
border-collapse: collapse;
}
td {
border: 1px solid #999;
padding: 8px 16px;
}
표는 기본으로 선이 없어서 CSS로 border를 줘야 칸이 보입니다. border-collapse: collapse가 없으면 칸마다 테두리가 따로 그려져 선이 이중으로 보입니다. CSS는 9장 이후에 배우니 이 장의 css 블록은 복사해서 쓰고, 지금은 HTML 태그가 만드는 구조만 보면 됩니다.
3. th 태그 및 속성들
<th>는 table header로, 표의 행 또는 열의 제목을 나타내는 칸입니다.
3.1 scope
scope속성은th태그에 사용하여 헤더가 어떤 칸에 대한 제목인지 명확하게 지정합니다.- 이를 통해 접근성 도구(스크린리더 등)가 표의 구조를 더 잘 이해할 수 있게 합니다.
- 쉽게 말해 행의 헤더인지, 열의 헤더인지를 나타내기 위함입니다.
- 행(row) 또는 열(col), 행그룹(rowgroup), 열그룹(colgroup)의 속성값으로 칸의 범위를 지정합니다
scope="col": 열 헤더를 정의합니다.scope="row": 행 헤더를 정의합니다.scope="rowgroup": 행 그룹 헤더를 정의합니다.<tbody>같은 행 묶음(5번 항목)의 제목에 씁니다.scope="colgroup": 열 그룹 헤더를 정의합니다.<colgroup>으로 묶은 열(6번 항목)의 제목에 씁니다.
rowgroup과 colgroup은 행 묶음·열 묶음을 만든 표에서만 뜻이 있습니다. 아래 예제는 그 두 묶음(<tbody> 둘, <colgroup> 하나)을 이미 쓰고 있으니, 5·6번 항목을 읽은 뒤 다시 와서 보면 이해가 쉽습니다.
<table>
<colgroup>
<col span="4" />
</colgroup>
<thead>
<tr>
<th scope="colgroup" colspan="4">강사 개인정보</th>
</tr>
<tr>
<th scope="col">강의 시수 구분</th>
<th scope="col">이름</th>
<th scope="col">나이</th>
<th scope="col">거주지</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="rowgroup" rowspan="2">보조강사</th>
<th scope="row">Rosy</th>
<td>52</td>
<td>브리즈번</td>
</tr>
<tr>
<th scope="row">ZeeZee</th>
<td>18</td>
<td>제주</td>
</tr>
</tbody>
<tbody>
<tr>
<th scope="rowgroup" rowspan="2">주강사</th>
<th scope="row">Licat</th>
<td>35</td>
<td>도쿄</td>
</tr>
<tr>
<th scope="row">Wade</th>
<td>28</td>
<td>파리</td>
</tr>
</tbody>
</table>
table {
width: 100%;
border-collapse: collapse;
margin-bottom: 20px;
font-family: Arial, sans-serif;
}
th,
td {
border: 1px solid #ddd;
padding: 10px;
text-align: center;
}
th {
background-color: #f4f4f4;
}
caption {
caption-side: top;
font-weight: bold;
font-size: 1.5em;
margin-bottom: 10px;
}
3.2 colspan, rowspan
- 칸 병합 속성입니다. 한 칸이 몇 칸 너비(
colspan) 또는 몇 줄 높이(rowspan)를 차지할지 정합니다. colspan="2": 오른쪽으로 2칸을 합칩니다.rowspan="2": 아래로 2줄을 합칩니다.
가장 흔한 실수는 합친 만큼 다른 칸을 줄이지 않는 것입니다. 아래 표에서 B는 colspan="2"로 2칸 너비를, A는 rowspan="2"로 2줄 높이를 차지합니다. A가 둘째 줄의 첫 자리까지 이미 차지하고 있으니, 둘째 줄은 그 자리를 빼고 C와 D 두 칸만 적습니다. 모든 줄이 차지하는 칸 수(합쳐진 칸은 합친 만큼으로 셉니다)가 같아야 표가 밀리지 않습니다.
<table>
<tr>
<td rowspan="2" class="a">A (2줄)</td>
<td colspan="2" class="b">B (2칸)</td>
</tr>
<tr>
<!-- 첫 자리는 위의 A가 차지했으므로 이 줄은 칸 2개만 씁니다 -->
<td>C</td>
<td>D</td>
</tr>
</table>
table {
border-collapse: collapse;
}
td {
border: 1px solid #999;
padding: 10px 20px;
text-align: center;
}
.a {
background: #ffe9a8;
}
.b {
background: #cfe6ff;
}
C 앞에 <td></td>를 하나 더 써 넣으면 표가 오른쪽으로 한 칸 삐져나옵니다. 직접 써 보며 확인하세요. 다음 해보기(07-2)가 이 규칙을 그대로 씁니다.
4. caption 태그
<table>
<caption>
2월 판매 기록
</caption>
<tr>
<td>사과</td>
<td>12개</td>
</tr>
<tr>
<td>배</td>
<td>7개</td>
</tr>
</table>
table {
border-collapse: collapse;
}
td {
border: 1px solid #999;
padding: 8px 16px;
}
caption {
caption-side: top;
font-weight: bold;
margin-bottom: 6px;
}
caption-side를 bottom으로 바꿔 제목이 아래로 가는지 확인해 보세요.
- 표의 제목이나 설명을 의미합니다.
- table의 첫 번째 자식으로 사용해야 합니다.
- 선택적으로 사용하면 됩니다. 필수 요소는 아닙니다
- 표의 목적에 대한 명확하고 상세한 설명을 포함하는
<caption>요소를 제공하여 사용자가 표 콘텐츠를 확인할지, 넘어갈지 결정할 때 도움을 줄 수 있습니다. caption-side: top, bottom으로 위치를 설정할 수 있습니다.
5. thead, tbody, tfoot 태그
- 표의 머리글, 본문, 바닥글을 의미.
- 줄(
<tr>)들을 구역으로 묶는 요소로 사용. - 선택적으로 사용하면 됩니다. 필수 요소는 아닙니다. 코드의 가독성을 위해 명시적으로 사용하면 좋습니다
<thead>: 머리글 줄들(각 열의 제목이 든 줄)을 묶습니다.<tbody>: 본문 데이터 줄들을 묶습니다. 한 표에 여러 개 둘 수 있습니다.<tfoot>: 합계·요약 줄들을 묶습니다. 표 안에서는 본문 줄들 뒤에 쓰며, 화면에서도 맨 아래에 나옵니다.
<tbody>를 쓰지 않아도 브라우저가 <tr>들을 감싸는 <tbody>를 알아서 만들어 넣습니다. 개발자 도구 Elements 탭에서 확인할 수 있고, CSS에서 table > tr이 안 먹는 이유도 이것입니다.
사전실습(1.1)의 답입니다. 먼저 직접 써 본 뒤 열어 보세요.
1.1 사전실습 답 보기
<h1>HTML Table 태그 예제</h1>
<table>
<caption>
학생 성적표
</caption>
<thead>
<tr>
<th>학생</th>
<th>영어</th>
<th>수학</th>
</tr>
</thead>
<tbody>
<tr>
<td>라이캣</td>
<td>100</td>
<td>90</td>
</tr>
<tr>
<td>로지</td>
<td>80</td>
<td>90</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>평균</td>
<td>90</td>
<td>90</td>
</tr>
</tfoot>
</table>
table {
width: 100%;
border-collapse: collapse;
margin-bottom: 20px;
}
th,
td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
caption {
caption-side: top;
font-weight: bold;
font-size: 1.2em;
margin-bottom: 10px;
}
6. colgroup과 col 태그
표 열 단위 스타일 지정을 위한 태그입니다. 아래 예제의 <colgroup>은 주석 처리돼 있어서 처음에는 주말 열에 색이 없습니다. <!--와 -->를 지워 주석을 풀어 보세요. 일요일·토요일 열 전체에 색이 칠해집니다.
<h1>2024년 1월 달력</h1>
<table>
<caption>
2024년 1월
</caption>
<!-- <colgroup>
<col class="weekend" />
<col span="5" />
<col class="weekend" />
</colgroup> -->
<thead>
<tr>
<th scope="col">일</th>
<th scope="col">월</th>
<th scope="col">화</th>
<th scope="col">수</th>
<th scope="col">목</th>
<th scope="col">금</th>
<th scope="col">토</th>
</tr>
</thead>
<tbody>
<tr>
<td> </td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
<tr>
<td>7</td>
<td>8</td>
<td>9</td>
<td>10</td>
<td>11</td>
<td>12</td>
<td>13</td>
</tr>
<tr>
<td>14</td>
<td>15</td>
<td>16</td>
<td>17</td>
<td>18</td>
<td>19</td>
<td>20</td>
</tr>
<tr>
<td>21</td>
<td>22</td>
<td>23</td>
<td>24</td>
<td>25</td>
<td>26</td>
<td>27</td>
</tr>
<tr>
<td>28</td>
<td>29</td>
<td>30</td>
<td>31</td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</tbody>
</table>
table {
width: 100%;
border-collapse: collapse;
margin-bottom: 20px;
font-family: Arial, sans-serif;
}
th,
td {
border: 1px solid #ddd;
padding: 10px;
text-align: center;
}
th {
background-color: #f4f4f4;
}
caption {
caption-side: top;
font-weight: bold;
font-size: 1.5em;
margin-bottom: 10px;
}
.weekend {
background-color: #fde3e3;
}
6.1 colgroup 태그
- 하나 이상의 표 열들의 그룹을 만들고 한꺼번에 스타일을 지정할 때 사용합니다.
<col>태그를 자식태그로 가집니다.
6.2 col 태그
- 표 열을 하나 이상 묶을 때 사용합니다.
- CSS와 함께 col에 스타일을 지정할 수 있습니다. 다만
col에는background·border·width·visibility정도만 적용되고,text-align같은 글자 속성은 적용되지 않습니다. span="n"속성 : 해당 태그의 스타일이 적용되는 열의 개수
<colgroup>·<col> 태그와 scope="colgroup" 속성은 이름만 비슷합니다. <colgroup>은 열을 묶어 스타일을 주는 태그이고, scope="colgroup"은 <th>가 어느 칸들의 제목인지 알리는 속성입니다.
<colgroup> 과 <col>은 있는데, <row>와 <rowgroup>은 없는 이유?
HTML 표는 줄(<tr>) 단위로 쓰기 때문에, 열은 줄 안의 칸을 세로로 보아야만 나타납니다. 그래서 열 쪽에는 <col>·<colgroup>이라는 별도 태그가 필요합니다. 줄 쪽에는 이미 <tr>이 있고, 줄을 묶는 태그가 <thead>·<tbody>·<tfoot>입니다. 그래서 <row>·<rowgroup>이라는 이름의 태그는 따로 없습니다.
7. 표 접근성 높이기
- caption으로 제목을 제공하여 표가 무엇에 관한 내용인지 스크린리더 사용자가 바로 알 수 있게 해줍니다.
- th 요소와 scope 속성으로 칸의 내용과 제목 칸의 관계를 지정할 수 있습니다.
- 적절한 표 구조를 사용합니다. (thead, tbody, tfoot)
7.1 id - headers
<table>
<caption>
서울/제주 월별 날씨
</caption>
<tr>
<th id="city" rowspan="2">도시</th>
<th id="jan" colspan="2">1월</th>
<th id="feb" colspan="2">2월</th>
</tr>
<tr>
<th id="jan-temp">온도</th>
<th id="jan-humid">습도</th>
<th id="feb-temp">온도</th>
<th id="feb-humid">습도</th>
</tr>
<tr>
<th id="seoul">서울</th>
<td headers="seoul jan jan-temp">-2°C</td>
<td headers="seoul jan jan-humid">45%</td>
<td headers="seoul feb feb-temp">3°C</td>
<td headers="seoul feb feb-humid">50%</td>
</tr>
<tr>
<th id="jeju">제주</th>
<td headers="jeju jan jan-temp">5°C</td>
<td headers="jeju jan jan-humid">70%</td>
<td headers="jeju feb feb-temp">8°C</td>
<td headers="jeju feb feb-humid">65%</td>
</tr>
</table>
table {
width: 100%;
border-collapse: collapse;
margin-bottom: 20px;
font-family: Arial, sans-serif;
}
th,
td {
border: 1px solid #ddd;
padding: 10px;
text-align: center;
}
th {
background-color: #f4f4f4;
}
caption {
caption-side: top;
font-weight: bold;
font-size: 1.5em;
margin-bottom: 10px;
}
머리글이 한 겹인 단순한 표는 scope로 충분하고, 위 예제처럼 머리글이 여러 겹(도시·월·온도/습도)이라 scope로 관계를 나타내기 어려울 때 id와 headers로 칸마다 제목을 직접 이어 줍니다.
빈 칸이 생기지 않게 표를 설계하는 것이 먼저입니다. 위 예제도 '도시' 칸을 rowspan="2"로 두 줄에 걸쳐 빈 머리글 칸을 없앴습니다. 데이터 칸이 정말 비어 있다면 -나 해당 없음처럼 글자로 적어 두세요. 스크린리더가 빈 칸을 읽을 때 뜻을 알 수 없기 때문입니다.
표 예제 살펴보기
OP.GG링크를 열고 개발자 도구(F12)로 표의 글자를 눌러 <thead>·<th>·<tbody>가 어디에 쓰였는지 찾아보세요.
마지막 수정