html - 垂直居中对齐一个div

标签 html css centering

我想将我的 div 与类“centeredContent”垂直居中对齐。我在 Kendo Mobile 选项卡中。也许这就是为什么似乎没有任何效果的原因。我可能需要做一些具体的事情。我已经尝试了一个实际有效的特定解决方案,图像居中,但它下面的无序列表实际上远远超出了页面的右侧......如果我们可以让无序列表不这样做那么那就是高手。这是使图像居中的原因:

#outer {
            display: -webkit-box;
            -webkit-box-pack: center;
            -webkit-box-align: center;
        }

并且具有整个选项卡 View 的 data-stretch="true"属性。

这是我现在的代码。在我有 10 个声誉之前我不能发布图像,但基本上 centredContent div 似乎距离顶部大约 20px,仍然太高而不能位于中心:

<div data-role="view" data-title="Log In" data-layout="main" data-model="APP.models.home" data-show="showLogin">
    <h1 data-bind="html: title"></h1>
    <div class="centeredContent">
        <div>
        <img class="logo1"/><br/>
        <ul class="listView" data-role="listview" data-style="inset">
            <li>
                <label class="question">Username
                    <input type="text" value="" id="Username" />
                </label>
            </li>
            <li>
                <label class="question">Password
                    <input type="password" value="" id="Password" />
                </label>
            </li>
            <li>
                <label>
                    <button onclick="signIn()" type="button">Submit</button>
                </label>
            </li>
        </ul> 
        </div>
    </div>


    <div data-role="modalview" id="modalviewError" style="width: 90%;">
        <ul data-role="listview" data-style="inset">
            <li>
                <label type="text" value="" id="errMessage"></label>
            </li>
            <li>
                <button onclick="hidePopup()" type="button">OK</button>
            </li>
        </ul>
    </div>




</div>

CSS:

.centeredContent {

        position:absolute;
        height: 100%;
        width:100%;
        top:50%;
    }

    .logo1 {
        background-image: url(../styles/images/VCareInternationalLimited.png);
        background-repeat: no-repeat;
        margin-left: auto;
        margin-right: auto;
        width:254px;
        height:129px;
        text-align: center;
        display:block;
        left:0;
        background-size: 254px 129px;
        margin: auto;
        border-radius:10px;
    }

.listView {
    width:254px;
    border-radius:10px;
    margin-left :auto;
    margin-right :auto;
}

最佳答案

使用 #outer.centeredContent,一种有效的方法是使用以下代码:

#outer {
    display: table;
}
.centeredContent {
    display: table-cell;
    vertical-align: middle;
    display: inline-block /* Add this for IE support */
}

如果这不起作用,您可能不得不摆弄位置(尝试相对和绝对)。您还可以在 http://www.vanseodesign.com/css/vertical-centering/ 查看其他一些选项。 .

关于html - 垂直居中对齐一个div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25494903/

相关文章:

html - 我怎样才能阻止这个单元格的高度加倍?

html - CSS 在 2 个 div 周围有边框

几个并排 div 的 CSS/HTML 居中

html - 带文本的居中图标

html - 尖 Angular flex 的背景图像

html - 将嵌套文本放入 div

javascript - 将 <li> 移动到另一个列表需要更新点击监听器

css - 在 Botframework Webchat 中,如何在悬停自适应卡片操作和发送按钮时自定义颜色

jquery margin 不会适应第一次点击

css - 将 float 元素集中在一个 div 容器中