이 게시물의 모든 코드는 Github 저장소에서 확인하실 수 있습니다.
여기서 수직 센터 - CodeSandbox 및 수평 센터에서 시각적인 내용을 확인할 수 있습니다.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>Centering</title> <link rel="stylesheet" href="styles.css" /> </head> <body> <p class="center-by-absolute-margin">Centering</p> <p class="center-by-pseudo-class">Centering</p> <p class="center-by-line-height">Centering</p> <p class="center-by-table">Centering</p> <div class="center-by-flex"> <p>Centering</p> </div> <div class="center-by-grid"> <p>Centering</p> </div> <div class="center-by-absolute-parent"> <p class="center-by-absolute-child">Centering</p> </div> <p class="center-by-calc">Centering</p> </body> </html>
.center-by-absolute-margin { position: absolute; top: 0; bottom: 0; margin: auto 0; } .center-by-pseudo-class::before { content: ""; display: inline-block; height: 100%; vertical-align: middle; } .center-by-line-height { height: 200px; line-height: 200; } .center-by-table { display: table-cell; vertical-align: middle; } .center-by-flex { display: flex; align-items: center; } .center-by-grid { display: grid; align-items: center; } .center-by-absolute-parent { position: relative; } .center-by-absolute-child { position: absolute; top: 50%; transform: translateY(-50%); } .center-by-calc { height: 70px; position: relative; top: calc(50% - 35px); }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>Centering</title> <link rel="stylesheet" href="styles.css" /> </head> <body> <p class="center-by-fixed-width">Centering</p> <p class="center-by-text-align">Centering</p> <div class="center-by-unfixed-width"> <span>Centering</span> </div> <p class="center-by-table">Centering</p> <div class="center-by-flex"> <p>Centering</p> </div> <div class="center-by-grid"> <p>Centering</p> </div> <div class="center-by-absolute-parent"> <p class="center-by-absolute-child">Centering</p> </div> <p class="center-by-calc">Centering</p> </body> </html>
.center-by-fixed-width { width: 70px; margin: 0 auto; } .center-by-text-align { text-align: center; } .center-by-unfixed-width { text-align: center; } .center-by-table { display: table; margin: 0 auto; } .center-by-flex { display: flex; justify-content: center; } .center-by-grid { display: grid; justify-content: center; } .center-by-absolute-parent { position: relative; } .center-by-absolute-child { position: absolute; left: 50%; transform: translateX(-50%); } .center-by-calc { width: 70px; margin-left: calc(50% - 35px); }
위 내용은 센터링 - CSS 문제의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!