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

다 만들었을 때 아래 네 가지가 맞으면 됩니다.
- 바깥 상자(
.container) 안에서header, 가운데 줄(.main),footer가 위에서 아래로 쌓인다. - 가운데 줄 안에서
section과aside가 왼쪽·오른쪽으로 나란히 놓이고,section이 더 넓다. - 상자와 상자 사이에 간격이 있다.
header와footer는 내용만큼의 높이이고, 가운데 줄이 남은 높이를 모두 채운다.
힌트 보기
flex든 grid든 레이아웃은 바로 아래 자식에게만 걸립니다(flex 1.1). 그래서 위아래로 쌓는 일은 .container에, 좌우로 나누는 일은 .main에 따로 줘야 합니다. 4번처럼 가운데 줄이 남은 높이를 채우게 하려면 .container에 높이를 정해 주세요. 높이를 정하지 않으면 남는 높이가 없습니다.
정답이 하나만 있는 것은 아니기 때문에 색, 크기는 자유롭게 작업하세요. 아래는 기본 코드입니다.
<!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>
위 코드를 index.html로 저장해 브라우저로 열어도 되고, 아래 결과 화면에서 바로 해 봐도 됩니다. 아래에는 기본 코드의 <body> 안쪽과 <style> 안쪽만 옮겨 두었으니 CSS 칸에 이어서 적어 보세요. 처음에는 결과 화면에 아무것도 보이지 않는 것이 맞습니다. 기본 코드가 모든 글자를 흰색(color: white)으로 정해 두어서, 흰 바탕에 흰 글자가 놓여 있기 때문입니다. 상자에 배경색을 주면 글자가 나타납니다.
<div class="container">
<header>header</header>
<div class="main">
<section>section > article</section>
<aside>aside</aside>
</div>
<footer>footer</footer>
</div>
* {
box-sizing: border-box;
color: white;
}
정답
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>
2. 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>
두 정답의 결과
위 두 정답을 각각 브라우저로 열어 같은 크기로 찍으면 아래와 같습니다.

검은 바탕이 .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가 더 간단한 경우가 많습니다.
마지막 수정