<div class="container">
<div class="item item1"></div>
<div class="item item2"></div>
<div class="item item3"></div>
<div class="item item4"></div>
<div class="item item5"></div>
<div class="item item6"></div>
</div>
.container {
display: grid;
grid-template-rows: 150px 150px;
grid-template-columns: 150px 150px 150px;
grid-gap: 1rem;
}
/* OTHER STYLES */
body {
background-color: #3b404e;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
.item {
background-color: #1EAAFC;
background-image: linear-gradient(130deg, #6C52D9 0%, #1EAAFC 85%, #3EDFD7 100%);
box-shadow: 0 10px 20px rgba(0,0,0,0.19), 0 6px 6px rgba(0,0,0,0.23);
color: #fff;
border-radius: 4px;
border: 6px solid #171717;
}
CSS Grid 레이아웃은 fr
이라는 새로운 길이 단위를 도입했는데, 이는 분수(fraction)
를 줄인 것입니다. MDN은 fr
단위를 그리드 컨테이너에서 사용 가능한 공간의 일부를 나타내는 단위로 정의합니다.
.container {
display: grid;
width: 800px; /* 고정 너비 지정 */
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: 150px 150px;
grid-gap: 1rem;
}
길이 단위를 반복해야 할 경우, CSS repeat()
함수를 사용합니다.
.container {
display: grid;
width: 800px;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(2, 150px);
grid-gap: 1rem;
}
트랙 크기를 선언할 때, px나 em과 같은 고정된 단위로 크기를 지정할 수 있습니다. 또한 백분율이나 fr 단위와 같은 유연한 크기를 사용할 수도 있습니다.
그러나 CSS Grid 레이아웃의 진정한 매력은 이러한 단위를 혼합하여 사용할 수 있는 능력입니다.
.container {
width: 100%;
display: grid;
grid-template-columns: 100px 30% 1fr;
grid-template-rows: 200px 100px;
grid-gap: 1rem;
}
여기에서 세 개의 열을 가진 그리드를 선언했습니다.