为什么 Bootstrap 类 在某些地方不起作用?

Why Bootstrap classes don't work in some places?

我试图将 h1 class 添加到跨度内的 header 文本也将其水平居中对齐但是 bootstrap classes text-center 和 h1不管用。后来我发现 fs-1 和 mx-auto 有效。这是什么原因造成的? h1元素只能在p标签内使用吗?

<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

    <title>Hello, world!</title>
  </head>
  <body>


    <div class="container">
            <nav class="navbar navbar-light mt-3 rounded border border-3 border-dark " style="background-color: black;">
                <div class="container-fluid text-center ">
                <span class="navbar-brand mb-0 h1 text-center" style="color: white; cursive;">Header</span>
                </div>
            </nav>
     </div>

   


    <!-- Option 1: Bootstrap Bundle with Popper -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
  </body>
</html>

您需要从 span 中删除 navbar-brand,因为它会覆盖 H1 的字体大小。您的 span 还需要一个 w-100 的 class,因为它位于弹性盒子中,并且会自动采用尽可能小的尺寸。或者,您可以将 class justify-content-center 添加到您的容器中,而不是将 w-100 添加到您的 span

<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

    <title>Hello, world!</title>
  </head>
  <body>


    <div class="container">
            <nav class="navbar navbar-light mt-3 rounded border border-3 border-dark " style="background-color: black;">
                <div class="container-fluid text-center">
                <span class="mb-0 h1 w-100" style="color: white; cursive;">Header</span>
                </div>
            </nav>
     </div>

   


    <!-- Option 1: Bootstrap Bundle with Popper -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
  </body>
</html>