2012-08-06 131 views
1

this jsFiddle中,如何垂直对齐图像和文本?如何垂直对齐这2个div?

HTML:

<div class="step_div"> 
    <div class="step_div_inner"> 
     <div class="step_div_inner_image"> 
      <img src="http://i.imgur.com/TasZn.png"> 
     </div> 
     <div class="step_div_inner_text"> 
      here is some text here is some text here is some text here is some text here is some text here is some text 
     </div> 
    </div> 
</div>​ 

CSS

.step_div_inner_text { 
    font-size:18px; 
    width:460px; 
    line-height:24px; 
    position:relative; 
    left:5px; 
    bottom:14px; 
    display:inline-block; 
} 
.step_div_inner_image { 
    display:inline-block; 
} 
.step_div { 
    padding:20px 0; 
} 
.step_div_inner { 
    width:574px; 
    margin:0 auto; 
}​ 
+1

试图把所有这些在一张桌子上。或者如果你只想把图像或文字居中,把它们放在一个单独的表格中。 – boyd 2012-08-06 20:53:50

+0

图像没有出现在小提琴中。 – 2012-08-06 20:54:37

+0

这里有一些关于垂直对齐的提示:http://phrogz.net/css/vertical-align/index.html – 2012-08-06 20:55:14

回答

0

http://jsfiddle.net/xH2da/11/

CSS:

.step_div_inner_text { 
    font-size:18px; 
    width:460px; 
    position:relative; 
    display:inline-block; 
    vertical-align: top; 
} 
.step_div_inner_image { 
    display:inline-block; 
    vertical-align: middle; 
    line-height: 2em; 
} 
.step_div { 
    padding:20px 0; 
} 
.step_div_inner { 
    width:574px; 
    margin: auto; 
} 

http://www.w3.org/TR/CSS2/visudet.html#propdef-vertical-align

+0

在Windows 7上使用Opera 12,IE 8和Firefox 14.0.1进行测试,并且工作正常。 – 2012-08-06 21:07:15

0

这是我最好的拍摄......

.step_div_inner_text { 
    font-size:18px; 
    width:460px; 
    line-height:18px; 
    left:5px; 
    vertical-align:top; 
    bottom:14px; 
    display:inline-block; 
} 
.step_div_inner_image { 
    display:inline-block; 
} 
.step_div { 
} 
.step_div_inner { 
    width:574px; 
    margin:0 auto; 
} 

而且你还可以把图片周围的几个像素在图片上方...取决于您使用的文本和字体G。没有定义你的问题的答案。但是,在特定情况下,问题是,行高比字号大。

如果要坚持你的行高,那么你就需要一个顶级的填充添加到您的图像DIV,直到它适合你的文字...试验和错误:)