CSS3에서 Flex 컨테이너의 교차 축(cross axis)을 따라 Flex 항목을 정렬하려면 align-items 속성을 사용합니다. 교차 축은 주 축(main axis)에 수직인 방향으로, 기본값인 flex-direction: row 상태에서는 세로 방향이 됩니다.
align-items 속성에는 stretch(기본값), center, flex-start, flex-end, baseline 등의 값을 지정할 수 있으며, 이 예제에서는 center를 사용해 항목들을 교차 축의 중앙에 정렬합니다.
예제
<!DOCTYPE html>
<html>
<head>
<style>
body {
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
}
.container {
height: 200px;
display: flex;
width: 100%;
align-items: center;
border: 2px solid red;
}
div {
width: 100px;
height: 100px;
color: white;
text-align: center;
font-size: 30px;
}
.first {
background-color: rgb(55, 0, 255);
}
.second {
background-color: red;
}
.third {
background-color: rgb(140, 0, 255);
}
</style>
</head>
<body>
<h1>Align flex items along cross axis</h1>
<div class="container">
<div class="first">First Div</div>
<div class="second">Second Div</div>
<div class="third">Third Div</div>
</div>
</body>
</html>출력 결과
위 코드를 실행하면 다음과 같은 결과가 출력됩니다 −

컨테이너에 display: flex와 align-items: center가 적용되어, 빨간색 테두리 안에서 세 개의 div 요소가 세로 방향(교차 축) 기준 중앙에 정렬된 것을 확인할 수 있습니다.