How to center a button element vertically and horizontally with CSS?

CSSWeb DevelopmentFront End Technology

To center a button vertically and horizontally with CSS, the code is as follows −

Example

 Live Demo

<!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>

Output

The above code will produce the following output −

raja
Published on 08-May-2020 17:38:13
Advertisements