使用CSS在div内垂直对齐文本

时间:2015-10-12 14:02:53

标签: html css

我有以下HTML& CSS代码。

我想将所有文字垂直居中放置。即使“文字在这里”也会与徽标保持一致。有人能用我的代码解释我是如何实现这一目标的吗非常感谢。

小提琴: http://jsfiddle.net/z6yet59f/

.slogan {
    background:#eaeaea;
    float:left;
    width:600px
}

.slogan img {
    cursor: pointer;
    width:92px;
    height:92px
}

.slogan div {
    float:left;
    margin-left: 35px;
}

.slogan div span {
    display:block
}
<div class="slogan">
    <div>
            <p>
                <span>Text here</span>
                <img src="https://cdn3.iconfinder.com/data/icons/rcons-social/32/blog_blogger_google-128.png" id="chat_link_35" title="click to start live chat ..">
            </p>
    </div>

    <div>
        <span class="heading">National Helpline</span>
        <span>Hello</span>
    </div>

    <div>
        <span class="heading" style="color:orange">National Helpline</span>
        <span>Hello</span>
    </div>
    
</div>

2 个答案:

答案 0 :(得分:4)

请在下面找到更新的代码,希望它能解决您的问题。

.slogan {
    background:#eaeaea;
    float:left;
    width:600px
}

.slogan img {
    cursor: pointer;
    width:92px;
    height:92px;
     vertical-align: middle;
}

.slogan div {
    display:table-cell;
    vertical-align:middle;
    margin-left: 35px;
}
.slogan div p{display:block;}
   .slogan div p span{
    display:inline-block;
    vertical-align:middle;
}

.slogan div span {
    display:block
}
<div class="slogan">
    <div>
            <p>
                <span>Text here</span>
                <img src="https://cdn3.iconfinder.com/data/icons/rcons-social/32/blog_blogger_google-128.png" id="chat_link_35" title="click to start live chat ..">
            </p>
    </div>

    <div>
        <span class="heading">National Helpline</span>
        <span>Hello</span>
    </div>

    <div>
        <span class="heading" style="color:orange">National Helpline</span>
        <span>Hello</span>
    </div>
    
</div>

答案 1 :(得分:0)

只需添加此课程NO

即可

<强> CSS

.valign-middle

<强> HTML

.valign-middle {
    -webkit-display: flex;
    display: flex;
    -webkit-flex-flow: row wrap;
    flex-flow: row wrap;
    -webkit-align-items: center;
    align-items: center;
}

<强> DEMO HERE