9. 중앙 정렬 -바닥부터 시작하는 CSS-
1. block 요소의 가로 중앙 정렬
margin: auto; 또는 margin: 0 auto;를 지정한다.
(좌우margin만 auto를 지정하면 된다.)
이때 전체 가로 길이보다 작게 width값을 꼭 준다.
예 9-1
- <html>
- <head>
- <style>
- .center {
- margin: auto;
- width: 60%;
- border: 3px solid #73AD21;
- padding: 10px;
- }
- </style>
- </head>
- <body>
- <h3>block요소의 가로 중앙 정렬</h3>
- <div class="center">
- <p>
- <b>알림:</b>IE8에서 margin:auto;를 사용 시 !DOCTYPE 을 선언하지 않으면 적용되지 않는다.</p>
- </div>
- </body>
- </html>
2. Text의 가로 중앙 정렬
앞서 공부했듯이 text-align: center;를 지정한다.
예 9-2
- <html>
- <head>
- <style>
- .center {
- text-align: center;
- border: 3px solid green;
- }
- </style>
- </head>
- <body>
- <h2>Text의 가로 중앙 정렬</h2>
- <div class="center">
- <p>This text is centered.</p>
- </div>
- </body>
- </html>
3. Image의 가로 중앙 정렬
<img>태그는 inline요소이므로 외부에 wrap을 씌우고, wrap요소의 text-align 속성에 center값을 주어 중앙 정렬을 시킨다.
예 9-3a
- <html>
- <head>
- <style>
- .wrap {
- text-align: center;
- }
- img {
- border: 1px solid;
- }
- </style>
- </head>
- <body>
- <h3>Image의 가로 중앙 정렬: wrap을 사용</h3>
- <div class="wrap">
- <img
- alt="black cat"
- src="http://cfs.tistory.com/custom/blog/246/2467441/skin/images/cat.image.jpg">
- </div>
- </body>
- </html>
<img>태그를 block요소로 바꾼 후 margin:auto;를 적용한다.
예 9-3b
- <html>
- <head>
- <style>
- img {
- display: block;
- margin: 0 auto;
- border: 1px solid;
- }
- </style>
- </head>
- <body>
- <h3>Image의 가로 중앙 정렬 - margin:auto;를 사용</h3>
- <img
- alt="black cat"
- src="http://cfs.tistory.com/custom/blog/246/2467441/skin/images/cat.image.jpg">
- </body>
- </html>
4. padding을 이용한 세로 중앙 정렬
Text가 들어갈 block요소에 상하 padding을 지정해 text가 중앙에 출력되게 한다.
딱히 마음에 드는 방법은 아님.
예 9-4
- <html>
- <head>
- <style>
- .center {
- padding: 70px 0;
- border: 3px solid green;
- }
- </style>
- </head>
- <body>
- <h3>padding을 이용한 세로 중앙 정렬</h3>
- <div class="center">
- <p>To climb steep hills requires slow pace at first.</p>
- </div>
- </body>
- </html>
이 방법에 text-align:center;를 적용하면 가로 세로 중앙 정렬이 된다.
예 9-5
- <html>
- <head>
- <style>
- .center {
- padding: 70px 0;
- border: 3px solid green;
- text-align: center;
- }
- </style>
- </head>
- <body>
- <h3>가로 세로 중앙 정렬</h3>
- <div class="center">
- <p>A rolling stone gathers no moss.</p>
- </div>
- </body>
- </html>
5. line-height을 이용한 세로 중앙 정렬
block요소의 height값과 line-height값을 같게 하면 그 안에 있는 inline, inline-block요소는 세로 중앙 정렬이 된다.
예 9-6
- <html>
- <head>
- <style>
- .center {
- line-height: 200px;
- height: 200px;
- border: 3px solid green;
- text-align: center;
- }
- /* 여러 줄의 text를 쓸 경우 아래 코드를 추가한다.*/
- .center p {
- line-height: 1.5;
- display: inline-block;
- vertical-align: middle;
- }
- </style>
- </head>
- <body>
- <h3>line-height를 이용한 세로 중앙 정렬</h3>
- <p>line-height 속성 값을 height 속성 값과 같게 주어 세로 중앙 정렬을 한다.</p>
- <div class="center">
- <p>Where there's a will, there's a way.</p>
- </div>
- </body>
- </html>
6. table-cell을 이용한 세로 중앙 정렬
block요소의 속성을 display: table-cell;로 변경한 뒤 vertical-align을 middle로 준다.
예 9-7
- <html>
- <head>
- <style>
- .center {
- width: 500px;
- height: 200px;
- display: table-cell;
- border: 3px solid green;
- vertical-align: middle;
- text-align: center;
- }
- </style>
- </head>
- <body>
- <h2>table-cell을 이용한 세로 중앙 정렬</h2>
- <div class="center">There is no royal road to learning.
- <div>There is no royal road to learning.</div>
- <div>There is no royal road to learning.</div>
- <p>There is no royal road to learning.</p>
- </div>
- </body>
- </html>
- <html>
- <head>
- <style>
- .wrap {
- display: table;
- margin: 0 auto;
- }
- .center {
- width: 500px;
- height: 200px;
- display: table-cell;
- border: 3px solid green;
- vertical-align: middle;
- text-align: center;
- }
- </style>
- </head>
- <body>
- <h3>table-cell을 이용한 가로 세로 중앙 정렬</h3>
- <div class="wrap">
- <div class="center">There is no royal road to learning.
- <div>There is no royal road to learning.</div>
- <div>There is no royal road to learning.</div>
- <p>There is no royal road to learning.</p>
- </div>
- </div>
- </body>
- </html>
'COMPUTER > CSS' 카테고리의 다른 글
10. table 관련 속성 -바닥부터 시작하는 CSS- (0) | 2017.01.09 |
---|---|
8. font 관련 속성들 -바닥부터 시작하는 CSS- (0) | 2017.01.07 |
7. text에 관련된 속성들 -바닥부터 시작하는 CSS- (0) | 2017.01.05 |
6. margin, padding, border 속성 -바닥부터 시작하는 CSS- (0) | 2016.12.30 |
5. position 속성 -바닥부터 시작하는 CSS- (0) | 2016.12.24 |