본문 바로가기
스터디 열기

<직접 해보기> 레이아웃 만들기

기본적인 웹 서비스의 레이아웃을 만들어 봅시다. flex와 grid 중 하나를 선택하여 레이아웃을 만들어보세요. 레이아웃은 아래와 같이 구성됩니다.

정답이 하나만 있는 것은 아니기 때문에 색, 크기는 자유롭게 작업하세요. 아래는 기본 코드입니다.

<!DOCTYPE html>
<html lang="ko">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Layout</title>
    <style>
      * {
        box-sizing: border-box;
        color: white;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <header>header</header>
      <div class="main">
        <section>section > article</section>
        <aside>aside</aside>
      </div>
      <footer>footer</footer>
    </div>
  </body>
</html>

정답

  1. flex만 사용해보기
<!DOCTYPE html>
<html lang="ko">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Flexbox Layout</title>
    <style>
      * {
        box-sizing: border-box;
        color: white;
      }

      .container {
        display: flex;
        flex-direction: column;
        width: 700px;
        height: 500px;
        background-color: black;
        gap: 10px;
        padding: 10px;
      }

      .main {
        flex: 1;
        /* flex-grow: 1; flex-shrink: 1; flex-basis: 0%; */
        display: flex;
        gap: 10px;
      }

      header,
      section,
      aside,
      footer {
        display: flex;
        justify-content: center;
        align-items: center;
        color: white;
        padding: 20px;
      }

      header {
        background-color: #6fa8dc;
      }

      section {
        background-color: #6d9eeb;
        flex-grow: 2;
      }

      aside {
        background-color: #b6d7a8;
        flex-grow: 1;
      }

      footer {
        background-color: #e06666;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <header>header</header>
      <div class="main">
        <section>section > article</section>
        <aside>aside</aside>
      </div>
      <footer>footer</footer>
    </div>
  </body>
</html>
  1. grid만 사용해보기
<!DOCTYPE html>
<html lang="ko">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Grid Layout</title>
    <style>
      * {
        box-sizing: border-box;
      }

      .container {
        display: grid;
        grid-template-rows: auto 1fr auto;
        gap: 10px;
        width: 700px;
        height: 500px;
        padding: 10px;
        background-color: black;
      }

      .main {
        display: grid;
        grid-template-columns: 2fr 1fr;
        gap: 10px;
      }

      header,
      section,
      aside,
      footer {
        display: flex;
        justify-content: center;
        align-items: center;
        color: white;
        padding: 20px;
      }

      header {
        background-color: #6fa8dc;
      }

      section {
        background-color: #6d9eeb;
      }

      aside {
        background-color: #b6d7a8;
      }

      footer {
        background-color: #e06666;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <header>header</header>
      <div class="main">
        <section>section > article</section>
        <aside>aside</aside>
      </div>
      <footer>footer</footer>
    </div>
  </body>
</html>

두 정답의 결과

위 두 정답을 각각 브라우저로 열어 같은 크기로 찍으면 아래와 같습니다.

flex만 쓴 정답과 grid만 쓴 정답은 거의 같은 화면을 만든다

검은 바탕이 .container, 그 안의 색 상자들이 header, section, aside, footer입니다. 그림 아래 숫자는 브라우저에서 잰 section과 aside의 크기입니다. 두 화면은 눈으로는 구별되지 않을 만큼 같고, 너비가 1px씩 다릅니다. flex 정답은 flex-grow: 2와 flex-grow: 1로 남는 공간을 2:1로 나누고, grid 정답은 2fr 1fr로 간격(gap)을 뺀 너비 전체를 2:1로 나누기 때문입니다(flex-grow 참고). 이처럼 같은 레이아웃도 여러 방법으로 만들 수 있습니다. 위아래로 쌓고 그 안을 좌우로 나누는 구조는 grid 쪽 코드가 더 짧고, 한 방향으로 나열하는 메뉴나 카드 줄은 flex가 더 간단한 경우가 많습니다.

마지막 수정