Vertically Center Align Text In Screen Using Bootstrap 5

HongHong
1 min read
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Vertically Centered Text</title>
    <!-- Bootstrap 5 CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <div class="container-fluid vh-100 d-flex justify-content-center align-items-center">
        <h1 class="text-center">This text is vertically centered</h1>
    </div>

    <!-- Bootstrap 5 JS (optional, only needed if you're using Bootstrap's JavaScript components) -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
  • container-fluid: Creates a full-width container.

  • vh-100: Sets the height of the container to 100% of the viewport height.

  • d-flex: Applies display: flex to the container.

  • justify-content-center: Centers the content horizontally within the flex container.

  • align-items-center: Centers the content vertically within the flex container.

0
Subscribe to my newsletter

Read articles from Hong directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Hong
Hong

I am a developer from Malaysia. I work with PHP most of the time, recently I fell in love with Go. When I am not working, I will be ballroom dancing :-)