웹 페이지를 만들다 보면 버튼 몇 개를 가로로 정렬하거나 카드들을 균등하게 배치해야 할 일이 많다. 대부분의 개발자들은 Flexbox를 쓰면서도 justify-content와 align-items의 차이를 제대로 모른 채 계속 헷갈려한다. 심지어 최신 gap 속성까지 혼동하면서 여백 처리를 두 번 세 번 고쳐야 하는 상황이 반복된다. 이번에는 Flexbox 정렬 속성의 정확한 동작 원리와 각 속성이 왜 필요한지, 그리고 실무에서 가장 흔한 정렬 오류를 완벽하게 정리해서 소개하겠다.
justify-content는 주축 방향 정렬을 담당하고, align-items는 교차축 방향 정렬을 담당한다. flex-direction을 column으로 바꾸면 주축과 교차축의 방향이 뒤바뀌므로, 각 속성의 역할도 함께 뒤바뀐다는 점이 개발자들을 헷갈리게 한다.
| 축 종류 | 기본값(flex-direction: row) | column 변경 후 | 제어 속성 |
|---|---|---|---|
| 주축(Main Axis) | 가로(왼쪽→오른쪽) | 세로(위→아래) | justify-content |
| 교차축(Cross Axis) | 세로(위→아래) | 가로(왼쪽→오른쪽) | align-items |
flex-start: 아이템을 주축의 시작점에 모은다. 기본값이다.
flex-end: 아이템을 주축의 끝점에 모은다.
center: 아이템을 주축의 중앙에 모은다.
space-between: 첫 번째와 마지막 아이템을 양 끝에 붙이고, 나머지는 균등하게 여백을 분배한다.
space-around: 모든 아이템 주변에 동일한 여백을 분배한다. 아이템 사이 여백이 양 끝 여백의 2배다.
space-evenly: 모든 여백을 완전히 균등하게 분배한다.
<!-- ✗ 잘못된 코드: justify-content 없이 여백을 margin으로만 처리 -->
<style>
.container {
display: flex;
width: 100%;
}
.item {
width: 100px;
margin-left: auto; /* 이렇게 하면 첫 번째 아이템만 밀린다 */
margin-right: auto;
}
</style>
<div class="container">
<button class="item">버튼 1</button>
<button class="item">버튼 2</button>
<button class="item">버튼 3</button>
</div>
문제점: 모든 아이템에 margin-left: auto를 주면 각각이 독립적으로 오른쪽으로 밀려서, 버튼들이 한쪽으로 쏠린다.
<!-- ✓ 올바른 코드: justify-content로 주축 정렬 -->
<style>
.container {
display: flex;
justify-content: center; /* 모든 아이템을 중앙 정렬 */
width: 100%;
gap: 10px; /* 아이템 사이 간격 */
}
.item {
width: 100px;
}
</style>
<div class="container">
<button class="item">버튼 1</button>
<button class="item">버튼 2</button>
<button class="item">버튼 3</button>
</div>
결과: 세 버튼이 컨테이너의 중앙에 모여서 정렬되고, gap으로 균등한 간격이 유지된다.
stretch: 아이템을 교차축 방향으로 꽉 채운다. 기본값이다.
flex-start: 아이템을 교차축의 시작점에 모은다.
flex-end: 아이템을 교차축의 끝점에 모은다.
center: 아이템을 교차축의 중앙에 모은다.
baseline: 아이템의 텍스트 기준선을 맞춘다. 텍스트 크기가 다를 때 유용하다.
<!-- ✗ 잘못된 코드: 높이를 명시하지 않고 align-items만 사용 -->
<style>
.container {
display: flex;
align-items: center; /* 높이가 없어서 효과가 없음 */
width: 100%;
}
.item {
width: 100px;
/* height를 지정하지 않음 */
}
</style>
<div class="container">
<button class="item">버튼 1</button>
<button class="item">버튼 2</button>
</div>
문제점: 컨테이너에 높이가 없으면 align-items:center는 아무 효과도 내지 못한다. 아이템이 콘텐츠의 높이에 맞춰져 있기 때문이다.
<!-- ✓ 올바른 코드: 컨테이너 높이를 지정하고 align-items 사용 -->
<style>
.container {
display: flex;
align-items: center; /* 교차축(세로) 중앙 정렬 */
width: 100%;
height: 100px; /* 명시적 높이 필수 */
}
.item {
width: 100px;
}
</style>
<div class="container">
<button class="item">버튼 1</button>
<button class="item">버튼 2</button>
</div>
결과: 컨테이너 높이가 100px일 때, 버튼들이 세로 중앙에 정렬된다.
gap: 10px는 아이템들 사이에만 간격을 넣고, 양 끝에는 여백이 없다는 게 중요하다. 반면 아이템들에 margin을 주면 양 끝까지 여백이 적용되므로, 정렬 결과가 달라진다.
<!-- ✗ 잘못된 코드: margin으로 간격을 주면 양 끝까지 여백 발생 -->
<style>
.container {
display: flex;
justify-content: space-between;
width: 100%;
}
.item {
width: 100px;
margin-right: 15px; /* 마지막 아이템도 오른쪽에 여백이 생김 */
}
.item:last-child {
margin-right: 0; /* 매번 이렇게 처리해야 함 */
}
</style>
<div class="container">
<button class="item">버튼 1</button>
<button class="item">버튼 2</button>
<button class="item">버튼 3</button>
</div>
문제점: :last-child 선택자로 마지막 여백을 없애야 하는 번거로움이 있다.
<!-- ✓ 올바른 코드: gap으로 간단하게 간격 관리 -->
<style>
.container {
display: flex;
justify-content: space-between;
width: 100%;
gap: 15px; /* 아이템 사이에만 간격, 양 끝은 여백 없음 */
}
.item {
width: 100px;
}
</style>
<div class="container">
<button class="item">버튼 1</button>
<button class="item">버튼 2</button>
<button class="item">버튼 3</button>
</div>
결과: gap으로 명확하게 아이템 간격만 정의되고, justify-content와 함께 작동한다.
<!-- ✗ 잘못된 코드: 높이 없이 중앙 정렬 시도 -->
<style>
.modal-container {
display: flex;
justify-content: center;
align-items: center;
/* height를 지정하지 않음 */
width: 100%;
}
.modal {
width: 400px;
padding: 20px;
background: white;
border: 1px solid #ddd;
}
</style>
<div class="modal-container">
<div class="modal">
<h2>로그인</h2>
<p>아이디와 비밀번호를 입력하세요</p>
</div>
</div>
문제점: 컨테이너가 콘텐츠 높이에 맞춰져서, 모달이 화면 중앙이 아닌 왼쪽 위에 붙는다.
<!-- ✓ 올바른 코드: 화면 전체를 차지하는 컨테이너로 중앙 정렬 -->
<style>
.modal-container {
display: flex;
justify-content: center; /* 가로 중앙 */
align-items: center; /* 세로 중앙 */
width: 100vw; /* 뷰포트 너비 */
height: 100vh; /* 뷰포트 높이 */
position: fixed;
top: 0;
left: 0;
background: rgba(0, 0, 0, 0.5);
}
.modal {
width: 400px;
padding: 20px;
background: white;
border-radius: 8px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}
</style>
<div class="modal-container">
<div class="modal">
<h2>로그인</h2>
<p>아이디와 비밀번호를 입력하세요</p>
</div>
</div>
결과: 모달이 화면 정중앙에 떠 있고, 배경은 반투명한 오버레이가 된다.
1. flex-direction이 row가 아닐 때 justify-content와 align-items 역할 바뀜
flex-direction: column으로 설정하면 주축이 세로가 되므로, justify-content가 세로 정렬을, align-items가 가로 정렬을 담당한다. 이를 모르고 justify-content를 쓰면 예상과 반대로 동작한다.
<!-- column 모드에서의 정렬 -->
<style>
.vertical-container {
display: flex;
flex-direction: column;
justify-content: center; /* 세로 중앙 (column 모드) */
align-items: center; /* 가로 중앙 (column 모드) */
height: 300px;
}
</style>
2. 컨테이너 크기를 명시하지 않으면 정렬이 보이지 않음
flex 컨테이너가 콘텐츠 크기에만 맞춰지면, justify-content와 align-items는 아무 효과가 없다.
3. gap과 margin을 함께 쓰면 중복 간격 발생
gap을 쓰면서 동시에 아이템에 margin을 주면, 간격이 두 배가 되거나 예상과 다르게 나타난다.