CSS Grid를 써서 반응형 레이아웃을 만들다 보면 repeat() 함수 안에서 auto-fit과 auto-fill이라는 키워드를 마주친다. 둘 다 비슷해 보이지만 그리드 트랙 배치 방식이 완전히 다르고, 실무에서 선택을 잘못하면 레이아웃이 의도와 달라진다. 개발자들은 보통 그냥 하나를 택해서 쓰다가 빈 트랙이 생기거나 아이템이 늘어나는 현상을 경험하곤 한다. 이번에는 auto-fit과 auto-fill이 정확히 뭔지, 언제 어떤 걸 써야 하는지, 실제 코드와 예제로 완벽하게 정리해보겠다.

 

1단계. auto-fit과 auto-fill의 기초 개념

CSS Grid에서 repeat() 함수는 반복되는 트랙을 간편하게 정의한다. 고정 개수를 쓸 수도 있고 repeat(auto-fit, minmax(200px, 1fr))처럼 동적으로 갯수를 계산하게 할 수도 있다.

auto-fill은 가능한 많은 트랙을 그리드 컨테이너에 맞춰 생성한다. 콘텐츠가 없는 빈 트랙도 만든다. 화면이 넓으면 콘텐츠는 3개인데 트랙은 5개가 생기는 식이다.

auto-fit은 콘텐츠가 없는 빈 트랙을 축소(collapse)시킨다. 콘텐츠만 채워진 트랙들로만 그리드를 구성하므로 불필요한 공간이 생기지 않는다.

 

2단계. 실전 비교: auto-fill vs auto-fit

동일한 HTML 구조에 CSS만 다르게 적용해서 차이를 명확하게 보자.

✗ auto-fill을 쓴 경우 - 빈 트랙이 남을 수 있다

.grid-container {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 16px;
  width: 100%;
}

.grid-item {
  background: #3498db;
  padding: 20px;
  text-align: center;
  color: white;
  border-radius: 4px;
}

HTML:

<div class="grid-container">
  <div class="grid-item">1</div>
  <div class="grid-item">2</div>
  <div class="grid-item">3</div>
</div>

컨테이너 너비가 1000px이고 각 아이템이 최소 200px이면 auto-fill은 약 5개 트랙을 만든다. 하지만 아이템은 3개뿐이므로 2개 트랙이 비어있다. 이 빈 트랙들이 화면 오른쪽에 공간을 차지한다.

✓ auto-fit을 쓴 경우 - 빈 트랙이 축소되어 아이템이 넓어진다

.grid-container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 16px;
  width: 100%;
}

.grid-item {
  background: #2ecc71;
  padding: 20px;
  text-align: center;
  color: white;
  border-radius: 4px;
}

auto-fit은 빈 트랙을 없앤다(0px로 축소). 그러면 남은 3개 트랙의 1fr이 더 커져서 아이템들이 더 넓게 늘어난다. 같은 콘텐츠인데 더 여유 있게 보인다.

 

3단계. 실제 동작 원리 깊이 있게 이해하기

컨테이너 너비 1000px, minmax(200px, 1fr) 설정, gap 16px, 아이템 3개라고 가정하자.

auto-fill 계산:

트랙 개수 = floor((1000 + 16) / (200 + 16)) = floor(1016 / 216) = 약 4~5개 트랙 생성. 아이템 3개 뒤에 1~2개 빈 트랙 남음. 각 아이템은 1fr의 크기를 받지만 빈 트랙도 1fr의 여유 공간을 차지한다.

auto-fit 계산:

동일하게 5개 트랙을 먼저 계산하지만, 아이템 3개 뒤의 2개 빈 트랙을 0px로 축소한다. 그러면 1000px 공간을 3개 트랙과 2개 gap(32px)으로만 나눈다. 각 아이템이 더 커진다.

 

4단계. 실제 프로젝트 예제 - 제품 카드 그리드

상품을 그리드로 표시하는 예제로 auto-fit과 auto-fill의 선택이 얼마나 중요한지 확인해보자.

❌ 잘못된 선택 - auto-fill로 빈 공간 낭비

<!DOCTYPE html>
<html>
<head>
  <style>
    .product-grid {
      display: grid;
      grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
      gap: 20px;
      padding: 20px;
      max-width: 800px;
    }
    .product-card {
      background: #ecf0f1;
      padding: 15px;
      border-radius: 8px;
      text-align: center;
    }
  </style>
</head>
<body>
  <div class="product-grid">
    <div class="product-card">상품 1</div>
    <div class="product-card">상품 2</div>
  </div>
</body>
</html>

max-width 800px에서 minmax(150px, 1fr)이면 5개 트랙을 만들려 한다. 하지만 상품은 2개뿐이므로 오른쪽에 3개 트랙의 빈 공간이 낭비된다. 화면이 답답해 보인다.

✅ 올바른 선택 - auto-fit으로 공간 효율화

<!DOCTYPE html>
<html>
<head>
  <style>
    .product-grid {
      display: grid;
      grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
      gap: 20px;
      padding: 20px;
      max-width: 800px;
    }
    .product-card {
      background: #3498db;
      color: white;
      padding: 15px;
      border-radius: 8px;
      text-align: center;
    }
  </style>
</head>
<body>
  <div class="product-grid">
    <div class="product-card">상품 1</div>
    <div class="product-card">상품 2</div>
  </div>
</body>
</html>

auto-fit을 사용하면 2개 아이템에 맞춰서 2개 트랙만 최종 유지된다. 2개 카드가 각각 1fr씩 받아서 800px를 2등분하고, 각 카드가 넉넉한 너비를 가진다. 더 균형 잡힌 레이아웃이다.

 

5단계. 언제 뭘 써야 할까? 선택 기준

auto-fill을 쓸 때: 갤러리나 포토 그리드처럼 항상 최대한 많은 열을 보여주고 싶을 때. 아이템이 적어도 여유 있는 그리드 구조를 유지하고 싶을 때. 예를 들어 썸네일 갤러리에서 아이템이 적어도 항상 5개 열 구조를 유지하면 더 멋있을 수 있다.

auto-fit을 쓸 때: 아이템 수가 가변적이고 콘텐츠를 최대한 활용해서 보여주고 싶을 때. 반응형 카드 레이아웃, 태그 목록, 상품 카드 등에서 auto-fit이 대부분 더 나은 선택이다. 아이템이 적어도 큰 사이즈로 넉넉하게 표시된다.

 

6단계. 반응형 조정 - minmax() 값 최적화

auto-fit/auto-fill의 동작은 minmax() 최솟값에 큰 영향을 받는다. 최솟값이 작을수록 더 많은 트랙이 생기고, 클수록 적은 트랙이 생긴다.

모바일 최적화:

/* 모바일(~600px): 최소 100px, 큰 화면: 최소 200px */
.grid-container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
  gap: 12px;
}

@media (min-width: 768px) {
  .grid-container {
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 16px;
  }
}

이렇게 하면 모바일에선 minmax(100px, 1fr)로 더 많은 열을 만들고, 태블릿 이상에선 minmax(200px, 1fr)로 더 큰 아이템을 표시한다.

 

7단계. 흔한 실수와 해결책

❌ 실수 1: auto-fit인데도 빈 공간이 생김

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  width: 500px; /* 너무 좁음 */
}

컨테이너 너비가 500px인데 minmax(200px, 1fr)이면 2개 트랙만 생긴다. auto-fit이라도 아이템이 1개면 1개 트랙만 최종 유지된다. 오른쪽에 1개 열 크기의 공간이 아예 없어진다. 이건 auto-fit의 정상 동작이다.

✓ 해결책: width 충분히 하거나 minmax 최솟값 조정

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
  width: 500px;
}

minmax를 100px로 낮추면 500px에 최대 5개 트랙 가능. 아이템 1개라도 auto-fit으로 그 1개가 더 넓어진다.

❌ 실수 2: auto-fill인데 반응형이 안 됨

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  /* 화면이 1000px 미만에서도 minmax가 고정 */
}

모바일(375px)에서 minmax(200px, 1fr)이면 1개 트랙만 생기고 나머지 공간은 1fr로 커진다. 그런데 auto-fill은 그 1개 트랙 옆에 보이지 않는 빈 트랙들을 계속 만들려고 한다. 성능 저하가 없지만 개념상 비효율적이다.

✓ 해결책: 미디어 쿼리로 minmax 단계적 조정

@media (max-width: 600px) {
  .grid {
    grid-template-columns: repeat(auto-fill, minmax(80px, 1fr));
  }
}

@media (min-width: 601px) and (max-width: 1024px) {
  .grid {
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  }
}

@media (min-width: 1025px) {
  .grid {
    grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  }
}

 

8단계. 완전한 실무 예제 - 적응형 대시보드

카드 형태의 위젯들을 보여주는 대시보드 레이아웃.

<!DOCTYPE html>
<html>
<head>
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }

    body {
      font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto;
      background: #f5f5f5;
      padding: 20px;
    }

    .dashboard {
      display: grid;
      grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
      gap: 20px;
      max-width: 1400px;
      margin: 0 auto;
    }

    .widget {
      background: white;
      border-radius: 8px;
      padding: 24px;
      box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
      transition: transform 0.2s, box-shadow 0.2s;
    }

    .widget:hover {
      transform: translateY(-2px);
      box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    }

    .widget-title {
      font-size: 14px;
      color: #666;
      margin-bottom: 12px;
      text-transform: uppercase;
      letter-spacing: 0.5px;
    }

    .widget-value {
      font-size: 32px;
      font-weight: bold;
      color: #333;
      margin-bottom: 8px;
    }

    .widget-change {
      font-size: 12px;
      color: #27ae60;
    }

    .widget-change.negative {
      color: #e74c3c;
    }

    @media (max-width: 768px) {
      .dashboard {
        grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
        gap: 16px;
      }
      .widget {
        padding: 16px;
      }
      .widget-value {
        font-size: 24px;
      }
    }

    @media (max-width: 480px) {
      .dashboard {
        grid-template-columns: 1fr;
      }
    }
  </style>
</head>
<body>
  <div class="dashboard">
    <div class="widget">
      <div class="widget-title">매출</div>
      <div class="widget-value">$12,500</div>
      <div class="widget-change">↑ 12% 증가</div>
    </div>
    <div class="widget">
      <div class="widget-title">방문자</div>
      <div class="widget-value">3,842</div>
      <div class="widget-change">↑ 8% 증가</div>
    </div>
    <div class="widget">
      <div class="widget-title">환율</div>
      <div class="widget-value">68.5%</div>
      <div class="widget-change negative">↓ 2% 감소</div>
    </div>
    <div class="widget">
      <div class="widget-title">활성 사용자</div>
      <div class="widget-value">521</div>
      <div class="widget-change">↑ 24% 증가</div>
    </div>
  </div>
</body>
</html>

이 대시보드는 auto-fit을 사용해서 카드 개수가 가변적이어도 항상 콘텐츠를 최대로 활용한다. 화면이 넓으면 4개 열, 중간 크기면 2~3개 열, 모바일은 1개 열로 자동 조정된다. 빈 공간 낭비 없이 모든 카드가 충분한 크기를 유지한다.

 

9단계. 최종 선택 기준 체크리스트
상황 추천 선택 이유
상품/카드 그리드, 아이템 개수 불고정 auto-fit 빈 트랙 축소로 카드가 더 커지고 안정적
이미지 갤러리, 썸네일, 항상 균등 배열 auto-fill 많은 열 유지로 갤러리 느낌 연출
태그, 뱃지, 적응형 목록 auto-fit 아이템 수가 가변적이고 공간 효율 중요
고정 개수의 콘텐츠 블록 auto-fit 불필요한 빈 공간 제거
화면 가득 메우는 그리드 필수 auto-fill 빈 열도 포함해서 항상 채운다

 

마무리

CSS Grid의 repeat(auto-fit, minmax())과 repeat(auto-fill, minmax())은 비슷해 보이지만 빈 트랙 처리 방식이 완전히 다르다. auto-fit은 콘텐츠만큼만 트랙을 유지해서 불필요한 공간을 없애고, auto-fill은 가능한 많은 트랙을 만들어서 여유 있는 그리드를 유지한다. 대부분의 실무 프로젝트에서는 auto-fit이 더 나은 선택이다. 왜냐하면 사용자가 보는 콘텐츠가 항상 최적의 크기를 유지하고, 아이템 개수 변화에도 유연하게 대응하기 때문이다. minmax() 최솟값을 미디어 쿼리로 단계적으로 조정하면 모든 화면에서 완벽한 반응형 레이아웃을 얻을 수 있다는 점도 잊지 말자. 이 글의 체크리스트와 실무 예제를 참고해서 다음 프로젝트에서 auto-fit/auto-fill을 올바르게 선택하면, 더 이상 그리드 레이아웃으로 고민하지 않을 것이다.