在页面上居中 bootstrap 网格

Centering a bootstrap grid on page

我一直在尝试让 Bootstrap 网格在我的页面上垂直居中,但似乎无法使其居中。水平居中效果很好。

HTML:

<div class="container-fluid">
    <div class="board vh-100">
        <div class="row t-row justify-content-center">
            <div class="col-4 tic-box">A</div>
            <div class="col-4 tic-box">B</div>
            <div class="col-4 tic-box">C</div>
          </div>
          <div class="row t-row justify-content-center">
            <div class="col-4 tic-box">D</div>
            <div class="col-4 tic-box">E</div>
            <div class="col-4 tic-box">F</div>
          </div>
          <div class="row t-row justify-content-center">
            <div class="col-4 tic-box">G</div>
            <div class="col-4 tic-box">H</div>
            <div class="col-4 tic-box">I</div>
          </div>
    </div>
</div>

CSS:

.tic-box {
    border: solid black 1px;
    font-size: 3vw;
    text-align: center;
    width: 90px;
    height: 90px;
}

感谢您的帮助!

由于您的 board 元素具有特定高度 v-100,因此您可以将以下 css 属性添加到您的 board 元素以使其内容居中。

.board{
display:flex;
flex-direction: column;
justify-content:center;}

您好,我认为这是执行您要求的最干净的方法!

   .tic-box {
            border: solid black 1px;
            font-size: 3vw;
            text-align: center;
            width: 90px;
            height: 90px;
        }
<!doctype html>
<html lang="en">

<head>
    <title>Center</title>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS v5.0.2 -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">

</head>

<body>
    <div class="container-fluid d-flex align-items-center min-vh-100">
        <div class="board mx-auto">
            <div class="row t-row justify-content-center">
                <div class="col-4 tic-box">A</div>
                <div class="col-4 tic-box">B</div>
                <div class="col-4 tic-box">C</div>
            </div>
            <div class="row t-row justify-content-center">
                <div class="col-4 tic-box">D</div>
                <div class="col-4 tic-box">E</div>
                <div class="col-4 tic-box">F</div>
            </div>
            <div class="row t-row justify-content-center">
                <div class="col-4 tic-box">G</div>
                <div class="col-4 tic-box">H</div>
                <div class="col-4 tic-box">I</div>
            </div>
        </div>
    </div>
    <!-- Bootstrap JavaScript Libraries -->
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.min.js" integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous"></script>
</body>

</html>

我明白了 -

body{
        justify-content: center;
        align-items: center;
        display: flex;
      }
        .tic-box {
    border: solid black 1px;
    font-size: 3vw;
    text-align: center;
    width: 90px;
    height: 90px;
}
<div class="container-fluid">
        <div class="board vh-100">
            <div class="row t-row justify-content-center">
                <div class="col-4 tic-box">A</div>
                <div class="col-4 tic-box">B</div>
                <div class="col-4 tic-box">C</div>
              </div>
              <div class="row t-row justify-content-center">
                <div class="col-4 tic-box">D</div>
                <div class="col-4 tic-box">E</div>
                <div class="col-4 tic-box">F</div>
              </div>
              <div class="row t-row justify-content-center">
                <div class="col-4 tic-box">G</div>
                <div class="col-4 tic-box">H</div>
                <div class="col-4 tic-box">I</div>
              </div>
        </div>
    </div>