C.W.K.
Stream
Lesson 02 of 04 · published

정렬과 justification: justify-content, align-items, align-self

~11 min · justify-content, align-items, align-self, alignment

Level 0Markup Novice
0 XP0/34 lessons0/12 achievements
0/100 XP to next level100 XP to go0% complete
"가운데 정렬이 옛날엔 CSS 인터뷰 질문. 이제는 두 줄."

두 동사: Justify 와 Align

Flexbox 가 정렬 패밀리 둘 가짐, 축마다 하나:

  • justify-* — main 축 (항목 흐르는 방향) 에 작용.
  • align-* — cross 축 (main 에 수직) 에 작용.

일관된 어휘: justify = 따라, align = 가로질러. Grid 까지 운반 — 같은 prefix 가 같은 거 뜻함.

justify-content: 항목이 main 따라 앉는 자리

Main 축 따라 추가 공간이 어떻게 분배:

  • flex-start — 항목이 main 축 시작에 packed (기본). 추가 공간이 끝.
  • flex-end — 항목이 끝에 packed. 추가 공간이 시작.
  • center — 항목이 가운데 packed. 추가 공간이 양 끝 나뉨.
  • space-between — 항목 사이 동등 공간; 끝에 공간 없음.
  • space-around — 각 항목 주위 동등 공간; 끝이 항목 간 공간의 절반.
  • space-evenly — 끝 포함 어디나 동등 공간.

align-items: 항목이 cross 따라 앉는 자리

항목이 cross 축 (자기 줄 안) 에 어떻게 정렬:

  • stretch — 기본. 항목이 cross 축 채우게 stretch.
  • flex-start — 항목이 cross 축 시작에 정렬.
  • flex-end — 항목이 끝에 정렬.
  • center — 항목이 cross 축 가운데.
  • baseline — 항목이 텍스트의 baseline 으로 정렬. 항목이 다른 font 크기 가지고 텍스트 바닥이 line up 되길 원할 때 유용.

한 줄씩 가운데 정렬

유명한 "이거 수직 가운데 정렬" 질문, 해결:

.container {
  display: flex;
  justify-content: center;  /* 수평 가운데 */
  align-items: center;       /* 수직 가운데 */
}

또는, Grid 로 (더 짧게): display: grid; place-items: center;.

align-self: 한 자식 override

대부분 항목이 한 방향으로 정렬되어야 하지만 하나가 달라야 할 때, 자식의 align-self 가 부모의 align-items override:

align-content: Multi-line Flex 용

flex-wrap: wrap 이 여러 줄 만들 때, align-content 가 그 줄들이 cross 축 공간에 어떻게 분배되는지 제어 (justify-content 가 main 축에서 항목을 분배하는 것과 비슷). justify-content 와 같은 값: flex-start, center, space-between 등.

align-content (줄) 와 align-items (한 줄 안 항목) 헷갈리지 마. 둘 다 cross 축이지만 다른 레벨에서 작동.

place-content 약자

place-content: center = align-content: center; justify-content: center;. place-items: center = align-items: center; justify-items: center; (Grid). place-self: center = align-self: center; justify-self: center; (Grid). 간결한 코드에 유용; 읽기 좋은 코드는 longhand 써.

정렬로서의 margin: auto

margin-left: auto 가진 flex 자식이 왼쪽의 사용 가능한 main 축 공간을 다 소비, 오른쪽으로 밀어. 이렇게 justify-content 없이 왼쪽에 로고, 오른쪽에 메뉴 있는 navbar 만듦:

margin-right: auto 는 반대 — 뒤의 모든 걸 오른쪽으로 밀어. Column flex 의 margin-top: auto 가 바닥으로 밀어. margin: auto 가 양 축에 가운데 정렬 (옛 "margin auto 로 가운데 정렬" 패턴의 source, 이제 일반화).

모든 항목 spacing 에는 justify-content; 단일 push 에는 margin: auto. 한 항목만 sibling 을 반대 쪽으로 밀어야 하면 (로고 + 메뉴 같이), 그 한 항목의 margin: autojustify-content: space-between 과 wrapper 로 재구성하는 것보다 깨끗.

흔한 패턴, 축소

피파의 노트

cwkPippa header 가 flex row: 왼쪽 로고, 오른쪽 settings 버튼. 구현이 nav { display: flex; align-items: center; gap: 0.5rem; }.actions { margin-left: auto }. 한 element 의 한 선언이 사이에 얼마나 많은 메뉴 항목이 나타나든 상관없이 버튼을 오른쪽 가장자리로 밀어. Council 의 brain picker 가 justify-content: space-between 써서 너비에 brain 카드 4 개 spread — 같은 어휘, 다른 layout 의도.

Code

justify-content × align-items 참조·css
/* justify-content 여섯 값, 시각화 */
.demo {
  display: flex;
  outline: 2px dashed #ccc;
  height: 60px;
  margin: 1rem 0;
}
.demo > div { background: #5BA3D8; padding: 0.5rem 1rem; color: white; }

.start { justify-content: flex-start; }
.end { justify-content: flex-end; }
.center { justify-content: center; }
.between { justify-content: space-between; }
.around { justify-content: space-around; }
.evenly { justify-content: space-evenly; }

/* align-items 도 cross 축에서 같은 거: stretch (기본), flex-start,
   flex-end, center, baseline */
가운데 정렬과 margin: auto 패턴·css
/* 흔한 가운데 정렬, row vs column */

/* 가운데 정렬된 버튼 그룹 */
.btn-row {
  display: flex;
  justify-content: center;   /* 수평 */
  align-items: center;        /* 단일 라인 항목의 수직 */
  gap: 1rem;
}

/* 풀 페이지 hero 가운데 정렬 */
.hero {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  text-align: center;
}

/* 또는 더 간결한 grid 문법으로 */
.hero-grid {
  display: grid;
  place-items: center;
  min-height: 100vh;
}

/* 위에 content, 바닥에 CTA 가진 카드 */
.card {
  display: flex;
  flex-direction: column;
  height: 300px;
  padding: 1.5rem;
}
.card .cta {
  margin-top: auto;          /* CTA 를 column 바닥으로 밂 */
}
정렬 쓰는 실제 패턴·css
/* Navbar: 로고 왼쪽, 링크 오른쪽, 중첩 컨테이너 없음 */
.navbar {
  display: flex;
  align-items: center;
  gap: 1.5rem;
  padding: 0.75rem 1.5rem;
  border-bottom: 1px solid #eee;
}
.navbar .links {
  margin-left: auto;          /* 오른쪽 가장자리로 밂 */
  display: flex;
  gap: 1rem;
}

/* align-self override */
.review {
  display: flex;
  align-items: flex-start;     /* row 의 기본 */
  gap: 1rem;
}
.review .timestamp {
  align-self: flex-end;        /* 이 한 항목이 바닥에 정렬 */
  font-size: 0.85rem;
  color: #999;
}

/* 혼합 텍스트 크기용 baseline 정렬 */
.headline-with-byline {
  display: flex;
  align-items: baseline;
  gap: 1rem;
}
.headline-with-byline h2 { font-size: 2rem; }
.headline-with-byline span { font-size: 0.9rem; color: #666; }
/* Byline 텍스트 바닥이 h2 baseline 과 정렬 */

External links

Exercise

justify-content, align-items, gap, margin: auto 만 써서 layout 셋 만들기: (1) 로고 + 링크 + 사용자 아바타 가진 navbar — 로고 왼쪽, 아바타 오른쪽, 링크가 사이; (2) title, description, 바닥에 pinned 된 'Learn more' 링크 가진 카드; (3) 다른 height 의 버튼 셋이 baseline 정렬된 200px 높이 hero 안 가운데 정렬 단일 라인 버튼 행. 각자 DevTools 의 Flexbox overlay 로 확인.
Hint
Navbar 는 아바타에 margin-left: auto 주거나 자식이 셋만 있으면 justify-content: space-between 사용. 카드는 바닥에 pin 된 링크가 flex column 의 margin-top: auto 원해. Baseline 정렬은 버튼이 텍스트 content 있어야 — icon-only 나 비어 있는 element 의 baseline 정렬은 다른 규칙으로 fall back.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.