【问题标题】:Bootstrap 4 elements don't align when added div添加 div 时,Bootstrap 4 元素不对齐
【发布时间】:2021-02-01 04:57:29
【问题描述】:

它应该是什么样子

它看起来不像

所以我的目标是尝试让我的注册表单看起来像我的登录表单。我所做的只是添加了额外的字段,然后表单变得疯狂。谁能解释一下?我在互联网上搜索了帮助,但没有奏效,我只是真的试图让这个工作,因为它看起来不错。是的,我正在工作和学习 dJango,所以我只是希望它看起来不错,即使它甚至不需要。我有时会加倍努力,但我不明白时会讨厌。

这是我的索引 html 和登录表单

<!DOCTYPE html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1, shrink-to-fit=no"
    />

    <!-- Bootstrap CSS -->
    <link
      rel="stylesheet"
      href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css"
      integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2"
      crossorigin="anonymous"
    />

    {% load static %}
    <link rel="stylesheet" href="{% static 'styles.css' %}" />

    <title>Albert's Page</title>
  </head>
  <body>
    <section class="Form my-4 mx-5">
      <div class="container">
        <div class="row no-gutters">
          <div class="col-lg-5">
            <img
              src="../static/img/swordandshield.jpg"
              alt=""
              class="img-fluid"
            />
          </div>
          <div class="col-lg-7 px-5 pt-5">
            <h1 class="font-weight-bold py3">Welcome to My place</h1>
            <h4>Sign into your account</h4>
            <form action="" method="post">
              <div class="form-row">
                <div class="col-lg-7">
                  <h4>User Name</h4>
                  <input
                    type="userName"
                    name="userName"
                    class="form-control my-3 p-4"
                  />
                </div>
              </div>
              <div class="form-row">
                <div class="col-lg-7">
                  <h4>Password</h4>
                  <input
                    type="password"
                    name="pw"
                    class="form-control my-3 p-4"
                  />
                </div>
              </div>
              <div class="form-row">
                <div class="col-lg-7">
                  <button class="btn1 mt-3 mb-5" type="submit">Login</button>
                </div>
              </div>

              <p>
                Don't have an account yet?<a href="/register">Register here</a>
              </p>
            </form>
          </div>
        </div>
      </div>
    </section>

    <!-- Option 1: jQuery and Bootstrap Bundle (includes Popper) -->
    <script
      src="https://code.jquery.com/jquery-3.5.1.slim.min.js"
      integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj"
      crossorigin="anonymous"
    ></script>
    <script
      src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.bundle.min.js"
      integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx"
      crossorigin="anonymous"
    ></script>
  </body>
</html>

这是我的注册表,它使用相同的 css 和 boostrap 方法

<!DOCTYPE html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1, shrink-to-fit=no"
    />

    <!-- Bootstrap CSS -->
    <link
      rel="stylesheet"
      href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css"
      integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2"
      crossorigin="anonymous"
    />

    {% load static %}
    <link rel="stylesheet" href="{% static 'styles.css' %}" />

    <title>Albert's Page</title>
  </head>
  <body>
    <section class="Form my-4 mx-5">
      <div class="container">
        <div class="row no-gutters">
          <div class="col-lg-5">
            <img
              src="../static/img/swordandshield.jpg"
              alt=""
              class="img-fluid"
            />
          </div>
          <div class="col-lg-7 px-5 pt-5">
            <h1 class="font-weight-bold py3">Registration Form</h1>
            <h4>Sign up for an account</h4>
            <form action="" method="post">
              <div class="form-row">
                <div class="col-lg-7">
                  <h4>Your Full Name</h4>
                  <input
                    type="fullName"
                    name="fullName"
                    class="form-control my-3 p-4"
                  />
                </div>
                
                <div class="form-row">
                <div class="col-lg-7">
                  <h4>User Name</h4>
                  <input
                    type="userName"
                    name="userName"
                    class="form-control my-3 p-4"
                  />
                </div>
              </div>
              <div class="form-row">
                <div class="col-lg-7">
                  <h4>Password</h4>
                  <input
                    type="password"
                    name="pw"
                    class="form-control my-3 p-4"
                  />
                </div>
              </div>
              <div class="form-row">
                <div class="col-lg-7">
                  <h4>Confirm Password</h4>
                  <input
                    type="password"
                    name="cpw"
                    class="form-control my-3 p-4"
                  />
                </div>
              </div>
              <div class="form-row">
                <div class="col-lg-7">
                  <button class="btn1 mt-3 mb-5" type="submit">Register</button>
                </div>
              </div>

              <p>Have an account then <a href="/">Sign In</a></p>
            </form>
          </div>
        </div>
      </div>
    </section>

    <!-- Option 1: jQuery and Bootstrap Bundle (includes Popper) -->
    <script
      src="https://code.jquery.com/jquery-3.5.1.slim.min.js"
      integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj"
      crossorigin="anonymous"
    ></script>
    <script
      src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.bundle.min.js"
      integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx"
      crossorigin="anonymous"
    ></script>
  </body>
</html>

这是我的css

* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}

body {
  background: rgb(211, 218, 218);
}
.row {
  background: white;
  border-radius: 30px;
  box-shadow: 12px 12px 22px;
}
img {
  border-top-left-radius: 30px;
  border-bottom-left-radius: 30px;
}
.btn1 {
  border: none;
  outline: none;
  height: 50px;
  width: 100%;
  background-color: black;
  color: white;
  border-radius: 4px;
  font-weight: bold;
}
.btn1:hover {
  border: none;
  outline: none;
  height: 50px;
  width: 100%;
  background-color: rgb(51, 105, 38);
  color: white;
  border-radius: 4px;
  font-weight: bold;
}

【问题讨论】:

    标签: html css django twitter-bootstrap


    【解决方案1】:

    * {
      padding: 0;
      margin: 0;
      box-sizing: border-box;
    }
    
    body {
      background: rgb(211, 218, 218);
    }
    .row {
      background: white;
      border-radius: 30px;
      box-shadow: 12px 12px 22px;
    }
    img {
      border-top-left-radius: 30px;
      border-bottom-left-radius: 30px;
    }
    .btn1 {
      border: none;
      outline: none;
      height: 50px;
      width: 100%;
      background-color: black;
      color: white;
      border-radius: 4px;
      font-weight: bold;
    }
    .btn1:hover {
      border: none;
      outline: none;
      height: 50px;
      width: 100%;
      background-color: rgb(51, 105, 38);
      color: white;
      border-radius: 4px;
      font-weight: bold;
    }
    <!DOCTYPE html>
    <html lang="en">
      <head>
        <!-- Required meta tags -->
        <meta charset="utf-8" />
        <meta
          name="viewport"
          content="width=device-width, initial-scale=1, shrink-to-fit=no"
        />
    
        <!-- Bootstrap CSS -->
        <link
          rel="stylesheet"
          href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css"
          integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2"
          crossorigin="anonymous"
        />
    
        {% load static %}
        <link rel="stylesheet" href="{% static 'styles.css' %}" />
    
        <title>Albert's Page</title>
      </head>
      <body>
        <section class="Form my-4 mx-5">
          <div class="container">
            <div class="row no-gutters">
              <div class="col-lg-5">
                <img
                  src="https://images.unsplash.com/photo-1602900004353-2e786ce8f35c?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=658&q=80"
                  alt=""
                  class="img-fluid"
                />
              </div>
              <div class="col-lg-7 px-5 pt-5">
                <h1 class="font-weight-bold py3">Registration Form</h1>
                <h4>Sign up for an account</h4>
                <form action="" method="post">
                  <div class="form-row">
                    <div class="col-lg-7">
                      <h4>Your Full Name</h4>
                      <input
                        type="fullName"
                        name="fullName"
                        class="form-control my-3 p-4"
                      />
                    </div>
                  <!-- I added below </div> tag here. only one change and it works. -->
                  </div>
                  <!-- Change Ended. -->                    
                    <div class="form-row">
                    <div class="col-lg-7">
                      <h4>User Name</h4>
                      <input
                        type="userName"
                        name="userName"
                        class="form-control my-3 p-4"
                      />
                    </div>
                  </div>
                  <div class="form-row">
                    <div class="col-lg-7">
                      <h4>Password</h4>
                      <input
                        type="password"
                        name="pw"
                        class="form-control my-3 p-4"
                      />
                    </div>
                  </div>
                  <div class="form-row">
                    <div class="col-lg-7">
                      <h4>Confirm Password</h4>
                      <input
                        type="password"
                        name="cpw"
                        class="form-control my-3 p-4"
                      />
                    </div>
                  </div>
                  <div class="form-row">
                    <div class="col-lg-7">
                      <button class="btn1 mt-3 mb-5" type="submit">Register</button>
                    </div>
                  </div>
    
                  <p>Have an account then <a href="/">Sign In</a></p>
                </form>
              </div>
            </div>
          </div>
        </section>
    
        <!-- Option 1: jQuery and Bootstrap Bundle (includes Popper) -->
        <script
          src="https://code.jquery.com/jquery-3.5.1.slim.min.js"
          integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj"
          crossorigin="anonymous"
        ></script>
        <script
          src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.bundle.min.js"
          integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx"
          crossorigin="anonymous"
        ></script>
      </body>
    </html>

    【讨论】:

    • 你能解释一下我做错了什么吗,因为它看起来与你的 sn-p 配合得很好,但我不知道你做了什么不同的@Jeet Patel
    • 很高兴解释。 :) 你只是想念&lt;/div&gt;&lt;div class="col-lg-7 px-5 pt-5"&gt; &lt;h1 class="font-weight-bold py3"&gt;Registration Form&lt;/h1&gt;........&lt;/div&gt;。我刚刚添加了它。
    • 你在哪里添加的
  • 用户名部分的主 div 是所有输入的父项,因为您忘记在开始“用户名”div 部分之前关闭它。所以 parant 正在控制所有输入标签的样式。这是小错误。我建议你使用自动 html 语法编译,这样以后就不会发生了。
  • 谢谢我补充说 /div 创造了奇迹 LOL 非常感谢 :)
  • 【解决方案2】:

    如果我有您的问题,这应该对您有所帮助。 您需要在索引页面的顶部添加清晰的表单标签。要使用crispy form标签,需要先安装;

    pip install django-crispy-forms
    

    然后在索引页面的顶部(或任何存在表单的页面或在base.html页面中使用它);

    {% load crispy_forms_tags %}
    

    在名为 forms.py 的不同文件中创建表单,您可以在其中从 python 类创建表单。 将这些表单导入到views.py 文件中,并将它们作为变量传输到html 页面中。 在html页面中;

    <form action=some_url>
    {{ form | crispy }} <!-- here the form is the variable whose value is set to be the class which created form in forms.py -->
    </form>
    

    【讨论】:

    • 另外你应该发布一些页面的屏幕截图(注册和登录以正确理解问题)
    • 我无法发布屏幕截图,所以我已将链接发送到图片
    • 我还没有了解脆皮的想法,所以我现在暂缓一下,非常感谢:)
    • 你的问题是对的。脆皮表格也能解决问题。你应该知道这很容易,而且你需要写的代码比你已经​​写的要少。
    猜你喜欢
    • 2017-08-26
    • 2020-02-26
    • 2018-05-22
    • 2020-12-04
    • 1970-01-01
    • 2016-08-09
    • 2017-10-13
    • 2016-08-22
    • 2018-03-18
    相关资源
    最近更新 更多