在 LinkedIn 上追蹤我
在 Github.com 上關注我
點擊閱讀
沒有Boaring Setion,我們可以重定向到編碼!
Flexbox 是一款功能強大的佈局工具,可輕鬆地將元素水平和垂直居中。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Center Div with Flexbox</title> <style> .container { display: flex; justify-content: center; /* Horizontal center */ align-items: center; /* Vertical center */ height: 100vh; /* Full viewport height */ } .centered-div { width: 200px; height: 200px; background-color: lightblue; } </style> </head> <body> <div class="container"> <div class="centered-div">Centered with Flexbox</div> </div> </body> </html>
CSS Grid 是另一個強大的佈局系統,可以輕鬆地將元素居中。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Center Div with Grid</title> <style> .container { display: grid; place-items: center; /* Center both horizontally and vertically */ height: 100vh; /* Full viewport height */ } .centered-div { width: 200px; height: 200px; background-color: lightcoral; } </style> </head> <body> <div class="container"> <div class="centered-div">Centered with Grid</div> </div> </body> </html>
此方法涉及絕對定位 div 並使用變換將其居中。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Center Div with Absolute Positioning</title> <style> .container { position: relative; height: 100vh; /* Full viewport height */ } .centered-div { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 200px; height: 200px; background-color: lightgreen; } </style> </head> <body> <div class="container"> <div class="centered-div">Centered with Absolute Positioning</div> </div> </body> </html>
對指定寬度的元素設定 margin: auto 可以使其水平居中。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Center Div with Margin Auto</title> <style> .container { width: 100%; height: 100vh; /* Full viewport height */ display: flex; align-items: center; /* Vertical center */ } .centered-div { margin: 0 auto; /* Horizontal center */ width: 200px; height: 200px; background-color: lightcoral; } </style> </head> <body> <div class="container"> <div class="centered-div">Centered with Margin Auto</div> </div> </body> </html>
此方法使用 display: table 和 display: table-cell 將元素置中。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Center Div with Table Display</title> <style> .container { display: table; width: 100%; height: 100vh; /* Full viewport height */ } .centered-div { display: table-cell; vertical-align: middle; /* Vertical center */ text-align: center; /* Horizontal center */ } .inner-div { display: inline-block; width: 200px; height: 200px; background-color: lightpink; } </style> </head> <body> <div class="container"> <div class="centered-div"> <div class="inner-div">Centered with Table Display</div> </div> </div> </body> </html>
拜伊
快樂編碼!
以上是在 HTML 和 CSS 中使 Div 居中的不同方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!