CodeGym /ํ–‰๋™ /Frontend SELF KO /๋ฐ˜์‘ํ˜• ํ…Œ์ด๋ธ”

๋ฐ˜์‘ํ˜• ํ…Œ์ด๋ธ”

Frontend SELF KO
๋ ˆ๋ฒจ 26 , ๋ ˆ์Šจ 3
์‚ฌ์šฉ ๊ฐ€๋Šฅ

9.1 ์Šคํฌ๋กค

ํ…Œ์ด๋ธ”์€ ๊ตฌ์กฐํ™”๋œ ๋ฐ์ดํ„ฐ๋ฅผ ํ‘œ์‹œํ•˜๋Š” ๋ฐ ์ž์ฃผ ์‚ฌ์šฉ๋˜์ง€๋งŒ, ์ž‘์€ ํ™”๋ฉด์˜ ๊ธฐ๊ธฐ์—์„œ๋Š” ํ‘œ์‹œํ•˜๊ธฐ ์–ด๋ ค์šธ ์ˆ˜ ์žˆ์–ด. ๋ฐ˜์‘ํ˜• ํ…Œ์ด๋ธ”์€ ๋ชจ๋ฐ”์ผ ํฐ๊ณผ ํƒœ๋ธ”๋ฆฟ์„ ํฌํ•จํ•œ ๋‹ค์–‘ํ•œ ๊ธฐ๊ธฐ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋” ์ž˜ ๋ณด๊ณ  ์ƒํ˜ธ์ž‘์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค˜. ๋ฐ˜์‘ํ˜• ํ…Œ์ด๋ธ”์„ ๋งŒ๋“œ๋Š” ๋ช‡ ๊ฐ€์ง€ ๊ธฐ์ˆ ์„ ์‚ดํŽด๋ณด์ž.

๋ฐ˜์‘ํ˜• ํ…Œ์ด๋ธ”์„ ๋งŒ๋“œ๋Š” ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜๋Š” ์ˆ˜ํ‰ ์Šคํฌ๋กค ์ถ”๊ฐ€ํ•˜๊ธฐ์•ผ. ์ด ๋ฐฉ๋ฒ•์€ ํ…Œ์ด๋ธ” ๊ตฌ์กฐ๋ฅผ ์œ ์ง€ํ•˜๊ณ  ์ข์€ ํ™”๋ฉด์—์„œ ์‚ฌ์šฉ์ž๊ฐ€ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜ํ‰์œผ๋กœ ์Šคํฌ๋กคํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค˜.

์˜ˆ์‹œ:

CSS
    
      .table-container {
        overflow-x: auto;
        width: 100%;
      }

      table {
        width: 100%;
        border-collapse: collapse;
      }

      th, td {
        border: 1px solid #ccc;
        padding: 8px;
        text-align: left;
      }
    
  
HTML
    
      <!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 ์—ด ์ˆจ๊น€

์ด ๋ฐฉ๋ฒ•์€ ์ข์€ ํ™”๋ฉด์—์„œ ๋œ ์ค‘์š”ํ•œ ์—ด์„ ์ˆจ๊ฒจ ์ฃผ์š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด์ด๊ฒŒ ํ•ด์ฃผ๋Š” ๊ฑฐ์•ผ. ์ด๊ฑธ ๋ฏธ๋””์–ด ์ฟผ๋ฆฌ๋กœ ํ•  ์ˆ˜ ์žˆ์–ด.

์˜ˆ์‹œ:

CSS
    
      @media (max-width: 600px) {
        .hide {
          display: none;
        }
      }

      table {
        width: 100%;
        border-collapse: collapse;
      }

      th, td {
        border: 1px solid #ccc;
        padding: 8px;
        text-align: left;
      }
    
  
HTML
    
      <!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. ๋ธ”๋ก์œผ๋กœ ์Œ“๊ธฐ

๋ธ”๋ก์œผ๋กœ ์Œ“๊ธฐ๋Š” ํ…Œ์ด๋ธ” ํ–‰์„ ์„ธ๋กœ๋กœ ๋ธ”๋ก ํ˜•ํƒœ๋กœ ๋ฐ”๊ฟ”์„œ ์ข์€ ํ™”๋ฉด์—์„œ ์•„๋ž˜๋กœ ์Œ“์ด๊ฒŒ ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด์•ผ. ๋ชจ๋ฐ”์ผ ๊ธฐ๊ธฐ์—์„œ ํ…Œ์ด๋ธ”์„ ๋” ์ฝ๊ธฐ ์‰ฝ๊ฒŒ ํ•ด์ค˜.

์˜ˆ์‹œ:

CSS
    
      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;
        }
      }
    
  
HTML
    
      <!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. ์นด๋“œ ๋ ˆ์ด์•„์›ƒ์œผ๋กœ ๋ณ€ํ™˜

์ด ๋ฐฉ๋ฒ•์€ ํ…Œ์ด๋ธ”์˜ ๊ฐ ํ–‰์„ ์ข์€ ํ™”๋ฉด์—์„œ ๊ฐœ๋ณ„ ์นด๋“œ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฑฐ์•ผ. ๋ชจ๋ฐ”์ผ ๊ธฐ๊ธฐ์—์„œ ๋ฐ์ดํ„ฐ ์ฝ๊ธฐ ์‰ฝ๊ฒŒ ํ•ด์ค˜.

์˜ˆ์‹œ:

CSS
    
      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;
        }
      }
    
  
HTML
    
      <!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 ์‚ฌ์šฉ ์˜ˆ์‹œ:

HTML
    
      <!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 ํด๋ž˜์Šค์•ผ
์ฝ”๋ฉ˜ํŠธ
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION