flexbox의 경우 flex item과 flex container가 있는 거 처럼,
grid도 grid item과 grid container가 있다.
<div class="container--1">
<div class="el el--1">(1) HTML</div>
<div class="el el--2">(2) and</div>
<div class="el el--3">(3) CSS</div>
<div class="el el--4">(4) are</div>
<div class="el el--5">(5) amazing</div>
<div class="el el--6">(6) languages</div>
<div class="el el--7">(7) to</div>
<div class="el el--8">(8) learn</div>
</div>
.el--1 {
background-color: blueviolet;
}
.el--2 {
background-color: orangered;
}
.el--3 {
background-color: green;
height: 150px;
}
.el--4 {
background-color: goldenrod;
}
.el--5 {
background-color: palevioletred;
}
.el--6 {
background-color: steelblue;
}
.el--7 {
background-color: yellow;
}
.el--8 {
background-color: crimson;
}
display: grid;
선언하기 (아직까진 변화 X)grid-template-columns: 250px 150px;
우리가 원하는 만큼 width 값을 설정해줄 수 있다.
여기서 하나는 250px, 다른 하나는 150px로 2개의 column을 만들어주었다.
flexbox와 마찬가지로 기본적으로는 item들 중 가장 큰 요소만큼 높다. (초록색 CSS item 주목)
grid-template-columns: 200px 200px 100px 100px;
grid-template-rows: 200px 400px;
gap: 10px
(gap = grid-gap)
column-gap: 30px;
row-gap: 60px;
grid-template-columns: 200px 200px 100px 100px;
grid-template-rows: 200px 400px;
gap: 10px;
grid-template-columns: 200px 200px 1fr 100px;
grid-template-rows: 200px 400px;
gap: 10px;
→ flexbox에서 flex를 1로 설정하는 것과 비슷한 동작
→ 반복 기능이 있다. 수와 크기를 정하면 자동으로 반복해준다. 아래의 경우 1fr을 4번 쓴 것과 같다.
grid-template-columns: repeat(4, 1fr);
grid-template-columns: 1fr 1fr 1fr auto;
grid-template-rows: 200px 400px;
gap: 10px;
명시적 행은 크기가 명확히 지정된 행이고, 암묵적 행은 자동으로 추가된 행으로 컨텐츠의 크기만큼 공간을 차지한다. 열의 경우도 같다.
grid-template-columns: repeat(3, 1fr); /* 3개의 열 명시 */
grid-template-rows: 200px 400px; /* 2개의 행 명시 */
/*
해당 그리드 컨테이너에 8개의 그리드 아이템이 있기 때문에
명시하지 않은 3번째 열이 존재한다.
이 열은 암묵적 행으로 컨텐츠 크기만큼 공간을 차지한다.
*/
.el--3 {
background-color: green;
height: 150px;
}
grid-template-columns: repeat(4, 1fr);
grid-template-rows: 1fr 1fr;
→ (3) CSS에만 height가 150px로 지정되어 있는데, 이 때 grid-template-rows: 1fr 1fr;
을 주면 모든 행의 크기가 150px이 된다. 1fr의 기준이 150px이 된 것이다.
.el--3 {
background-color: green;
/* height: 150px; */
}
→ (3) CSS에 있는 height를 없애면, row의 1fr 기준은 필요한 컨텐츠의 크기 만큼으로 변경된다.
.el--3 {
background-color: green;
/* height: 150px; */
}
.container--1{
grid-template-columns: repeat(4, 1fr);
grid-template-rows: 1fr 1fr;
height: 300px;
gap: 10px;
}
→ 컨테이너의 높이가 300px이 되고 이를 채울 수 있는 크기로 1fr이 조정된다.
위를 보면 1부턴 5까지의 column과 1부터 3까지의 row를 그리는 grid line을 볼 수 있다.
이에 대해 grid-column과 grid-row에 값을 준다면?
.el--2 {
grid-column: 1/2; /* column 1부터 시작해 2에서 끝나야 한다는 뜻 */
grid-row: 2/3; /* start : 2 , end : 3 */
}
.el--8 {
grid-column: 2/3; /* start : 2 , end : 3 */
grid-row: 1/2; /* start : 1 , end : 2 */
}
/* 위와 동일한 코드.
두 번째 값이 첫 번째 값보다 하나 더 크면, 두 번째 값을 생략해도 된다. */
.el--2 {
grid-column: 1 /* start : 1 , end : 2 */
grid-row: 2; /* start : 2 , end : 3 */
}
.el--8 {
grid-column: 2; /* start : 2 , end : 3 */
grid-row: 1; /* start : 1 , end : 2 */
}
/* 라인의 최종 값을 지정하는 대신
n개의 셀을 차지하도록 span n을 사용할 수 있다. */
.el--2 {
grid-column: 1 / span 3;
grid-row: 2 / span 1;
}
.el--8 {
grid-column: 2 / span 2;
grid-row: 1 / span 1;
.el--2 {
grid-column: 1 / -1;
grid-row: 2;
}
.el--8 {
grid-column: 2 / -1;
grid-row: 1;
}
.container--1{
...
grid-template-rows: 1fr 1fr 1fr 1fr;
...
}
→ grid line은 양수 뿐만 아니라 음수로도 표현이 가능하다.
display: grid;
grid-template-columns: 125px 200px 125px;
grid-template-rows: 250px 100px;
justify-content: center;
align-content: center;
justify-items: center;
align-items: center;
justify-items: center;
align-items: center;
.el--1{
justify-self: start;
}
.el--1{
align-self: start;
}
.el--1{
justify-self: start;
align-self: start;
}
→ flexbox에서는 flex-start, flex-end를 쓰지만
→ grid에서는 그냥 start, end를 쓴다.