主頁
CSS
CSS 對齊
Tryit: 使用 position 和 transform 進行居中
執行 ❯
建立您
自己的
網站
×
更改方向
儲存程式碼
更改主題,深色/淺色
前往 Spaces
<!DOCTYPE html> <html> <head> <style> .center { height: 200px; position: relative; border: 3px solid green; } .center p { margin: 0; position: absolute; top: 50%; left: 50%; -ms-transform: translate(-50%, -50%); transform: translate(-50%, -50%); } </style> </head> <body> <h2>Center with position and transform</h2> <p>In this example, we use positioning and the transform property to vertically and horizontally center the div element:</p> <div class="center"> <p>I am vertically and horizontally centered.</p> </div> </body> </html>