简体   繁体   English

Div与图像的垂直对齐

[英]Div Vertical Alignment with Images

I know this has been asked many, many times before, but I still am not able to accomplish what I want on my own. 我知道这已经被问过很多次了,但是我仍然无法独自完成自己想要的事情。 I have looked at various websites for help such as Here and Here as well as using display-table with vertical align, line height etc. 我看过各种网站以寻求帮助,例如“ 这里”和“ 这里” ,以及使用具有垂直对齐,行高等的显示表。

Here is what I am trying to accomplish ( I know I will probably have to add more divs ). 这是我要完成的工作(我知道我可能必须添加更多的div)。 The text isn't always constant, so I can't just set the padding and be done with it as the text in red and blue may change in length. 文本并不总是恒定的,所以我不能只设置填充并完成填充,因为红色和蓝色的文本的长度可能会改变。

我的div图片

Here is a simple jsFiddle for what I currently have: http://jsfiddle.net/gP2U8/9/ 这是我目前拥有的简单jsFiddle: http : //jsfiddle.net/gP2U8/9/

<div class="container">
    <div class="left">
        <img src="http://www.gadgets-for-men.co.uk/wp-content/themes/revolution_tech-20/images/rss-icon-50.gif" />
        <span>This is text below the image</span>
    </div>
    <div class="right">
        <span>This is text to the right of the image, will usualy contain a lot of text. This is text to the right of the image, will usualy contain a lot of text. This is text to the right of the image, will usualy contain a lot of text. This is text to the right of the image, will usualy contain a lot of text.</span>
    </div>
</div>


.container{
    border: 1px solid black;
    width: 400px;
    height: auto;
    position: relative;
    display: inline-block;
}

.left{
    float:left;
    width: 25%;
}

.right{
     float: right;
    width: 75%;
}

.left, .right{
     margin-top: 25px;
    margin-bottom: 25px;
}

You were so close! 你好亲密! Just set the image to display: block 只需将图像设置为display: block

http://jsfiddle.net/d4DaV/1/ http://jsfiddle.net/d4DaV/1/

you can use display: table and display: table-cell for vertical alignment. 您可以使用display: tabledisplay: table-cell进行垂直对齐。

See http://jsfiddle.net/d4DaV/3/ 参见http://jsfiddle.net/d4DaV/3/

doesn't work on IE6 and IE7 but from IE8 upward 在IE6和IE7上不起作用,但从IE8开始

Don't use floating elements. 不要使用浮动元素。 Instead, use inline elements (on which you can use the vertical-align style), glued together with white-space: nowrap and font-size: 0 , as shown in this demo fiddle . 相反,请使用内联元素(可以在其上使用vertical-align样式),该内联元素与white-space: nowrap粘合在一起white-space: nowrapfont-size: 0 ,如本演示小提琴所示。

Markup (unchanged): 标记(不变):

<div class="container">
    <div class="left"></div>
    <div class="right"></div>
</div>

Style sheet: 样式表:

.container{
    border: 1px solid black;
    width: 400px;
    padding: 25px 0;
    white-space: nowrap;
    font-size: 0;
}
.left, .right {
    display: inline-block;
    vertical-align: middle;
    white-space: normal;
    font-size: 12pt;
}
.left{
    width: 25%;
}
.right{
    width: 75%;
}
img {
    display: block;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM