1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
게시판

08 / 14

CSS 레이아웃

카드가 여러 개면 격자로 깔려야죠. 세 가지를 해요. 카드를 감싸는 상자 만들기, 카드를 복사해서 세 개로 늘리기, 그리드 CSS.

templates/index.html에서 <article>을 <div class="cards">로 감싸고, 카드를 두 개 더 복사해 붙이세요. 주소, 이미지, 제목, 요약, 날짜, 작성자만 바꾸면 돼요.

templates/index.html
  <main>
    <h1>내 블로그</h1>
    <div class="cards">
      <article class="card">
        (첫 번째 카드는 그대로)
      </article>
      <article class="card">
        <a href="/posts/2">
          <img class="thumb" src="/static/thumb2.svg" alt="">
          <div class="body">
            <h2>Jinja2로 HTML 분리하기</h2>
            <p>app.py 안에 HTML을 문자열로 두면 금방 지저분해진다. templates 폴더로 꺼내자.</p>
            <span class="date">2026년 10월 11일</span>
          </div>
        </a>
        <div class="foot">
          <span>by <b>aa_100</b></span>
          <span>♥ 523</span>
        </div>
      </article>
      <article class="card">
        <a href="/posts/3">
          <img class="thumb" src="/static/thumb3.svg" alt="">
          <div class="body">
            <h2>fetch로 DELETE 보내기</h2>
            <p>버튼 하나에 fetch 한 줄. 서버는 리스트에서 글을 빼고 200을 돌려준다.</p>
            <span class="date">2026년 10월 12일</span>
          </div>
        </a>
        <div class="foot">
          <span>by <b>nosmeri</b></span>
          <span>♥ 9</span>
        </div>
      </article>
    </div>
  </main>

static/style.css에서 .card 안의 max-width: 320px; 줄은 지우고, 맨 아래에 추가해요.

static/style.css
.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 24px;
}

.card {
  transition: transform 0.25s, box-shadow 0.25s;
}

.card:hover {
  transform: translateY(-8px);
  box-shadow: 0 12px 24px rgba(0, 0, 0, 0.12);
}
  • display: grid 안의 것들을 격자로. grid-template-columns가 칸 나누는 규칙이에요. repeat(auto-fill, minmax(260px, 1fr))는 "260px 이상 되는 칸을 들어가는 만큼 만들어라". 창을 줄이면 알아서 줄 수가 바뀌어요.
  • gap 칸 사이 간격.
  • .card를 또 썼죠. 같은 선택자를 두 번 써도 돼요. 합쳐져요.
  • .card:hover 마우스를 올렸을 때. translateY(-8px)로 8px 떠오르고, transition이 그걸 0.25초 동안 부드럽게 해요.

실행하고 카드에 마우스를 올려 보세요. 가운데 경계선을 끌어서 오른쪽 너비를 바꿔 보면 줄 수가 바뀌어요.

이 단계 전체 코드
app.py
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates

app = FastAPI()
app.mount("/static", StaticFiles(directory="static"), name="static")
templates = Jinja2Templates(directory="templates")


@app.get("/", response_class=HTMLResponse)
def home(request: Request):
    return templates.TemplateResponse(request, "index.html")
templates/index.html
<!DOCTYPE html>
<html lang="ko">
<head>
  <meta charset="UTF-8">
  <title>crack.log</title>
  <link rel="stylesheet" href="/static/style.css">
</head>
<body>
  <header>
    <a href="/">crack.log</a>
  </header>
  <main>
    <h1>내 블로그</h1>
    <div class="cards">
      <article class="card">
        <a href="/posts/1">
          <img class="thumb" src="/static/thumb1.svg" alt="">
          <div class="body">
            <h2>FastAPI 첫 서버 띄우기</h2>
            <p>uvicorn 한 줄이면 끝. 흰 화면에 Hello World가 뜨는 순간부터 시작.</p>
            <span class="date">2026년 10월 10일</span>
          </div>
        </a>
        <div class="foot">
          <span>by <b>sfadfe</b></span>
          <span>♥ 518</span>
        </div>
      </article>
      <article class="card">
        <a href="/posts/2">
          <img class="thumb" src="/static/thumb2.svg" alt="">
          <div class="body">
            <h2>Jinja2로 HTML 분리하기</h2>
            <p>app.py 안에 HTML을 문자열로 두면 금방 지저분해진다. templates 폴더로 꺼내자.</p>
            <span class="date">2026년 10월 11일</span>
          </div>
        </a>
        <div class="foot">
          <span>by <b>aa_100</b></span>
          <span>♥ 523</span>
        </div>
      </article>
      <article class="card">
        <a href="/posts/3">
          <img class="thumb" src="/static/thumb3.svg" alt="">
          <div class="body">
            <h2>fetch로 DELETE 보내기</h2>
            <p>버튼 하나에 fetch 한 줄. 서버는 리스트에서 글을 빼고 200을 돌려준다.</p>
            <span class="date">2026년 10월 12일</span>
          </div>
        </a>
        <div class="foot">
          <span>by <b>nosmeri</b></span>
          <span>♥ 9</span>
        </div>
      </article>
    </div>
  </main>
</body>
</html>
static/style.css
:root {
  --bg: #f8f9fa;
  --panel: #ffffff;
  --text: #212529;
  --muted: #868e96;
  --line: #e9ecef;
  --mint: #12b886;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, "Segoe UI", "Noto Sans KR", "Malgun Gothic", sans-serif;
  background: var(--bg);
  color: var(--text);
}

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 32px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}

header a {
  font-size: 22px;
  font-weight: 800;
  color: var(--mint);
  text-decoration: none;
}

main {
  max-width: 1000px;
  margin: 0 auto;
  padding: 32px;
}

h1 { font-size: 22px; }

.card {
  background: var(--panel);
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.06);
}

.card a { color: inherit; text-decoration: none; }
.card .thumb { display: block; width: 100%; height: 160px; object-fit: cover; }
.card .body { padding: 16px; }
.card h2 { margin: 0 0 8px; font-size: 16px; }
.card p { margin: 0 0 12px; font-size: 14px; line-height: 1.5; color: var(--muted); }
.card .date { font-size: 12px; color: var(--muted); }

.card .foot {
  display: flex;
  justify-content: space-between;
  padding: 10px 16px;
  border-top: 1px solid var(--line);
  font-size: 12px;
  color: var(--muted);
}

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 24px;
}

.card {
  transition: transform 0.25s, box-shadow 0.25s;
}

.card:hover {
  transform: translateY(-8px);
  box-shadow: 0 12px 24px rgba(0, 0, 0, 0.12);
}
준비
localhost:8000 ↗