在 CSS 中垂直居中元素

cssweb developmentfront end technology更新于 2024/4/24 7:40:17

要在 CSS 中垂直居中元素,请使用顶部和底部填充。

示例

您可以尝试运行以下代码来使元素居中

<!DOCTYPE html>
<html>
   <head>
      <style>
         .center {
            padding: 50px 0;
            border: 2px solid blue;
         }
      </style>
   </head>
   <body>
      <h2>Center Vertically</h2>
      <p>In this example, we use the padding property to center the div element vertically:</p>
      <div class = "center">
         <p>I am vertically centered.</p>
      </div>
   </body>
</html>

输出


相关文章


有用资源