페이지를 구획으로 나누는 다양한 방법
<div class="wrapper">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
</div>
body { margin: 0; padding: 0; }
.wrapper { border: 2px solid black; }
.wrapper > div {
background-color: yellow;
}
.wrapper > div:nth-child(even) {
background-color: yellowgreen;
}
.wrapper {
display: grid;
}
=> 당장은 바뀌는 부분은 없지만 그러나 안 쪽에 내용을 어떻게 표시할 지 속성으로 넣을 수 있게 됨
grid-template-columns
.wrapper {
grid-template-columns: 100px 200px 300px;
}
=> 페이지 너비에 따라서 빈 공간이 생길 수 있음 추천 안 함
.wrapper {
grid-template-columns: 1fr 2fr 3fr 4fr; // 1 : 2 : 3 : 4
}
=> 페이지 너비에 상관없이 꽉 차게 사용 가능
.wrapper {
grid-template-columns: 200px 1fr 3fr;
}
=> 고정하고 싶은 값만 절대값으로 고정하고 나머지 값은 비율 분할
.wrapper {
grid-template-columns: repeat(3, 1fr);
}
grid-template-rows
.wrapper {
grid-template-columns: repeat(3, 1fr);
grid-template-rows: 100px 200px 400px;
}
.wrapper {
grid-template-columns: repeat(3, 1fr);
height: 800px;
grid-template-rows: 1fr 2fr 3fr;
}
.wrapper {
grid-template-columns: repeat(3, 1fr);
height: 800px;
grid-template-rows: 1fr 2fr 3fr;
}
<div class="wrapper">
<div><div>1</div></div>
<div><div>2</div></div>
<div><div>3</div></div>
<div><div>4</div></div>
<div><div>5</div></div>
<div><div>6</div></div>
<div><div>7</div></div>
<div><div>8</div></div>
<div><div>9</div></div>
</div>
style.css
.wrapper {
grid-template-columns: repeat(3, 1fr);
}
.wrapper > div > div { background-color: orange; }
.wrapper > div:nth-child(even) > div { background-color: tomato; }
.wrapper > div:nth-child(3n + 1) > div {
height: 100px;
}
.wrapper > div:nth-child(3n + 2) > div {
height: 200px;
}
.wrapper > div:nth-child(3n) > div {
height: 300px;
}
.wrapper {
grid-template-rows: 100px auto minmax(100px, 200px);
}
grid-column, grid-row
<div class="wrapper">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
.wrapper {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(4, 150px);
}
.wrapper > div {
background-color: yellow;
}
.wrapper > div:nth-child(even) {
background-color: yellowgreen;
}
.wrapper > div:nth-child(1) {
background-color: red;
grid-column: 1/4;
grid-row: 1/2;
}
.wrapper > div:nth-child(2) {
background-color: green;
grid-column: 1/2;
grid-row: 2/4;
}
.wrapper > div:nth-child(3) {
background-color: purple;
grid-column: 2/4;
grid-row: 2/4;
}
.wrapper > div:nth-child(4) {
background-color: blue;
grid-column: 1/3;
grid-row: 4/5;
}
.wrapper > div:nth-child(5) {
background-color: orange;
grid-column: 3/4;
grid-row: 4/5;
}
grid-template-rows
.wrapper {
grid-template-areas:
"red red red"
"green blue blue"
"green blue blue"
"purple purple orange";
}
.wrapper > div:nth-child(1) {
background-color: red;
grid-area: red;
}
.wrapper > div:nth-child(2) {
background-color: green;
grid-area: green;
}
.wrapper > div:nth-child(3) {
background-color: blue;
grid-area: blue;
}
.wrapper > div:nth-child(4) {
background-color: purple;
grid-area: purple;
}
.wrapper > div:nth-child(5) {
background-color: orange;
grid-area: orange;
}
.wrapper {
gap: 10px;
}