Computer >> 컴퓨터 >  >> 프로그래밍 >> CSS

CSS align-content 속성의 center 값으로 플렉스 라인 중앙 정렬하기

CSS의 align-content 속성에 center 값을 지정하면, 플렉스 컨테이너 내부의 여러 줄(플렉스 라인)이 교차축(cross axis) 기준으로 컨테이너의 중앙에 배치됩니다.

이 속성은 flex-wrap: wrap;처럼 플렉스 항목이 여러 줄로 나뉘는 경우에만 효과가 있으며, 한 줄만 존재하는 경우에는 시각적인 변화가 없습니다.

align-content: center 예제

아래 코드를 실행하면 빨간색 배경의 컨테이너 안에서 노란색 박스들이 세로 방향으로 중앙 정렬되는 것을 확인할 수 있습니다.

<!DOCTYPE html>
<html>
   <head>
      <style>
         .mycontainer {
            display: flex;
            height: 200px;
            background-color: red;
            align-content: center;
            flex-wrap: wrap;
         }
         .mycontainer > div {
            background-color: yellow;
            text-align: center;
            line-height: 60px;
            font-size: 30px;
            width: 100px;
            margin: 5px;
         }
      </style>
   </head>
   <body>
      <h1>Queue</h1>
      <div class = "mycontainer">
         <div>Q1</div>
         <div>Q2</div>
         <div>Q3</div>
         <div>Q4</div>
         <div>Q5</div>
         <div>Q6</div>
         <div>Q7</div>
         <div>Q8</div>
      </div>
   </body>
</html>

코드 설명

  • display: flex; — 해당 요소를 플렉스 컨테이너로 지정합니다.
  • flex-wrap: wrap; — 항목들이 한 줄에 다 들어가지 못할 때 자동으로 여러 줄로 감싸지도록 설정합니다.
  • align-content: center; — 여러 개의 플렉스 라인 전체를 컨테이너의 세로 중앙으로 모아 배치합니다.

참고로 align-content에서 사용할 수 있는 주요 값으로는 flex-start(시작점 정렬), flex-end(끝점 정렬), center(중앙 정렬), space-between, space-around, stretch(기본값) 등이 있습니다.