简体   繁体   English

垂直对齐不起作用

[英]vertical-align is not working

I know it's quite annoying, seems that vertical-align in CSS is common pitfall but I really need your help. 我知道这很烦人,似乎CSS的垂直对齐是常见的陷阱,但我确实需要您的帮助。

I want the text to be always be in "middle" vertical position left to the image (image test.gif has different dimensions) 我希望文本始终保持在图像左侧的“中间”垂直位置(图像test.gif具有不同的尺寸)

I have such structure of html elements (unfortunately I can't change it due to system restrictions) 我具有html元素的这种结构(很遗憾,由于系统限制,我无法更改它)

<div class="wrapper">
    <div class="logo"> 
        <img alt="some text" src="http://localhost/test.gif">
    </div> 
    <div  class="source">some text</div>
</div>

CSS: CSS:

.wrapper {clear: both;}
.logo {float: left;}
.source { float: left;line-height: 16px;}

Spent 1.5 hours and no luck. 花了1.5个小时,没有运气。 Tried to apply vertical align to different elements with different positioning, etc...Please help! 试图将垂直对齐应用于具有不同位置等的不同元素...请帮助!

A common issue but is explained well here: http://phrogz.net/css/vertical-align/ 一个常见的问题,但在这里得到了很好的解释: http : //phrogz.net/css/vertical-align/

Key Points 关键点

Specify the parent container as position:relative or position:absolute 将父容器指定为position:relativeposition:absolute

Specify a fixed height on the child container. 在子容器上指定固定高度。

Set position:absolute and top:50% on the child container to move the top down to the middle of the parent. 在子容器上设置position:absolute和top:50% ,以将顶部向下移动到父容器的中间。

Set margin-top:-yy where yy is half the height of the child container to offset the item up. 设置margin-top:-yy ,其中yy是子容器高度的一半,以向上偏移项目。

<style type="text/css">
    #myoutercontainer { position:relative }
    #myinnercontainer { position:absolute; top:50%; height:10em; margin-top:-5em }
</style>

...

<div id="myoutercontainer">
    <div id="myinnercontainer">
        <p>Hey look! I'm vertically centered!</p>
        <p>How sweet is this?!</p>
    </div>
</div>

If you don't need to support IE7, try this: 如果不需要支持IE7,请尝试以下操作:

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

Basically, tables give you the option to vertically align to the middle, and by making your div s act like a table, we can mimic that option. 基本上,表格为您提供了垂直对齐中间位置的选项,通过使div像表格一样,我们可以模仿该选项。

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

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