html 引导形式导致溢出

fbcarpbf  于 2023-04-27  发布在  其他
关注(0)|答案(2)|浏览(116)

**问题:**我试图建立一个注册表单,由于某种原因,如果里面有任何表单组,屏幕右侧会有轻微的溢出。
**已经完成:**我使用过Chromium开发工具,发现如果我删除每个.form-group,溢出就会消失。我还搜索了stack-overflow和Google,但都没有结果。
**当前代码:**这里是我的表单(所有的bootstrap也包含在cdn中)

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.2/css/all.css" integrity="sha384-oS3vJWv+0UjzBfQzYUhtDYW+Pj2yciDJxpsK1OYPAYjqT085Qq/1cq5FLXAZQ7Ay" crossorigin="anonymous">

    <div class="content">
        <div class="row">
            <div class="col-sm-12 col-md-3 col-lg-3 col-xl-4"></div>
            <div class="col-sm-12 col-md-6 col-lg-6 col-xl-4">
                <form class="">

                  <div class="form-group">
                    <label for="email">Email address</label>
                    <input type="email" class="form-control" id="email" name="email" placeholder="Enter email">
                  </div>

                  <div class="form-group">
                    <label for="username">Username</label>
                    <input type="text" class="form-control" id="username" name="username" placeholder="Enter username">
                  </div>

                  <div class="row">
                    <div class="col-sm-6 col-md-6 col-lg-6 col-xl-6">
                            <div class="form-group">
                            <label for="password">Password</label>
                            <input type="password" class="form-control" id="password" name="password" placeholder="Password">
                            </div>
                        </div>
                        <div class="col-sm-6 col-md-6 col-lg-6 col-xl-6">
                            <div class="form-group">
                                <label for="confirmpassword">Confirm Password</label>
                                <input type="password" class="form-control" id="confirmpassword" name="confirmpassword" placeholder="Confirm Password">
                            </div>
                        </div>
                    </div>
                    <div class="text-center">
                    <button type="submit" class="btn btn-primary">Submit</button>
                  </div>
                </form>
            </div>
        </div>

    </div>

.content是:

.content {
    padding-left: 10px;
    padding-right: 10px;
}

**预期结果:**我相信你已经从标题中猜到了,但是..

期望:没有溢出,和一个幸福的生活。
实际:轻度溢出和大量悲伤

vc6uscn9

vc6uscn91#

.content css中添加overflow: hidden;

.content {
 overflow: hidden;
 padding-left: 10px;
 padding-right: 10px;
}

演示如下:

.content {
  overflow: hidden;
  padding-left: 10px;
  padding-right: 10px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />

<div class="content">
  <div class="row">
    <div class="col-sm-12 col-md-3 col-lg-3 col-xl-4"></div>
    <div class="col-sm-12 col-md-6 col-lg-6 col-xl-4">
      <form class="">

        <div class="form-group">
          <label for="email">Email address</label>
          <input type="email" class="form-control" id="email" name="email" placeholder="Enter email">
        </div>

        <div class="form-group">
          <label for="username">Username</label>
          <input type="text" class="form-control" id="username" name="username" placeholder="Enter username">
        </div>

        <div class="row">
          <div class="col-sm-6 col-md-6 col-lg-6 col-xl-6">
            <div class="form-group">
              <label for="password">Password</label>
              <input type="password" class="form-control" id="password" name="password" placeholder="Password">
            </div>
          </div>
          <div class="col-sm-6 col-md-6 col-lg-6 col-xl-6">
            <div class="form-group">
              <label for="confirmpassword">Confirm Password</label>
              <input type="password" class="form-control" id="confirmpassword" name="confirmpassword" placeholder="Confirm Password">
            </div>
          </div>
        </div>
        <div class="text-center">
          <button type="submit" class="btn btn-primary">Submit</button>
        </div>
      </form>
    </div>
  </div>


</div>

或者,你可以使用bootstrap内置的.container类,而不是你自定义的类(我甚至建议在使用bootstrap时这样做,更多信息在这里),你甚至不需要应用overflow: hidden hack:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />

<div class="container">
  <div class="row">
    <div class="col-sm-12 col-md-3 col-lg-3 col-xl-4"></div>
    <div class="col-sm-12 col-md-6 col-lg-6 col-xl-4">
      <form class="">

        <div class="form-group">
          <label for="email">Email address</label>
          <input type="email" class="form-control" id="email" name="email" placeholder="Enter email">
        </div>

        <div class="form-group">
          <label for="username">Username</label>
          <input type="text" class="form-control" id="username" name="username" placeholder="Enter username">
        </div>

        <div class="row">
          <div class="col-sm-6 col-md-6 col-lg-6 col-xl-6">
            <div class="form-group">
              <label for="password">Password</label>
              <input type="password" class="form-control" id="password" name="password" placeholder="Password">
            </div>
          </div>
          <div class="col-sm-6 col-md-6 col-lg-6 col-xl-6">
            <div class="form-group">
              <label for="confirmpassword">Confirm Password</label>
              <input type="password" class="form-control" id="confirmpassword" name="confirmpassword" placeholder="Confirm Password">
            </div>
          </div>
        </div>
        <div class="text-center">
          <button type="submit" class="btn btn-primary">Submit</button>
        </div>
      </form>
    </div>
  </div>


</div>
yk9xbfzb

yk9xbfzb2#

对于任何在2023年遇到同样问题的人(使用bootstrap 5.2的行为仍在继续),以下是我如何解决它的方法。

  • 这个问题是由bootstrap css类“form-control”引起的,它的左右填充为.75rem,宽度为100%。所以输入框的一部分超出了右边的父div。
  • 解决方案:只需在输入标记中添加一个“px-0”(也可以是“px-1”)类。
<input
          type="text"
          class="form-control px-0"
          id="fieldId"
          aria-describedby="basicInfo"
        />
  • 您可能不想使用溢出隐藏,这可能会根据您的网格系统裁剪输入框的某些部分,并创建不必要的情况。

相关问题