如何在div中垂直对齐标记

use*_*010 2 css

我有这个HTML代码:

<div class="news_item">    
    <div class="featured_leftpart">
        <img src="" width="48" height="48" />
    </div>
    <div class="featured_rightpart">
        <div class="news_content">
            <h2 class="entry-title"><a href="" >TEXT </a></h2>
        </div>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

并使用此CSS:

.news_item
{
    width:300px;
    position:relative;
    padding:10px;
    height:100px;
    margin:10px;
    border:1px solid #e8e8e8;
}

div.featured_leftpart
{
    position:relative;
    float:left;
    width:64px;
    height:100%;
}

div.featured_leftpart img{
    position:absolute;
    background-color:#ff00ff;
    top:0;
    bottom:0;
    left:0;
    right:0;
    margin:auto;
}

div.featured_rightpart
{
    background-color:#ff0000;    
    float:left;
    width:180px;
    padding-left:10px;
    height:100%;
}

.news_content
{
    background-color:#00ff00;
    position:relative;
}

.news_content h2
{
    vertical-align:middle;
}
Run Code Online (Sandbox Code Playgroud)

我要做的是垂直对齐h2标签.此标记将包含帖子标题,因此有时它将是单行,有时是多行.这<div class="news_content">也只是我试图让它发挥作用.如果没有这个解决方案div,我可以轻松删除它.

这是上面代码的jsFiddle链接.

Pas*_*rby 7

延伸评论:

无需添加额外的内容.news_content,只需告诉浏览器线路有多高并且vertical-align可以正常工作:

http://jsfiddle.net/cJaSL/17/

<div class="featured_rightpart">
    <h2 class="entry-title"><a href="" >TEXT </a></h2>
</div>
Run Code Online (Sandbox Code Playgroud)
div.featured_rightpart
{
    line-height:100px; /* calculated from .news_item */
}
.featured_rightpart h2
{
    vertical-align:middle;
    margin-top:0px; /* need this to clear the default margin */
    margin-bottom:0px;
}
Run Code Online (Sandbox Code Playgroud)

但请注意,只有标题中只有一行时,此解决方案才有效.

编辑:

如果多线是不可避免的,包装似乎也无法避免:

http://jsfiddle.net/cJaSL/18/

<div class="featured_rightpart">
    <div class="title_wrapper">
        <h2 class="entry-title"><a href="" >TEXT<br />multi<br />line </a></h2>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)
div.title_wrapper {
    display:inline-block;
    vertical-align:middle;
    line-height:1.25em; /* can adjust to look nice */
}
Run Code Online (Sandbox Code Playgroud)