【问题标题】:Bootstrap Horizontal Alignment IssueBootstrap 水平对齐问题
【发布时间】:2020-10-22 08:09:19
【问题描述】:

我正在开发一个 Spring Boot Web 应用程序,但在登录页面上遇到问题,您可以在附图中看到,其中 div 没有完全位于页面中心。我也在使用 Bootstrap。

它似乎几乎居中,但由于某种原因偏向左侧。我在下面发布了我的 HTML 和相关的 CSS。它的某些部分一定是冲突的,但我不确定在哪里。

<div id="parentLogin">

    <div class="row" style="width: 40%;">
        
        <div class="col-md-12 col-md-offset-2">
        
            <div>
                <c:if test="${param.error != null}">
                    <div class="login-error" style="margin: 0 auto;">Incorrect username or password</div>
                </c:if>
            </div>
    
            <div class="panel panel-default">
    
                <div class="panel-heading">
                    <div class="panel-title" style="font-weight: bold; font-size: x-large; margin: 1%;">User Login</div>
                </div>
                
                <div class="panel-body">
    
                    <form method="post" action="${loginUrl}" class="login-form">
                    
                        <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
    
                        <div class="input-group">
                            <input type="text" name="username" placeholder="Username"
                                class="form-control" />
                        </div>
    
    
                        <div class="input-group">
                            <input type="password" name="password" placeholder="Password"
                                class="form-control" />
                        </div>
    
                        
                        <button type="submit" class="suit_and_tie">Sign In</button>
                        
                    </form>
    
                </div>
    
            </div>
    
        </div>
        
    </div>
    
</div>
#parentLogin {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 75vh;
}

【问题讨论】:

    标签: html css twitter-bootstrap center-align


    【解决方案1】:

    以 id="parentLogin" 作为父级的 div 居中...固定宽度为可用宽度的 40%。但是看起来你里面的引导面板并没有在那个 div 中居中......你应该将类添加到整个子对象链中......每个类都应该使用 display: flex;并且应该指定 flex-direction、align-items 和 justify-content。您可以使用 Bootstrap 类...但是父子链中的每个元素都应指定其内容的对齐方式。

    #parentLogin {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      height: 75vh;
      width: 100%;
    }
    
    .parentRow {
      display: flex;
      flex-direction: row;
      justify-content: center;
      align-items: center;
      height: 75vh;
    }
    
    .parentCol {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      height: 75vh;
    }
    <div id="parentLogin">
      <div class="row parentRow" style="width: 40%;">
        <div class="col-md-12 col-md-offset-2 parentCol">
          <div>
            <c:if test="${param.error != null}">
              <div class="login-error" style="margin: 0 auto;">
                Incorrect username or password
              </div>
            </c:if>
          </div>
          <div class="panel panel-default">
            <div class="panel-heading">
              <div class="panel-title" style="font-weight: bold; font-size: x-large; margin: 1%;">
                User Login
              </div>
            </div>
            <div class="panel-body">
              <form method="post" action="${loginUrl}" class="login-form">
                <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
                <div class="input-group">
                  <input type="text" name="username" placeholder="Username" class="form-control" />
                </div>
                <div class="input-group">
                  <input type="password" name="password" placeholder="Password" class="form-control" />
                </div>
                <button type="submit" class="suit_and_tie">
                  Sign In
                </button>
              </form>
            </div>
          </div>
        </div>
      </div>
    </div>

    【讨论】:

    • 这在页面居中方面起作用,但缩小了我的父 div 的大小,因此现在即使它居中,输入字段也被切断,用户登录进入两个单独的行。我应该在哪里放置宽度样式以使其居中?但是,如果我尝试在 .panel panel-default 上设置 width: 75%,它会将其移回左侧,就像之前的问题一样。
    • 如果顶层设置为 width:40 并且您希望每个子 div 尽可能多地填充空间,请确保将 flex-grow 指定为至少 1。所以...如果父级将 flex-direction 设置为 row.. 然后使用 flex-grow: 1 的直接子级将尝试尽可能水平地增长到父容器的固定大小。列也一样.. 如果父级有 flex-direction: 列,那么 flex-grow: 1 的直接子级将尝试尽可能垂直增长。
    • 问题是在哪里放置 flex-direction: row 以及哪些孩子放置 flex-grow: 1,因为对于我尝试的每个组合,所发生的只是包含所有项目的 div页面移动到页面的最顶部,但仍居中且未正确展开。
    • 在我的响应代码中,我将 width: 100% 添加到 parentLogin 类。你也这样做了吗?只要你掌握了顶层盒子的大小,FlexBox 就可以很好地工作。
    • 添加 CSS .parentRow { display: flex; flex-direction: row; justify-content: center; align-items: center; height: 75vh; } .parentCol { display: flex; flex-direction: column; justify-content: center; align-items: center; height: 75vh; } #profileAbout, #parentLogin { display: flex; justify-content: center; align-items: center; height: 75vh; width: 100%; }
    猜你喜欢
    • 1970-01-01
    • 2021-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多