首页 > 解决方案 > bootstrap(css):如何在页面中心布局登录表单

问题描述

现在登录表单是左侧的布局。左侧布局. 我想在中心布局,我尝试了一些css样式,但是失败了。当我在代码中删除 '{% extends "common/base.html" %} ' 时,虽然没有 css 样式和导航栏,但表单可以在中间。

我的 html 代码如下,我添加了 css 样式。

{% extends "common/base.html" %}    
{% block content %}
<div class="main-login", id="parent">
<form align="center" action="/login" method="POST" >
    <div class="login-screen">
        <div class="app-title">
            <h1>Login</h1>
        </div>
        <div class="login-form">
            <div class="control-group">
            <input type="text" class="login-field" value="" placeholder="username" name="username">
            <label class="login-field-icon fui-user" for="login-name"></label>
            </div>
            <div class="control-group">
            <input type="password" class="login-field" value="" placeholder="password" name="password">
            <label class="login-field-icon fui-lock" for="login-pass"></label>
            </div>
            <input type="submit" value="Log in" class="btn btn-primary btn-large btn-block" >
            <br>
    <p>{{ _('New User?') }} <a href="{{ url_for('auth.register') }}">{{ _('Click to Register!') }}</a></p>
    <p>
        {{ _('Forgot Your Password?') }}
        <a href="{{ url_for('auth.reset_password_request') }}">{{ _('Click to Reset It') }}</a>
    </p>
        </div>
    </div>
</form>
 </div>]

我的CSS代码:

    .main-login{
  padding-top: 100px;
  text-align: center;
  width:500px;
  /*align-items: center;*/
  vertical-align: middle;
}

     #parent {
   /*display: table;*/
   width: 50%;
   vertical-align: middle;
   text-align: center;

}
.login {
margin: 20px auto;
width: 300px;
}
.login-screen {
/*background-color: #FFF;*/
padding-top: 1000px;
background-color: #3498DB;
padding: 20px;
border-radius: 5px;
text-align: center;

}

.app-title {
text-align: center;
color: #777;
}

.login-form {
text-align: center;
}
.control-group {
margin-bottom: 10px;
}

input {
text-align: center;
background-color: #ECF0F1;
border: 2px solid transparent;
border-radius: 3px;
font-size: 16px;
font-weight: 200;
padding: 10px 0;
width: 250px;
transition: border .5s;
}

input:focus {
border: 2px solid #3498DB;
box-shadow: none;
}

.btn {
  border: 2px solid transparent;
  background: #3498DB;
  color: #ffffff;
  font-size: 16px;
  line-height: 25px;
  padding: 10px 0;
  text-decoration: none;
  text-shadow: none;
  border-radius: 3px;
  box-shadow: none;
  transition: 0.25s;
  display: block;
  width: 250px;
  margin: 0 auto;
}

.btn:hover {
  background-color: #2980B9;
}

.login-link {
  font-size: 12px;
  color: #444;
  display: block;
    margin-top: 12px;
}

使用 vsevolodts 的回答后中间的图像我想使下面的登录按钮的长度与密码形式相同:

在此处输入图像描述

标签: htmlcssbootstrap-4

解决方案


将下一个 CSS 技巧应用于主块:

margin-left:auto;
margin-right:auto;

推荐阅读