[CSS] Flexbox

조애옹·2025년 6월 11일

🔍 Flexbox란?

  • Flexible Box Layout의 의미로, 요소들을 일렬로 정렬하고 배치하는 방법
  • 주로 메뉴, 그리드, 반응형 레이아웃 등에 자주 사용

✅ 컨테이너 주요 속성

속성값 예시설명
displayflex / inline-flexFlexbox를 활성화함
flex-directionrow / column / row-reverse / column-reverse주축 방향 설정
flex-wrapnowrap / wrap / wrap-reverse줄바꿈 여부 설정
justify-contentflex-start / center / flex-end / space-between / space-around / space-evenly주축(main axis) 정렬
align-itemsstretch / flex-start / center / flex-end / baseline교차축(cross axis) 정렬
align-contentstretch / flex-start / center / flex-end / space-between / space-around다줄 정렬 (줄이 여러 줄일 때 사용)
gapgap: 1rem아이템 간 간격

✅ 아이템 주요 속성

속성값 예시설명
flex-grow0 / 1 / ...남는 공간을 얼마나 채울지
flex-shrink0 / 1 / ...공간 부족할 때 얼마나 줄어들지
flex-basisauto / 200px기본 크기 지정
flex1 (1 1 0%) / auto / none위 세 속성의 shorthand
align-selfauto / flex-start / center / flex-end / stretch개별 아이템 정렬 방식
order숫자 (예: 0, 1, -1)아이템 순서 재정렬(기본값은 0)

✅ Tailwind CSS에서 활용 비교

목적기본 CSSTailwind CSS
Flex 컨테이너 선언display: flex;flex
수평 가운데 정렬justify-content: center;justify-center
수직 가운데 정렬align-items: center;items-center
요소 사이 여백 분산justify-content: space-between;justify-between
요소를 아래로 정렬align-items: flex-end;items-end
열 방향 배치flex-direction: column;flex-col
줄바꿈 허용flex-wrap: wrap;flex-wrap
요소 반대 방향 배치flex-direction: row-reverse;flex-row-reverse
개별 아이템 정렬align-self: flex-start;self-start
요소가 남은 공간 채우기flex-grow: 1;grow
요소가 줄어들 수 있게flex-shrink: 1;shrink
기본 크기 설정flex-basis: 200px;basis-[200px] 또는 커스텀

💻 예제: 간단한 메뉴 구현

https://codepen.io/joyewon0705/pen/dPodOQG?editors=1010


💡 TIPS

profile
아자잣

0개의 댓글