如何制作" vertical-align:middle"当父容器指定"显示:table"?

时间:2018-04-08 19:04:09

标签: html css css3 center vertical-alignment

我在这里阅读 - CSS vertical-align: middle not working如果" vertical-align:middle;"属性不在CSS中工作,因为父容器没有" display:table;"指定。所以我在父母DIV中试了一下

.btn
{
    position: relative;
    margin-bottom:20px;
    padding: 0 10px;
    text-align: left;
    font-size: 16px;
    color: #333;
    background:gray;
    display: table;
    vertical-align: middle;
}

但是子元素

<span>Start</span>

仍在底部对齐 - https://jsfiddle.net/xwdnvcy5/35/。如何让span元素在中间垂直对齐?

1 个答案:

答案 0 :(得分:2)

您错过了以display: table-cell样式添加.btn span

&#13;
&#13;
    .btn
    {
        position: relative;
        margin-bottom:20px;
        padding: 0 10px;
        text-align: left;
        font-size: 16px;
        color: #333;
        background:gray;
        display: table;
        
    }

    #btn_container
    {
        border-radius:5px;
        background:red;
        position: relative;
        display: inline-block;
    }

    .btn img
    {
        left: 50%;
        margin-left: 0px;
        top: 50%;
        margin-top: 0px;
    }


    .btn span
    {
        vertical-align: middle;
        display: table-cell;
    }
&#13;
<div id="control" class="btn">

<div id="btn_container"><img width="100" height="100" src="https://cdn3.iconfinder.com/data/icons/minecraft-icons/512/Stone_Pickaxe.png" alt="Mining pick" /></div>

<span>Start</span> 

</div> 
&#13;
&#13;
&#13;