front_end/css-sass

flexbox alignment 01 (justify-content)

joepasss 2022. 4. 6. 21:06

플렉스박스 안의 자식 요소를 정렬하는 방법

  • justify-content
  • align-items
  • align-content

mainAxis 상에서 자식 요소를 조작 => justify-content

crossAxis 상에서 자식 요소를 조작 => align-items, align-content

(Row (default)일 때 row 방향이 mainAxis, column 방향이 crossAxis)

mainAxis / crossAxis

Setup / apply 방법

플렉스박스 내 자식 요소들을 조작하는 property이니 부모 요소에 적용하면 된다

적용 예시

html 셋업

<div className='Container'>
      <ul>
        <li>1st Child</li>
        <li>2nd Child</li>
        <li>3rd Child</li>
        <li>4th Child</li>
      </ul>
    </div>

 

css 셋업

/*  flexbox container  */
ul {
  background-color: lightslategray;
  padding: 20px;
  height: 300px;
  display: flex;
  flex-wrap: wrap;
  
  justify-content: flex-start; /* alignment 적용부분 */
 }
 
 /*  flexbox child */
 li {
   list-style: none;
    color: white;
    background-color: #20b2aa;
    margin: 20px;
    padding: 10px;
    width: 100px;
    font-size: 20px;
    text-align: center;
    }

mainAxis가 row, crossAxis가 column인 상황 (flex-direction: row (default))에서 설명

 

justify-content

mainAxis 상에서 flexbox 자식 요소들을 조작함.

flex-start, flex-end, center, space-between, space-around 등이 있음

  • justify-content: flex-start;

flex-start

flex-start = 플렉스가 스타트하는 지점에서부터 정렬

 

  • justify-content: flex-end;

flex-end

flex가 끝나는 시점에서부터 정렬

 

  • justify-content: center;

center

mainAxis의 중앙으로 정렬

 

space-between, space-around, space-evenly 차이

space-between

각 요소 사이에 거리가 생긴다

space-around

요소를 둘러싼 형태로 거리가 생긴다

space-evenly

거리가 동일하게 조정된다

 


참고

alignment -- justify-content

justify-content

  • 1st
  • 2nd
  • 3rd
  • 4th