如何将 bootstrap 行居中

how to center a bootstrap row

我正在尝试将此温度转换器居中,但我做不到。列中的所有内容似乎都是可靠的,但行不是。我在下面附上了一张图片。

这是我的代码。

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Temperature convertor</title>

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


  <style>
    .content {
      padding-top: 30px;
      background-color: #eeeeee;
    }
    
    body {
      background: linear-gradient(to left, black, purple);
    }
  </style>


</head>

<body>



  <div class="container">
    <div class="row row-sm-offset-5">
      <div class="col-sm-3 text-center content">

        <h2>
          Temperature convertor
        </h2>

        <form action="">
          <div class="form-group">
            <label for="userInput"> Enter the temperature:  </label>
            <input type="number" id="userInput">

          </div>

          <div class="form-group" style="margin-top: 10px;">

            <input type="button" value="To Celsius" onclick="toCelsius()">
            <input type="button" value="To Fahrenheit" onclick="toFahrenheit()">
          </div>
        </form>

        <p id="result"> </p>
      </div>

    </div>
  </div>




  <!-- javascript file placed right before closing tag to reduce log times caused by script -->
  <script src="alert.js"></script>
</body>

</html>

请让我知道我做错了什么。

您可以在行元素

中使用justify-content-centerclass

以下代码可能对您有所帮助,

 .content {
        padding-top: 30px ;
        background-color: #eeeeee;
        width: 100%;
        margin:auto 0;
    }
<div class="container">
    <div class="row justify-content-center">
      <div class="col-sm-3 text-center content">

        <h2>
          Temperature convertor
        </h2>

        <form action="">
          <div class="form-group">
            <label for="userInput"> Enter the temperature:  </label>
            <input type="number" id="userInput">

          </div>

          <div class="form-group" style="margin-top: 10px;">

            <input type="button" value="To Celsius" onclick="toCelsius()">
            <input type="button" value="To Fahrenheit" onclick="toFahrenheit()">
          </div>
        </form>

        <p id="result"> </p>
      </div>

    </div>
  </div>

希望您不需要将行居中。您可以使用 mx-auto 将 Col 在 Row 中居中。这将在左侧和右侧创建自动边距,并且所有内容都将完美居中。

<div class="row">
   <div class="col-sm-3 content mx-auto">
       <!-- Your form -->
   </div>
</div>