📝

CSS 중앙 정렬

 
💡
Tip : 수직 정렬은 높이가 있으면 수월하게 할 수 있습니다!

1. transform을 이용한 중앙 정렬

<div class="parent"> <div class="child">Centered content</div> </div>
.parent { border: 1px solid #9C27B0; height: 250px; position: relative; width: 250px; } .child { left: 50%; position: absolute; top: 50%; transform: translate(-50%, -50%); text-align: center; }
 

2. contents 중앙 정렬

<div class="container"> <div class="center"><span>Centered content</span></div> </div>
.container { border: 1px solid #9C27B0; height: 250px; width: 250px; } .center { display: table; height: 100%; width: 100%; } .center > span { display: table-cell; text-align: center; vertical-align: middle; }
 

3. flex를 이용한 contents 중앙 정렬

<div class="flexbox-centering"> <div>Centered content.</div> </div>
.flexbox-centering { display: flex; justify-content: center; align-items: center; height: 100px; }