如何使用 CSS 将按钮元素垂直和水平居中?

cssweb developmentfront end technology更新于 2025/12/8 1:16:17

要使用 CSS 将按钮垂直和水平居中,代码如下 −

示例

<!DOCTYPE html>
<html>
<head>
<style>
   body {
      font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
   }
   .centered {
      display: flex;
      justify-content: center;
      align-items: center;
      height: 200px;
      border: 3px solid rgb(0, 70, 128);
   }
   button{
      font-size: 18px;
      border: none;
      padding:10px;
      background-color: darkblue;
      color:white;
   }
</style>
</head>
<body>
<h1>Centering Example</h1>
<div class="centered">
<button>This button is centered</button>
</div>
</body>
</html>

输出

上述代码将产生以下输出 −


相关文章


有用资源