首页
CSS
CSS 对齐
Tryit:使用定位和变换进行居中
运行 ❯
获取您
自己的
网站
×
更改方向
保存代码
更改主题,深色/浅色
前往 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>