中心垂直和水平对齐表格

时间:2015-06-13 12:13:55

标签: html css html5 css3

.cls_forgotpwd_container
{
    font-family: sans-serif;
    font-size: 13px;
    width: 350px;
    /*margin: auto;*/
    width: 350px;
    display: table-cell;
    vertical-align: middle;
}

.cls_forgotpwd_table label
{
    margin-right: 10px;
}

.cls_forgotpwd_table input[type="password"]
{
    width: 200px;
    height: 20px;
    border: 1px solid #555;
}

body
{
    display: table;
}
<div class="cls_forgotpwd_container">
    <form class="cls_forgotpwd_form" id="forgotpwd_form">
        <table class="cls_forgotpwd_table">
            <tbody>
            <tr>
                <td><label>Password</label></td>
                <td><input type="password" name="password"></input></td>
            </tr>
                <td><label>Confirm Password</label></td>
                <td><input type="password" name="confirm_password"></input></td>
            <tr>
                <td><input class="cls_submit" type="submit" name="submit" value="Change It"></input></td>
            </tr>
            <tr>
            </tr>
            </tbody>
        </table>
    </form>
</div>

质疑

1)我尝试了display: tabletabel-cell with vertical-align:middle但它在我的方案中无效。这些数据不是表格式的,所以我认为这不是正确的方法。

2)我不喜欢position absolutetop and left values in %。如果我没有其他选择,我会选择这个。

还有其他可能使div对齐吗?或者我应该在上述两个选项中使用哪一个?如果我必须选择option1,那就不适用于我的情况。

我不希望margin居中对齐。 IE不支持flex

margin:0 autowidth:350px成功对齐div。

任何建议?

2 个答案:

答案 0 :(得分:1)

你的代码

body, html - 默认 - height: auto; width: auto;

.cls_forgotpwd_container
{
    font-family: sans-serif;
    font-size: 13px;
    width: 350px;
    /*margin: auto;*/
    width: 350px;
    display: table-cell;
    vertical-align: middle;
}

.cls_forgotpwd_table label
{
    margin-right: 10px;
}

.cls_forgotpwd_table input[type="password"]
{
    width: 200px;
    height: 20px;
    border: 1px solid #555;
}

body
{
    display: table;
    border: 2px solid #f00;
}
<div class="cls_forgotpwd_container">
    <form class="cls_forgotpwd_form" id="forgotpwd_form">
        <table class="cls_forgotpwd_table">
            <tbody>
            <tr>
                <td><label>Password</label></td>
                <td><input type="password" name="password"></input></td>
            </tr>
                <td><label>Confirm Password</label></td>
                <td><input type="password" name="confirm_password"></input></td>
            <tr>
                <td><input class="cls_submit" type="submit" name="submit" value="Change It"></input></td>
            </tr>
            <tr>
            </tr>
            </tbody>
        </table>
    </form>
</div>

因此,bodyhtml添加 - height: 100%width: 100%

需要居中

html, 
body {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
    box-sizing: border-box;
    display: table;     
    border: 3px solid #f00;
}

.cls_forgotpwd_container {
    font-family: sans-serif;
    font-size: 13px;
    width: 350px;      
    border: 1px solid #ccc;
    display: table-cell;
    vertical-align: middle;
    text-align: center;
}
.cls_forgotpwd_container form{
    display: inline-block;
    border: 1px solid #ccc;
}
.cls_forgotpwd_table label {
    margin-right: 10px;
}
.cls_forgotpwd_table input[type="password"] {
    width: 200px;
    height: 20px;
    border: 1px solid #555;
}
<div class="cls_forgotpwd_container">
    <form class="cls_forgotpwd_form" id="forgotpwd_form">
        <table class="cls_forgotpwd_table">
            <tbody>
                <tr>
                    <td>
                        <label>Password</label>
                    </td>
                    <td>
                        <input type="password" name="password"></input>
                    </td>
                </tr>
                <td>
                    <label>Confirm Password</label>
                </td>
                <td>
                    <input type="password" name="confirm_password"></input>
                </td>
                <tr>
                    <td>
                        <input class="cls_submit" type="submit" name="submit" value="Change It"></input>
                    </td>
                </tr>
                <tr></tr>
            </tbody>
        </table>
    </form>
</div>

答案 1 :(得分:1)

可以使用CSS transform轻松完成,浏览器支持: IE9 +

<强> JsFiddle Demo

.cls_forgotpwd_container {
    position: absolute;
    top: 50%;
    left: 50%;
    -ms-transform: translate(-50%, -50%);
    -webkit-transform: translate(-50%, -50%);
    transform: translate(-50%, -50%);
}

如果您确实需要支持 IE8 + ,请参阅内联评论。

<强> JsFiddle Demo

html, body, .cls_forgotpwd_container {
    height: 100%; /*make all the contaniers to 100% height*/
}
body {
    margin: 0; /*reset default margin*/
}
.cls_forgotpwd_container {
    display: table;
    margin: 0 auto; /*horizontally center itself*/
}
.cls_forgotpwd_form {
    display: table-cell;
    vertical-align: middle; /*vertically center things inside*/
}

应该纠正一个小问题 - <input>是自动关闭标记。

这是错误的<input></input>

这是正确的<input><input/>