【问题标题】:Extra space at left and right of form element in bootstrap引导程序中表单元素左右两侧的额外空间
【发布时间】:2017-07-02 21:08:35
【问题描述】:

我正在为注册表单使用引导程序,但我想删除表单字段的左右两侧有多余的空间,我需要在表单输入字段结束后开始边框。

**HTML** **Bootstrap**

<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <title>Bootstrap 3 Registration Form with Validation</title>

  <link rel='stylesheet prefetch' href='http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css'>
  <link rel='stylesheet prefetch' href='http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css'>
  <link rel='stylesheet prefetch' href='http://cdnjs.cloudflare.com/ajax/libs/jquery.bootstrapvalidator/0.5.0/css/bootstrapValidator.min.css'>
  <link rel="stylesheet" href="css/style.css"> </head>
<style>
  .container {
    border: 1px solid black;
  }
</style>

<body>

  <div class="container">
    <form class="form-horizontal" action=" " method="post" id="contact_form" onsubmit="return FormValidation();">
      <fieldset>
        <!-- Form Name -->

        <center>
          <h2><b>Registration Form</b></h2>
        </center>

        <br>
        <!-- Text input-->
        <div class="form-group">
          <label class="col-md-4 control-label">First Name</label>
          <div class="col-md-4 inputGroupContainer">
            <div class="input-group">
              <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
              <input name="first_name" placeholder="First Name" id="first_name" class="form-control" type="text">
            </div>
          </div>
        </div>

        <!-- Success message -->
        <div class="alert alert-success" role="alert" id="success_message">Success <i class="glyphicon
glyphicon-thumbs-up"></i> Success!.</div>
        <!-- Button -->
        <div class="form-group">
          <label class="col-md-4 control-label"></label>
          <div class="col-md-4"><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <button type="submit" class="btn btn-warning">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SUBMIT <span class="glyphicon
glyphicon-send"></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</button>
          </div>
        </div>
      </fieldset>
    </form>

  </div>
  <!-- /.container -->

  <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
  <script src='http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js'></script>
  <script src='http://cdnjs.cloudflare.com/ajax/libs/bootstrap-validator/0.4.5/js/bootstrapvalidator.min.js'></script>
  <script src="js/index.js"></script>
</body>

</html>

I want the border asdescribed in image

更新了sn-p

.container { 边框:1px 纯黑色; } .fieldset { 填充:0px; }

              <center>
                 <h2><b>Registration Form</b></h2>
              </center>

           <br>
           <!-- Text input-->
           <div class="form-group">
              <label class="col-md-4 control-label">First Name</label>  
              <div class="col-md-4 inputGroupContainer">
                 <div class="input-group">
                    <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
                    <input  name="first_name" placeholder="First Name" id="first_name" class="form-control"  type="text">
                 </div>
              </div>
           </div>

【问题讨论】:

  • 老兄,你的代码搞砸了。你介意正确添加吗?显示一个 sn-p 会很棒...:)
  • 您的意思是“边框”还是“页面中心”,请澄清,因为图片描述性不够
  • #Mark 我的意思是表格应该在小盒子里而不是大盒子里。页面的位置相对于页面来说足够好,但我的要求是减少容器边框和文本输入表单元素的结尾。表单容器框边框的宽度应等于文本输入+名字。
  • @MarkSchultheiss 我的意思是表格应该在小盒子里而不是大盒子里。页面的位置相对于页面来说足够好,但我的要求是减少容器边框和文本输入表单元素的结尾。表单容器框边框的宽度应等于文本输入+名字。
  • @PraveenKumar 添加了 sn-p

标签: jquery html css twitter-bootstrap


【解决方案1】:

好的,首先,您添加类的方式是错误的。你应该有这样的结构:

  • 容器

它不在你的路上。所以,只需用这个替换你的代码。另外,你不为.container做边框,而是为form做边框:

form {
  border: 1px solid orange;
}
<link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css'>
<link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css'>

<div class="container">
  <div class="row">
    <div class="col-sm-4 col-sm-offset-4">
      <form class="form-horizontal" action=" " method="post" id="contact_form" onsubmit="return FormValidation();">
        <fieldset>
          <!-- Form Name -->
          <h2 class="text-center">Registration Form</h2>
          <!-- Text input-->
          <div class="form-group">
            <label class="col-md-4 control-label">First Name</label>
            <div class="col-md-4 inputGroupContainer">
              <div class="input-group">
                <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
                <input name="first_name" placeholder="First Name" id="first_name" class="form-control" type="text">
              </div>
            </div>
          </div>

          <!-- Success message -->
          <div class="alert alert-success" role="alert" id="success_message">Success <i class="glyphicon
glyphicon-thumbs-up"></i> Success!.</div>
          <!-- Button -->
          <div class="form-group">
            <label class="col-md-4 control-label"></label>
            <div class="col-md-4"><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
              <button type="submit" class="btn btn-warning">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SUBMIT <span class="glyphicon
  glyphicon-send"></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</button>
            </div>
          </div>
        </fieldset>
      </form>
    </div>
  </div>
</div>

注意事项:

  • &lt;b&gt; 不需要 &lt;hX&gt; 标签。
  • &lt;center&gt; 已弃用,请使用 class="text-center"
  • 将边框添加到form 而不是.container

【讨论】:

    【解决方案2】:

    将以下内容添加到您的 CSS:

    fieldset { padding: 0; }
    

    【讨论】:

    • 那么很可能是由于特异性。您可以从浏览器发布字段集的 CSS 吗?