FE 개발자의 성장 기록👩‍💻

Container 속성 - justify-content, align-items, align-content 본문

HTML&CSS

Container 속성 - justify-content, align-items, align-content

yeeebin 2022. 10. 19. 20:36

 

justify-content, align-items, align-content는 컨테이너 속성이다. 

세 속성의 차이점을 알아보자.

 

기본 세팅

<body>
    <div class="container">
        <div class="item">one</div>
        <div class="item">two</div>
        <div class="item">three</div>
        <div class="item">four</div>
    </div>
</body>
.container{
    border: 5px dashed orange;
    width: 300px;
    height: 200px;
    display: flex;
    /*
    justify-content: flex-start;
    justify-content: flex-end;
    justify-content: center;
    justify-content: space-around;
    justify-content: space-between;
    */
}

.item{
    border: 2px solid pink;
    background-color: lemonchiffon;
    width: 50px;
    height: 50px;
    margin: 3px;
    font-weight: 700;
}

기본 세팅 결과

justify-content

: flex 컨테이너의 main 축 ( 주축 )을 기준으로 하는 정렬방법을 설정

현재 주축은 row ( 기본값 )이다. 따라서 가로 방향을 기준으로 정렬된다.

 

justify-content : flex-start; ( 기본값 )

→ 주축의 시작지점에 자식요소들이 정렬된다.

 

justify-content : flex-end;

→ 주축의 마지막지점에 자식요소들이 정렬된다.

 

justify-content : center;

→ 주축의 가운데 지점에 자식요소들이 정렬된다.

 

justify-content : space-around;

→ 모든 요소들이 주축 방향으로 정렬되며, 모든 요소들은 양쪽(주축 방향) 여백을 동일하게 차지한다.

 

justify-content : space-between;

→ 첫 요소와 마지막 요소는 부모 요소의 양 옆(주축 방향)에 여백없이 위치하며, 나머지 요소들은 양 옆 여백을 동일하게 차지하며 위치한다. 


align-items & align-content

align-items와 align-content의 차이를 더 확실히 보기 위해 자식 요소를 2개 더 추가하고 flex-wrap을 wrap으로 설정하여 두 줄로 나타내보았다.

.container{
    border: 5px dashed orange;
    width: 300px;
    height: 200px;
    display: flex;
    flex-wrap: wrap;
    /*
    align-items: flex-start;
    align-items: flex-end;
    align-items: center;
    */
    /*
    align-content: flex-start;
    align-content: flex-end;
    align-content: end;
    align-content: space-around;
    align-content: space-between;
    */
}

.item{
    border: 2px solid pink;
    background-color: lemonchiffon;
    width: 50px;
    height: 50px;
    margin: 3px;
    font-weight: 700;
}

 

align-items: flex-start / align-content: normal ( 기본 값 )

부모 요소는 위 그림처럼 자신의 영역을 위 아래로 반 나누어(보라색 박스, 초록색 박스) 자식 요소들을 정렬시킨다. 1~5번 요소들은 보라색 박스의 상단 부분에, 6번 요소는 초록색 박스의 상단 부분에 위치한 것을 확인할 수 있다.  

 

여기서 align-items각 영역 내에서 cross 축 ( column )을 기준으로 자식 요소들의 위치를 조절한다. 

 

align-items: flex-start;  

→ 각 영역 내에서 cross축의 시작지점부터 자식요소들이 정렬된다. ( 즉, 보라색 박스, 초록색 박스 내에서 cross 축 방향 시작점 부터 정렬 )

 

align-items: flex-end;

→각 영역 내에서 cross축의 마지막지점에 자식요소들이 정렬된다.

 

align-items: center;

→ 각 영역 내에서 cross축의 중간지점에 자식요소들이 정렬된다.


align-items는 각 구역 내에서의 정렬 방법을 설정하는 속성이었다면, align-content는 자식요소들을 하나로 모아서 위치시키는 방법을 설정하는 속성이다. 

 

align-content: flex-start;

→ 모든 자식요소들이 cross축의 시작지점에 정렬된다.

 

align-content: flex-end;

→ 모든 자식요소들이 cross축의 마지막지점에 정렬된다.

 

align-content: center;

→ 모든 자식요소들이 cross축의 중간지점에 정렬된다.

 

align-content: space-around;

→ 모든 자식요소들이 위 아래(cross 축 방향)에 동일한 크기의 여백을 가지며 정렬된다. 

 

align-content: space-between;

→ ( cross 축 방향으로 )맨 위와 맨 아래는 여백 없이 위치하고 그 사이의 여백은 모두 동일한 크기를 가지며 정렬된다. 

Comments