我怎样才能将一个 bootstrap column/form 置于屏幕的正中央?

How can i center one bootstrap column/form in the exact center of my screen?

我已经尝试了几个小时但找不到解决方案,我的问题是:

我正在尝试创建一个页面登录页面,其中登录表单在页面中水平和垂直居中。我正在使用 syncfusion blazor 并创建了容器全屏:

HTML:

<div class="control-section" style="min-height: 100vh !important; background-color: red !important;">
    
</div>

CSS:

body, html {
   margin: 0 !important;
   padding: 0 !important;
}

.container {
 max-width: 100% !important;
 width: 100% !important;
 height: 100% !important;
 padding-right: 0px !important;
 padding-left: 0px !important;
}

.pb-3 {
  padding-bottom: 0rem !important;
}

这有效,容器设置为全宽和全高,没有任何滚动条。

不过,我正在尝试在此页面中创建一列,该列在页面中心垂直和水平居中。

注意:我想保持容器的背景为红色,因为我想在 column/login 表单完成后在那里显示全屏图像。因此我认为我无法使用行?

到目前为止我尝试过的:

<div class="control-section" style="min-height: 100vh !important; background-color: red !important;">
     <div class="col-xs-12 col-sm-2 offset-5" style="background-color: blue !important;">Test</div>
</div>

这创建了一个宽度为 2 且偏移量为 5 的列,因此它在水平方向居中,但是我无法居中或垂直。任何人都可以帮我解决这个问题吗?

更新 回答后

我使用了答案的 centerArea 代码并将表格放在我的行中:

.HTML:

<div class="control-section centerArea">
    <div class="col-xs-12 col-sm-2" style="background-color: blue !important;">
        <section>
            <form id="account" method="post">
                <hr />
                <div asp-validation-summary="All" class="text-danger"></div>
                <div class="form-group" style="margin-top: 150px !important;">
                    <label asp-for="Input.Email"></label>
                    <input asp-for="Input.Email" class="form-control" />
                    <span asp-validation-for="Input.Email" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="Input.Password"></label>
                    <input asp-for="Input.Password" class="form-control" />
                    <span asp-validation-for="Input.Password" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <div class="checkbox d-inline-block">
                        <label asp-for="Input.RememberMe">
                            <input asp-for="Input.RememberMe" />
                            @Html.DisplayNameFor(m => m.Input.RememberMe)
                        </label>
                    </div>
                    <div class="d-inline-block">
                        <a id="forgot-password" asp-page="./ForgotPassword">Password?</a>
                    </div>
                </div>
                <div class="form-group">
                    <button type="submit" class="btn btn-primary">Log in</button>
                </div>
                <div class="form-group">
                    <p>
                        <a id="forgot-password" asp-page="./ForgotPassword">Password?</a>
                    </p>
                    <p>
                        <a asp-page="./Register" asp-route-returnUrl="@Model.ReturnUrl">Register as a new user</a>
                    </p>
                    <p>
                        <a id="resend-confirmation" asp-page="./ResendEmailConfirmation">Resend email confirmation</a>
                    </p>
                </div>
            </form>
        </section>
    </div>
</div>

.CSS:

<style>
body, html {
    margin: 0 !important;
    padding: 0 !important;
}

.container {
    max-width: 100% !important;
    width: 100% !important;
    height: 100% !important;
    padding-right: 0px !important;
    padding-left: 0px !important;
}

.pb-3 {
    padding-bottom: 0rem !important;
}

.centerArea {
    min-height: 100vh !important;
    background-color: red !important;
    color: white;
    display: flex;
    justify-content: center;
}

img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.form-group {
    display: block !important;
    margin: 0 auto !important;
    max-width: 90% !important;
    margin-bottom: 1% !important;
}

.d-inline-block {
    display: inline-block !important;
}
</style>

这会输出以下内容:

如您所见,内容是水平居中但不是垂直居中?我正在尝试做这样的事情:

提前致谢

试试这个:

.html

<div class="control-section centerArea">
  <div class="col-xs-12 col-sm-2 offset-5 centerAreaChild">Test</div>
</div>

.css

.centerArea {
    margin: 0px;
    padding: 0px;
    border-width: 0px;
    background-color: blue;
    min-height: 800px;
    display: flex;
    align-items: center;
    justify-content: center;
}
.centerAreaChild {
    text-align: center;
    height: 300px;
    background-color: red;
    justify-content: center;
    align-items: center;
    color: white;
    width: 200px;
    display: flex;
}

Bootstrap唯一解:

将包装器div.control-section作为弹性容器,方法是给它d-flex class并将m-auto分配给.col-*元素.

d-flex, m-auto are Bootstrap classes. Also, I added Bootsrap's p-2 class to the wrapper div.control-section to have a little spacing if the viewport's height is less than the content height.

.control-section {
  min-height: 100vh;
  background: rgba(0, 0, 0, .4) url("https://images.unsplash.com/photo-1506765515384-028b60a970df") center no-repeat;
  background-size: cover;
  background-blend-mode: overlay;
}
<!-- importing Bootsrap (just for the demo) -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">

<!-- Your code -->
<div class="control-section d-flex p-2">
  <div class="col-11 col-md-8 col-lg-4 m-auto" style="background-color: blue !important;">
    <section>
      <form id="account" method="post">
        <hr />
        <div asp-validation-summary="All" class="text-danger"></div>
        <div class="form-group">
          <label asp-for="Input.Email"></label>
          <input asp-for="Input.Email" class="form-control" />
          <span asp-validation-for="Input.Email" class="text-danger"></span>
        </div>
        <div class="form-group">
          <label asp-for="Input.Password"></label>
          <input asp-for="Input.Password" class="form-control" />
          <span asp-validation-for="Input.Password" class="text-danger"></span>
        </div>
        <div class="form-group">
          <div class="checkbox d-inline-block">
            <label asp-for="Input.RememberMe">
                            <input asp-for="Input.RememberMe" />
                            @Html.DisplayNameFor(m => m.Input.RememberMe)
                        </label>
          </div>
          <div class="d-inline-block">
            <a id="forgot-password" asp-page="./ForgotPassword">Password?</a>
          </div>
        </div>
        <div class="form-group">
          <button type="submit" class="btn btn-primary">Log in</button>
        </div>
        <div class="form-group">
          <p>
            <a id="forgot-password" asp-page="./ForgotPassword">Password?</a>
          </p>
          <p>
            <a asp-page="./Register" asp-route-returnUrl="@Model.ReturnUrl">Register as a new user</a>
          </p>
          <p>
            <a id="resend-confirmation" asp-page="./ResendEmailConfirmation">Resend email confirmation</a>
          </p>
        </div>
      </form>
    </section>
  </div>
</div>