gpt4 book ai didi

html - Bootstrap 表单间距已关闭

转载 作者:太空宇宙 更新时间:2023-11-04 00:59:05 25 4
gpt4 key购买 nike

我正在使用 bootstrap 表单元素,我想将所有表单元素居中。我已经尝试添加 margin: 0 auto !important 到表单控件类,但填充/边距仍然存在问题。输入字段的左右填充/边距仍然不均匀。另外,文本区域和按钮固定在左侧。牢记响应式设计,解决此问题的最佳方法是什么?

enter image description here

HTML:

   <form class="form">
<h4 class="form-contactme">Contact Me</h4>
<div class="form-row">
<div class="form-group col-sm-6">
<label for="firstName" class="form-inputlabel">First Name</label>
<input type="text" class="form-control" id="firstName" placeholder="First Name">
</div>
<div class="form-group col-sm-6">
<label for="lastName" class="form-inputlabel">Last Name</label>
<input type="text" class="form-control" id="lastName" placeholder="Last Name">
</div>
</div>
<div class="form-row">
<div class="form-group col-sm-6">
<label for="email" class="form-inputlabel">Email</label>
<input type="email" class="form-control" id="email" placeholder="you@example.com">
</div>
<div class="form-group col-sm-6">
<label for="phone" class="form-inputlabel">Phone</label>
<input type="number" class="form-control" id="phone" placeholder="xxx-xxx-xxxx">
</div>
<div class="form-row">
<div class="form-group">
<label for="messageBox">Send me a message <i class="far fa-smile-beam"></i></label>
<textarea type="text" class="form-control" id="messageBox"></textarea>
</div>
</div>
<div class="form-row">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>

科学:

.form{
padding:3% !important;
background-color:#9fd199;
margin-bottom:5%;
&-contactme{
font-size:1.8rem;
font-weight:900;
text-align:center;
margin-bottom:15%;
}
&-row{
width:100%;
}
&-inputlabel{
display:none;
}
&-control{
margin-left:0 auto !important;
}

最佳答案

给容器添加 display flex,然后你可以使用 align-items: center 将所有元素对齐到中心

.form-group, .form-row {
display: flex;
flex-direction: column;
align-items: center;
}

.form {
padding: 3% !important;
background-color: #9fd199;
margin-bottom: 5%;
}

.form-group,
.form-row {
display: flex;
flex-direction: column;
align-items: center;
}

.form-contactme {
font-size: 1.8rem;
font-weight: 900;
text-align: center;
margin-bottom: 15%;
}

.form-row {
width: 100%;
}

.form-inputlabel {
display: none;
}

.form-control {
margin-left: 0 auto !important;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">

<form class="form">
<h4 class="form-contactme">Contact Me</h4>
<div class="form-row">
<div class="form-group col-sm-6">
<label for="firstName" class="form-inputlabel">First Name</label>
<input type="text" class="form-control" id="firstName" placeholder="First Name">
</div>
<div class="form-group col-sm-6">
<label for="lastName" class="form-inputlabel">Last Name</label>
<input type="text" class="form-control" id="lastName" placeholder="Last Name">
</div>
</div>
<div class="form-row">
<div class="form-group col-sm-6">
<label for="email" class="form-inputlabel">Email</label>
<input type="email" class="form-control" id="email" placeholder="you@example.com">
</div>
<div class="form-group col-sm-6">
<label for="phone" class="form-inputlabel">Phone</label>
<input type="number" class="form-control" id="phone" placeholder="xxx-xxx-xxxx">
</div>
<div class="form-row">
<div class="form-group">
<label for="messageBox">Send me a message <i class="far fa-smile-beam"></i></label>
<textarea type="text" class="form-control" id="messageBox"></textarea>
</div>
</div>
<div class="form-row">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>

关于html - Bootstrap 表单间距已关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53909453/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com