![](https://maz.kr/wp-content/uploads/2024/10/howto_css_center-vertical_thumbnail.png)
w3.css
요소를 가운데 정렬
CSS를 사용하여 요소를 수직 및 수평으로 중앙에 배치하는 방법을 알아보자.
수직으로 무엇이든 중앙에 배치하는 방법
1단계) HTML 추가
<style> .container { height: 200px; position: relative; border: 3px solid green; } .vertical-center { margin: 0; position: absolute; top: 50%; -ms-transform: translateY(-50%); transform: translateY(-50%); } </style> <div class="container"> <div class="vertical-center"> <p>I am vertically centered.</p> </div> </div>
기본 예시
예제 보기수직 및 수평으로 가운데 정렬하는 방법
예제
<style> .container { height: 200px; position: relative; border: 3px solid green; } .center { margin: 0; position: absolute; top: 50%; left: 50%; -ms-transform: translate(-50%, -50%); transform: translate(-50%, -50%); } </style> <div class="container"> <div class="center"> <p>I am vertically and horizontally centered.</p> </div> </div>
기본 예시
예제 보기
flexbox를 사용하여 사물을 중앙에 정렬할 수도 있다.
.center { display: flex; justify-content: center; align-items: center; height: 200px; border: 3px solid green; }
기본 예시
예제 보기참고
W3C School - Center Elements Vertically