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

CSS justify-content: flex-start로 플렉스 항목을 컨테이너 시작 부분에 정렬하기

CSS 플렉스박스(Flexbox) 레이아웃에서 플렉스 항목(flex item)들을 컨테이너의 시작 부분에 정렬하려면 justify-content 속성에 flex-start 값을 지정하면 됩니다.

flex-start는 주축(main axis)의 시작점을 기준으로 항목들을 배치합니다. 가로 방향 레이아웃에서는 항목들이 왼쪽에 붙어 정렬되며, RTL(오른쪽에서 왼쪽) 언어 환경에서는 오른쪽에 정렬됩니다. 참고로 이 값은 justify-content의 기본값이기도 합니다.

예제

다음 코드를 실행하면 flex-start 값이 실제로 어떻게 적용되는지 확인할 수 있습니다.

<!DOCTYPE html>
<html>
   <head>
      <style>
         .mycontainer {
            display: flex;
            background-color: red;
            justify-content: flex-start;
         }
         .mycontainer > div {
            background-color: orange;
            text-align: center;
            line-height: 60px;
            font-size: 30px;
            width: 100px;
            margin: 5px;
         }
      </style>
   </head>
   <body>
      <h1>Result</h1>
      <div class = "mycontainer">
         <div>Rank1</div>
         <div>Rank2</div>
         <div>Rank3</div>
         <div>Rank4</div>
      </div>
   </body>
</html>

위 예제를 실행하면 빨간색 배경의 플렉스 컨테이너 안에 네 개의 주황색 항목(Rank1 ~ Rank4)이 왼쪽부터 차례대로 정렬되어 표시됩니다. 만약 flex-end, center, space-between 등 다른 값으로 바꾸면 항목들이 배치되는 위치가 달라지는 것을 비교해 볼 수 있습니다.