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

CSS align-content 속성의 stretch 값 활용 방법

CSS에서 align-content 속성에 stretch 값을 사용하면 플렉스 컨테이너 내부의 플렉스 라인(flex lines)이 남은 공간을 채우도록 늘어납니다.

이 값은 flex-wrap: wrap;과 함께 사용할 때 특히 유용하며, 여러 줄로 배치된 아이템들이 컨테이너의 세로 방향 여백을 균등하게 분배받아 확장됩니다.

예제 코드

다음 코드를 실행하여 stretch 값이 실제로 어떻게 동작하는지 확인해 보세요.

<!DOCTYPE html>
<html>
   <head>
      <style>
         .mycontainer {
            display: flex;
            height: 200px;
            background-color: red;
            align-content: stretch;
            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>

코드 설명

위 예제에서 .mycontainer 클래스는 display: flexflex-wrap: wrap을 설정하여 아이템들이 여러 줄로 배치될 수 있도록 합니다. 컨테이너의 높이는 200px로 지정되어 있으며, align-content: stretch 덕분에 각 플렉스 라인이 이 높이를 균등하게 나누어 차지하게 됩니다.

참고로 align-content 속성은 한 줄만 있는 경우에는 효과가 없으며, 반드시 두 줄 이상으로 감싸기(wrap)가 발생해야 stretch 효과를 확인할 수 있습니다.