在Bootstrap中居中一个表单无效。

3

我正在尝试将我的表单居中在一个 div 中,但我使用的方法不起作用。我尝试使用 Bootstrap 文档中提到的 justify-content-center 属性,但它没有居中。当我试图在 CSS 中应用 display: flexjustify-content: center 将其居中时,表单失去了方向并混乱了。我无法弄清楚如何将其居中,我错在哪里了。感谢任何帮助。提前致谢。

.onlineRefilFormCon {
  display: flex;
  justify-content: center;
  align-items: center;
  position: absolute;
  top: 50%;
  width: 100%;
  height: 90%;
}

.onlineRefillTitleCon {
  display: flex;
  justify-content: center;
  align-items: center;
  position: absolute;
  top: 0%;
  left: 0%;
  width: 100%;
  height: 10%;
}

.onlineRefillForm {
  position: absolute;
  top: 20%;
  width: 40%;
  height: 80%;
  background-color: white;
  box-shadow: rgba(0, 0, 0, 0.35) 0px 5px 15px;
}

.form-group {
  padding-top: 5%;
}

.col-form-label {
  font-weight: 600;
}

.form-check-input {
  border: 2px solid;
}

.form-control {
  width: 60%;
  border: 2px solid;
}

.textarea {
  margin-top: -10%;
  margin-left: 17%;
  border: 2px solid;
}

.radio1 {
  margin-top: 3%;
  margin-left: 20%;
}

.form-check-inline {
  padding: 0%;
}

.OnlineSubmitBtnCon {
  position: absolute;
  bottom: 7%;
  width: 100%;
  height: 10%;
}

.OnlineSubmitBtnCon button {
  margin-left: 35%;
}
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>


<div class="onlineRefilFormCon">
  <div class="onlineRefillTitleCon">
    <h2 class="onlineRefillTitle">Online Refill</h2>
  </div>
  <form class="onlineRefillForm" id="onlineRefillForm">
    <div class="form-group row justify-content-center">
      <label for="text-input" class="col-2 col-form-label">First Name</label>
      <div class="col-10">
        <input class="form-control" type="text" value="" id="text-input">
      </div>
    </div>
    <div class="form-group row justify-content-center">
      <label for="text-input" class="col-2 col-form-label">Last Name</label>
      <div class="col-10">
        <input class="form-control" type="text" value="" id="text-input">
      </div>
    </div>
    <div class="form-group row justify-content-center">
      <label for="date-input" class="col-2 col-form-label">Date</label>
      <div class="col-10">
        <input class="form-control" type="date" value="yyyy-mm-dd" id="date-input">
      </div>
    </div>
    <div class="form-group row justify-content-center">
      <label for="email-input" class="col-2 col-form-label">Email</label>
      <div class="col-10">
        <input class="form-control" type="email" value="" id="email-input">
      </div>
    </div>
    <div class="form-check-inline radio1">
      <label class="form-check-label">
                    <input type="radio" class="form-check-input" name="optionsRadios" id="optionsRadios1"
                        value="option1" checked>
                    Pickup
                </label>
    </div>
    <div class="form-check-inline">
      <label class="form-check-label">
                    <input type="radio" class="form-check-input" name="optionsRadios" id="optionsRadios2"
                        value="option2">
                    Delivery
                </label>
    </div>
    <div class="form-check-inline">
      <label class="form-check-label">
                    <input type="radio" class="form-check-input" name="optionsRadios" id="optionsRadios2"
                        value="option2">
                    Mail
                </label>
    </div>
    <div class="form-group justify-content-center">
      <label for="textarea" class="col-2 col-form-label">Refill Details</label>
      <textarea class="form-control textarea" rows="7" placeholder="Example: Lisinopril"></textarea>
    </div>
    <div class="OnlineSubmitBtnCon">
      <button type="button" class="btn btn-primary contactUsSubmitBtn" onclick="proccessContactMessage()">Submit</button>
    </div>
  </form>
</div>

3个回答

2
如果您想将表单元素居中对齐,应使用 text-align:center 样式。因此,您应该在要居中对齐的元素中添加 text-center bootstrap 类。

这对您有用吗?

enter image description here


我已经更新了我的答案,请试一试并让我知道。 - Cardoso
请给我展示一下你的界面截图。 - Cardoso
我觉得你误解了我的意思,表单需要居中。 - seriously
那么,你的意思是“表单需要居中显示”? - Cardoso
我已经更新了答案。请检查一下是否适用于你。 - Cardoso
显示剩余5条评论

1
应用样式。
margin-left:auto;margin-right:auto;

也将为以后的参考居中任何内容


1
有没有可能是因为引用bootstrap样式的链接失效了?当我尝试示例代码时,链接失败了,所以我上传了一个新的CDN链接,表单就居中显示了。

.onlineRefilFormCon {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  width: 100%;
  font-weight: 600;
}

.onlineRefillForm {
  display: flex;
  flex-direction: column;
  width: 40%;
  padding: 48px;
  background-color: white;
  box-shadow: rgba(0, 0, 0, 0.35) 0px 5px 15px;
}

.form-group {
  padding-top: 5%;
}

.form-check-input {
  border: 2px solid;
}

.form-control {
  width: 60%;
  border: 2px solid;
}

.textarea {
  margin-top: -10%;
  margin-left: 17%;
  border: 2px solid;
}

.radio1 {
  margin-top: 3%;
  margin-left: 20%;
}

.form-check-inline {
  padding: 0%;
}
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/js/bootstrap.min.js"></script>
<div class="onlineRefilFormCon">
  <form class="onlineRefillForm" id="onlineRefillForm">
    <label for="text-input">First Name </label>
    <input type="text" value="" id="text-input" />
    <br>
    <label for="text-input">Last Name</label>
    <input type="text" value="" id="text-input" />
    <br>
    <label for="date-input">Date</label>
    <input type="date" value="yyyy-mm-dd" id="date-input" />
    <br>
    <label for="email-input">Email</label>
    <input type="email" value="" id="email-input" />
    <br>
    <label>Pickup
    <input type="radio" name="optionsRadios" id="optionsRadios1" value="option1" checked /></label>
    <br>
    <label>Delivery
    <input type="radio" name="optionsRadios" id="optionsRadios2" value="option2" /></label>
    <br>
    <label>Mail
    <input type="radio" name="optionsRadios" id="optionsRadios2" value="option2" /></label>
    <br>
    <label for="textarea" class="col-2 col-form-label">Refill Details</label>
    <textarea rows="7" placeholder="Example: Lisinopril"></textarea>
    <br>
    <button type="button" class="btn btn-primary contactUsSubmitBtn" onclick="proccessContactMessage()">
      Submit
    </button>
  </form>
</div>


仍未居中。 - seriously
那里有很多事情要处理,我认为我刚刚做的修改并没有完全回答你的问题,但是简化方法的优缺点值得一试。简化HTML结构可能有助于发现冲突的CSS声明。 - Ross Moody

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接