CSS

[CSS] Grid 레이아웃 공부2 Template areas, auto-fit, min-max

이채야채 2022. 1. 14. 23:03

Template areas


 

초기세팅

<!DOCTYPE html>
<html lang="en">
  <head>
    <style>
      .container div {
        text-align: center;
        color: #f2f2f2;
        font-size: 2em;
      }
      .container {
        display: grid;
        grid-gap: 3px;
        grid-template-columns: repeat(12, 1fr);
        grid-template-rows: 40px 200 40x;
        /* background-color: olive; */
      }
      .header {
        background-color: red;
      }

      .menu {
        background-color: yellow;
      }

      .content {
        background-color: green;
      }

      .footer {
        background-color: violet;
      }
    </style>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <div class="container">
    <div class="header">HEADER</div>
    <div class="menu">MENU</div>
    <div class="content">CONTENT</div>
    <div class="footer">FOOTER</div>
  </div>
</html>

 

ㅋㅋㅋㅋㅋ

요딴짓을해보면?

  <style>
      .container div {
        text-align: center;
        color: #f2f2f2;
        font-size: 2em;
      }
      .container {
        display: grid;
        grid-gap: 3px;
        grid-template-columns: repeat(12, 1fr);
        grid-template-rows: 40px 200 40x;
        grid-template-areas:
          "h h h h h h h h h h h h"
          "m c c c c c c c c c c c"
          "f f f f f f f f f f f f";
      }
      .header {
        background-color: red;
        grid-area: h;
      }

      .menu {
        background-color: yellow;
        grid-area: m;
      }

      .content {
        background-color: green;
        grid-area: c;
      }

      .footer {
        background-color: violet;
        grid-area: f;
      }

12개의 나만의 그리드 시스템을 만든거

 

 

컬럼라인수로 하는게 아니고 이름으로 지정해주는것 

 

비우고싶으면? 그자리에 . 을 넣으면된다!


Auto-fit


기본셋팅

<!DOCTYPE html>
<html lang="en">
  <head>
    <style>
      .container div {
        text-align: center;
        font-size: 2em;
      }
      .container {
        display: grid;
        grid-gap: 3px;
        grid-template-columns: repeat(6, 1fr);
        grid-template-rows: repeat(2, 100px);
      }
      .container div:nth-child(1) {
        background-color: red;
      }

      .container div:nth-child(2) {
        background-color: yellow;
      }

      .container div:nth-child(3) {
        background-color: green;
      }

      .container div:nth-child(4) {
        background-color: violet;
      }
      .container div:nth-child(5) {
        background-color: blue;
      }
      .container div:nth-child(6) {
        background-color: black;
      }
      .container div:nth-child(7) {
        background-color: skyblue;
      }
      .container div:nth-child(8) {
        background-color: orange;
      }
      .container div:nth-child(9) {
        background-color: burlywood;
      }
      .container div:nth-child(10) {
        background-color: brown;
      }
      .container div:nth-child(11) {
        background-color: gray;
      }
      .container div:nth-child(12) {
        background-color: salmon;
      }
    </style>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <div class="container">
    <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>10</div>
    <div>11</div>
    <div>12</div>
  </div>
</html>

 

auto 기능

컨테이너 부분을 auto로 바꾸면?

 

.container {
        display: grid;
        grid-gap: 3px;
        grid-template-columns: repeat(6, 1fr);
        grid-template-rows: repeat(2, 100px);
      }

- 일반화면일때

- 화면을 내가 조정해서 줄이면??

 => 반응형웹이여서 아래처럼 내려간다.

 => 100px 아래일때 알아서 밑으로 떨어진다.

 

이옆에 비는부분이 짜증나는데..

바꾸려면 어떻게 해야할까?

 

repeat(컬럼의개수, 너비)

 

비는 칸이 없어진다. => 최소 100px 최대 1:1:1 비율

minmax를 안주면 auto 자체가 안먹힘

    .container {
        display: grid;
        grid-gap: 3px;
        grid-template-columns: repeat(auto-fit, minmax(100px,1fr));
        grid-template-rows: repeat(2, 100px);
      }