0

我需要一些帮助。我已经尝试了好几个小时,但找不到有效的解决方案。

<div id="Header">
<img src='http://3.bp.blogspot.com/-rN0cMMTn_Mw/ToQ6VTghSOI/AAAAAAAAAfs/xl1XMFyn7Jo/s640/18_5_orig.jpg'
/>
 <h1>siajdasdasdasd</h1>

<p>sdhaosidasdas</p>

我想做什么的例子

我想要一个液体标题,其图像向左对齐,标题与中心对齐,但是两个 em 都必须对齐到高度的中间,如果我增加 img / div 的高度就不行了

4

3 回答 3

1

不得不添加更多的div,但它的工作原理。http://jsfiddle.net/74Rnq/23/

HTML:

<div id="Header">
    <div class="wrap">
        <img src='http://3.bp.blogspot.com/-rN0cMMTn_Mw/ToQ6VTghSOI/AAAAAAAAAfs/xl1XMFyn7Jo/s640/18_5_orig.jpg'/>
        <div class="text">
            <h1>siajdasdasdasd</h1>
            <p>sdhaosidasdas</p>
        </div>
    </div>
</div>

CSS:

#Header {
    position: relative;
    height: 200px;
    padding: 15px;
    background: #DBE6EC;
    border-bottom: 1px solid #595959;
    overflow: auto;
}
#Header h1, p {
    text-align: center;
    letter-spacing: -1px;
    text-align: center;
    font-size: 2em;
    color: #1F1F1F;
}
#Header p {
    font-size: 1em;
}
#Header img {
    float: left;
    max-height:100px;
}

#Header .wrap {
    display: block;
    position: absolute;
    width: 100%;
    top: 50%;
    margin-top: -50px; /* Half of wrap div height */
}

#Header .wrap .text {
    position: absolute;
    top: 50%;
    margin-top: -27.5px; /* Half of text div height */
    width: 100%;
}
于 2013-02-02T19:52:18.403 回答
0

对于现代浏览器,您可以通过 display:table、table-row 表格单元格来完成:

像这样修改你的html:

<div id="Header" class="table">
    <div class="row">
        <div class="cell">
            <img src='http://3.bp.blogspot.com/-rN0cMMTn_Mw/ToQ6VTghSOI/AAAAAAAAAfs/xl1XMFyn7Jo/s640/18_5_orig.jpg'/>
        </div>
        <div class="cell main">
             <h1>siajdasdasdasd</h1>
             <p>sdhaosidasdas</p>
        </div>
    </div>
</div>

#Header {
    background: #DBE6EC;
    border-bottom: 1px solid #595959;
    position:relative;
    padding:15px;
    -moz-box-sizing: border-box; 
    -webkit-box-sizing: border-box; 
    box-sizing: border-box; 
}

.table {
    display:table;
    width:100%;
}

.row {
    display:table-row;
}

.cell {
    display:table-cell;
    vertical-align:middle;
}

.cell.main {
    width:100%;
}

更新的小提琴在这里。此解决方案不适用于 ie7。如果您必须支持 ie7,则垂直对齐中间有一个较旧的解决方法。

于 2013-02-02T20:09:52.340 回答
0

制作容器 divdisplay: table-cell和每个子 div display: table-cell。然后你可以给子 div vertical-align: middle,它们总是垂直居中。

HTML:

<div id="Header">
    <div id="image">
        <img src='http://3.bp.blogspot.com/-rN0cMMTn_Mw/ToQ6VTghSOI/AAAAAAAAAfs/xl1XMFyn7Jo/s640/18_5_orig.jpg' />
    </div>
    <div id="title">
       <h1>siajdasdasdasd</h1>
       <p>sdhaosidasdas</p>
    </div>
</div>

和 CSS:

#Header {
    padding: 15px;
    background: #DBE6EC;
    border-bottom: 1px solid #595959;
    display: table;
}
#Header h1, p {
    text-align: center;
    letter-spacing: -1px;
    font-size: 2em;
    color: #1F1F1F;
}
#Header p {
    font-size: 1em;
}
#Header img {
    float: left;
    max-height:100px;
}
#title{
    display: table-cell;
    vertical-align: middle;    
    width: 100%;
}
#image{
    display: table-cell;
    vertical-align: middle;
}

Aaand 这是jsFiddle

于 2013-02-02T20:13:56.483 回答