9.1 ์คํฌ๋กค
ํ ์ด๋ธ์ ๊ตฌ์กฐํ๋ ๋ฐ์ดํฐ๋ฅผ ํ์ํ๋ ๋ฐ ์์ฃผ ์ฌ์ฉ๋์ง๋ง, ์์ ํ๋ฉด์ ๊ธฐ๊ธฐ์์๋ ํ์ํ๊ธฐ ์ด๋ ค์ธ ์ ์์ด. ๋ฐ์ํ ํ ์ด๋ธ์ ๋ชจ๋ฐ์ผ ํฐ๊ณผ ํ๋ธ๋ฆฟ์ ํฌํจํ ๋ค์ํ ๊ธฐ๊ธฐ์์ ๋ฐ์ดํฐ๋ฅผ ๋ ์ ๋ณด๊ณ ์ํธ์์ฉํ ์ ์๊ฒ ํด์ค. ๋ฐ์ํ ํ ์ด๋ธ์ ๋ง๋๋ ๋ช ๊ฐ์ง ๊ธฐ์ ์ ์ดํด๋ณด์.
๋ฐ์ํ ํ ์ด๋ธ์ ๋ง๋๋ ๊ฐ๋จํ ๋ฐฉ๋ฒ ์ค ํ๋๋ ์ํ ์คํฌ๋กค ์ถ๊ฐํ๊ธฐ์ผ. ์ด ๋ฐฉ๋ฒ์ ํ ์ด๋ธ ๊ตฌ์กฐ๋ฅผ ์ ์งํ๊ณ ์ข์ ํ๋ฉด์์ ์ฌ์ฉ์๊ฐ ๋ฐ์ดํฐ๋ฅผ ์ํ์ผ๋ก ์คํฌ๋กคํ ์ ์๊ฒ ํด์ค.
์์:
.table-container {
overflow-x: auto;
width: 100%;
}
table {
width: 100%;
border-collapse: collapse;
}
th, td {
border: 1px solid #ccc;
padding: 8px;
text-align: left;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>์คํฌ๋กค์ด ์๋ ๋ฐ์ํ ํ
์ด๋ธ</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="table-container">
<table>
<thead>
<tr>
<th>์ด 1</th>
<th>์ด 2</th>
<th>์ด 3</th>
<th>์ด 4</th>
</tr>
</thead>
<tbody>
<tr>
<td>๋ฐ์ดํฐ 1</td>
<td>๋ฐ์ดํฐ 2</td>
<td>๋ฐ์ดํฐ 3</td>
<td>๋ฐ์ดํฐ 4</td>
</tr>
<!-- ๋ค๋ฅธ ํ๋ค -->
</tbody>
</table>
</div>
</body>
</html>
์ฝ๋ ์ค๋ช :
.table-container
: ํ ์ด๋ธ์ด ์ปจํ ์ด๋์ ๋๋น๋ฅผ ์ด๊ณผํ ๊ฒฝ์ฐ ์ํ ์คํฌ๋กค์ ์ถ๊ฐํด์คtable
: ํ ์ด๋ธ์ ๋๋น๋ฅผ 100%๋ก ์ค์ ํ๊ณ ์ ์ ๊ฒฝ๊ณ๋ฅผ ๊ฒฐํฉํด์ค
9.2 ์ด ์จ๊น
์ด ๋ฐฉ๋ฒ์ ์ข์ ํ๋ฉด์์ ๋ ์ค์ํ ์ด์ ์จ๊ฒจ ์ฃผ์ ๋ฐ์ดํฐ๋ฅผ ๋ณด์ด๊ฒ ํด์ฃผ๋ ๊ฑฐ์ผ. ์ด๊ฑธ ๋ฏธ๋์ด ์ฟผ๋ฆฌ๋ก ํ ์ ์์ด.
์์:
@media (max-width: 600px) {
.hide {
display: none;
}
}
table {
width: 100%;
border-collapse: collapse;
}
th, td {
border: 1px solid #ccc;
padding: 8px;
text-align: left;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>์ด ์จ๊น์ด ์๋ ๋ฐ์ํ ํ
์ด๋ธ</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<table>
<thead>
<tr>
<th>์ด 1</th>
<th>์ด 2</th>
<th class="hide">์ด 3</th>
<th class="hide">์ด 4</th>
</tr>
</thead>
<tbody>
<tr>
<td>๋ฐ์ดํฐ 1</td>
<td>๋ฐ์ดํฐ 2</td>
<td class="hide">๋ฐ์ดํฐ 3</td>
<td class="hide">๋ฐ์ดํฐ 4</td>
</tr>
<!-- ๋ค๋ฅธ ํ๋ค -->
</tbody>
</table>
</body>
</html>
์ฝ๋ ์ค๋ช :
.hide
: ๋ฏธ๋์ด ์ฟผ๋ฆฌ๋ฅผ ์ฌ์ฉํด ๋๋น 600px ๋ฏธ๋ง์ ํ๋ฉด์์ ํน์ ์ด์ ์จ๊ฒจ์คtable
: ํ ์ด๋ธ์ ๋๋น๋ฅผ 100%๋ก ์ค์ ํ๊ณ ์ ์ ๊ฒฝ๊ณ๋ฅผ ๊ฒฐํฉํด์ค
9.3. ๋ธ๋ก์ผ๋ก ์๊ธฐ
๋ธ๋ก์ผ๋ก ์๊ธฐ๋ ํ ์ด๋ธ ํ์ ์ธ๋ก๋ก ๋ธ๋ก ํํ๋ก ๋ฐ๊ฟ์ ์ข์ ํ๋ฉด์์ ์๋๋ก ์์ด๊ฒ ํ๋ ๋ฐฉ๋ฒ์ด์ผ. ๋ชจ๋ฐ์ผ ๊ธฐ๊ธฐ์์ ํ ์ด๋ธ์ ๋ ์ฝ๊ธฐ ์ฝ๊ฒ ํด์ค.
์์:
table {
width: 100%;
border-collapse: collapse;
}
th, td {
border: 1px solid #ccc;
padding: 8px;
text-align: left;
}
@media (max-width: 600px) {
table, thead, tbody, th, td, tr {
display: block;
}
th {
display: none;
}
td {
display: flex;
justify-content: space-between;
padding: 10px;
border-bottom: 1px solid #ccc;
}
td::before {
content: attr(data-label);
font-weight: bold;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>๋ธ๋ก์ผ๋ก ์๋ ๋ฐ์ํ ํ
์ด๋ธ</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<table>
<thead>
<tr>
<th>์ด 1</th>
<th>์ด 2</th>
<th>์ด 3</th>
<th>์ด 4</th>
</tr>
</thead>
<tbody>
<tr>
<td data-label="์ด 1">๋ฐ์ดํฐ 1</td>
<td data-label="์ด 2">๋ฐ์ดํฐ 2</td>
<td data-label="์ด 3">๋ฐ์ดํฐ 3</td>
<td data-label="์ด 4">๋ฐ์ดํฐ 4</td>
</tr>
<!-- ๋ค๋ฅธ ํ๋ค -->
</tbody>
</table>
</body>
</html>
์ฝ๋ ์ค๋ช :
- ๋ฏธ๋์ด ์ฟผ๋ฆฌ: ๋๋น 600px ๋ฏธ๋ง์ ํ๋ฉด์์ ํ ์ด๋ธ์ ๋ธ๋ก ์์๋ก ๋ณํํด
td::before
: ๋ฐ์ดํฐ ์ปจํ ์คํธ๋ฅผ ์ ์งํ๊ธฐ ์ํด ์ ๊ฐ ์์ ์ด ์ด๋ฆ์ ๊ฐ์ง ๊ฐ์ ์์๋ฅผ ์ถ๊ฐํด์ค
9.4. ์นด๋ ๋ ์ด์์์ผ๋ก ๋ณํ
์ด ๋ฐฉ๋ฒ์ ํ ์ด๋ธ์ ๊ฐ ํ์ ์ข์ ํ๋ฉด์์ ๊ฐ๋ณ ์นด๋๋ก ๋ณํํ๋ ๊ฑฐ์ผ. ๋ชจ๋ฐ์ผ ๊ธฐ๊ธฐ์์ ๋ฐ์ดํฐ ์ฝ๊ธฐ ์ฝ๊ฒ ํด์ค.
์์:
table {
width: 100%;
border-collapse: collapse;
}
th, td {
border: 1px solid #ccc;
padding: 8px;
text-align: left;
}
@media (max-width: 600px) {
table, thead, tbody, th, td, tr {
display: block;
}
th {
display: none;
}
tr {
margin-bottom: 10px;
border-bottom: 2px solid #ccc;
padding-bottom: 10px;
}
td {
display: flex;
justify-content: space-between;
padding: 10px 0;
border: none;
}
td::before {
content: attr(data-label);
font-weight: bold;
margin-right: 10px;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>์นด๋ ๋ ์ด์์์ด ์๋ ๋ฐ์ํ ํ
์ด๋ธ</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<table>
<thead>
<tr>
<th>์ด 1</th>
<th>์ด 2</th>
<th>์ด 3</th>
<th>์ด 4</th>
</tr>
</thead>
<tbody>
<tr>
<td data-label="์ด 1">๋ฐ์ดํฐ 1</td>
<td data-label="์ด 2">๋ฐ์ดํฐ 2</td>
<td data-label="์ด 3">๋ฐ์ดํฐ 3</td>
<td data-label="์ด 4">๋ฐ์ดํฐ 4</td>
</tr>
<!-- ๋ค๋ฅธ ํ๋ค -->
</tbody>
</table>
</body>
</html>
์ฝ๋ ์ค๋ช :
- ๋ฏธ๋์ด ์ฟผ๋ฆฌ: ๋๋น 600px ๋ฏธ๋ง์ ํ๋ฉด์์ ํ ์ด๋ธ์ ๋ธ๋ก ์์๋ก ๋ณํํ๊ณ ์นด๋ ํํ๋ก ๋ง๋ค์ด์ค
td::before
: ๋ฐ์ดํฐ ์ปจํ ์คํธ๋ฅผ ์ ์งํ๊ธฐ ์ํด ์ ๊ฐ ์์ ์ด ์ด๋ฆ์ ๊ฐ์ง ๊ฐ์ ์์๋ฅผ ์ถ๊ฐํด์ค
9.5 CSS Frameworks ์ฌ์ฉํ๊ธฐ
Bootstrap๊ณผ ๊ฐ์ ๋ง์ CSS ํ๋ ์์ํฌ๋ ๋ฐ์ํ ํ ์ด๋ธ์ ๋ง๋๋ ๋ฐ ํ์ํ ๋ด์ฅ ์๋ฃจ์ ์ ์ ๊ณตํด. ์ด๋ฐ ํ๋ ์์ํฌ๋ฅผ ์ฌ์ฉํ๋ฉด ๊ณผ์ ์ ๋จ์ํํ๊ณ ์ค๋น๋ ์คํ์ผ๊ณผ ๊ตฌ์ฑ ์์๋ฅผ ์ ๊ณตํด์ค.
Bootstrap ์ฌ์ฉ ์์:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Bootstrap๊ณผ ํจ๊ปํ๋ ๋ฐ์ํ ํ
์ด๋ธ</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<table class="table table-responsive">
<thead>
<tr>
<th>์ด 1</th>
<th>์ด 2</th>
<th>์ด 3</th>
<th>์ด 4</th>
</tr>
</thead>
<tbody>
<tr>
<td>๋ฐ์ดํฐ 1</td>
<td>๋ฐ์ดํฐ 2</td>
<td>๋ฐ์ดํฐ 3</td>
<td>๋ฐ์ดํฐ 4</td>
</tr>
<!-- ๋ค๋ฅธ ํ๋ค -->
</tbody>
</table>
</div>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.5.2/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</body>
</html>
์ฝ๋ ์ค๋ช :
table.table-responsive
: ์ข์ ํ๋ฉด์์ ํ ์ด๋ธ์ ์ํ ์คํฌ๋กค์ ์ถ๊ฐํ๋ Bootstrap ํด๋์ค์ผcontainer
: ์ปจํ ์ด๋์ ์ต๋ ๋๋น๋ฅผ ์ค์ ํ๊ณ ์ค์์ ๋ฐฐ์นํด์ฃผ๋ Bootstrap ํด๋์ค์ผ
GO TO FULL VERSION