How to center an element vertically and horizontally with CSS?

CSSWeb DevelopmentFront End Technology

To center an element 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);
   }
</style>
</head>
<body>
<h1>Centering Example</h1>
<div class="centered">
<h2>This text is centered vertically and horizontally both</h2>
</div>
</body>
</html>

Output

The above code will produce the following output −

raja
Published on 08-May-2020 14:06:05
Advertisements