简体   繁体   English

在DIV内垂直对齐,在div内部有一个块元素

[英]Vertical align within DIV, with a block element inside the div

This has always driven me crazy and never found the right answer. 这总是让我发疯,从来没有找到正确的答案。

I want to achieve the following: http://juicybyte.com/stack-overflow.jpg 我想实现以下目标: http//juicybyte.com/stack-overflow.jpg

Meaning, I want to have an image on a div on the left, and text that nicely vertical-aligns itself depending on how much content there is. 意思是,我希望在左边的div上有一个图像,并且根据内容有多少,文本可以很好地垂直对齐。 Height of the text div can be fixed. 文本div的高度可以修复。

However, everything is no go. 但是,一切都没有。

<div id="widgetWhite">
<div id="widgetWhiteIcon">
    <a href="#" title="White"><img src="/images/iconWhiteIconTn.png" alt="White Icon" /></a>
</div>
<div id="widgetWhiteContent">
    <p>I would love it if this worked.</p>
    <a href="#">Download PDF</a> 
</div>
</div>

The CSS: CSS:

#widgetWhiteIcon {
width: 82px;
margin: 0 10px 0 20px;
display: inline-block;
vertical-align: middle;
}

#widgetWhiteContent {
width: 108px;
font: normal normal 11px/14px Arial, sans-serif; 
height: 110px; 
display: inline-block;
vertical-align: middle;
}

#widgetWhiteContent a {
color: #f37032;
}

Don't really care about IE6.0, but IE7.0 is required unfortunately. 不要真的关心IE6.0,但不幸的是需要IE7.0。

Thanks for any help! 谢谢你的帮助!

Here, I put together a solution for you based on the site I linked. 在这里,我根据我链接的网站为您整理了一个解决方案。 I didn't bother mapping your existing css into it, but I think you will get the idea. 我没有把你现有的CSS映射到它,但我想你会得到这个想法。

http://jsfiddle.net/M3h6v/5/ http://jsfiddle.net/M3h6v/5/

<div class="ie7vert1">
    <a href="#" title="White"><img src="http://placehold.it/120x150" alt="White Icon" /></a>            
    <div class="ie7vert2">
        <div class="ie7vert3">         
            <p>I would love it if this worked.</p>
            <a href="#">Download PDF</a> 
            <br style="clear: both;" />
        </div>
    </div>
</div>

.ie7vert1 {
    display: table; 
    #position: relative; 
    overflow: hidden;
    border: 1px dashed gray;
    float: left;
    width: 100%;
}

.ie7vert2 {
    #position: absolute; 
    #top: 50%;
    display: table-cell; 
    vertical-align: middle;
}

.ie7vert3 {
    #position: relative; 
    #top: -50%;
    border: 1px dashed red;
}

The vertical-align property has two prerequisites for use: vertical-align属性有两个使用先决条件:

  • The elements you are trying to vertically-align must be siblings. 您尝试垂直对齐的元素必须是兄弟姐妹。
  • The elements you are trying to vertically-align must not be block-level elements. 您尝试垂直对齐的元素不能是块级元素。

That being said, this is actually quite easy to solve: 话虽这么说,这实际上很容易解决:

<div id="widgetWhite">
    <div id="widgetWhiteIcon">
        <a href="#" title="White"><img src="http://placehold.it/100x100" alt="White Icon" /></a>
    </div><div id="widgetWhiteContent">
        <p>I would love it if this worked.</p>
        <a href="#">Download PDF</a> 
    </div>
</div>

Note that the closing div for #widgetWhiteIcon and the opening div for #widgetWhiteContent are touching: </div><div id="widgetWhiteContent"> . 注意,右DIV为#widgetWhiteIcon和用于开口的div #widgetWhiteContent是感人: </div><div id="widgetWhiteContent"> This allows for you to control the spacing between these two elements, since normally any space between inline elements in your markup is shown in the presentation. 这允许您控制这两个元素之间的间距,因为通常标记中的inline元素之间的任何空间都会显示在演示文稿中。

Edit: You could equivalently set font-size: 0 on #widgetWhite without worrying about whitespace. 编辑:您可以在#widgetWhite上等效地设置font-size: 0而不必担心空格。 font-size is inherited in the children elements, so you would need to explicitly set that after, like so: #widgetWhite { font-size: 0; } #widgetWhite * { font-size: 12px; } font-size在children元素中继承,因此您需要在之后显式设置,如下所示: #widgetWhite { font-size: 0; } #widgetWhite * { font-size: 12px; } #widgetWhite { font-size: 0; } #widgetWhite * { font-size: 12px; }

CSS: CSS:

p { margin: 0; }
#widgetWhite > div { 
    vertical-align: middle;
    display: inline-block; }
#widgetWhiteContent { margin: 0 0 0 4px; }
#widgetWhiteContent a { 
    margin: 1em 0 0;
    display: block; }

Preview: http://jsfiddle.net/Wexcode/DcWB8/ 预览: http//jsfiddle.net/Wexcode/DcWB8/

You have to set a fixed height for the wrapper div (div#widgetWhiteContent) first in order for vertical-align to work. 您必须首先为包装器div(div#widgetWhiteContent)设置固定高度,以使vertical-align工作。 To keep everything in div#widgetWhiteContent vertically aligned with div#widgetWhiteIcon, both div's should be at the same height. 为了使div#widgetWhiteContent中的所有内容与div#widgetWhiteIcon垂直对齐,两个div应该处于相同的高度。

So a good solution would be to set a height for the outer div, and then set the height of both child div's to 100%. 因此,一个好的解决方案是为外部div设置一个高度,然后将两个子div的高度设置为100%。

Your CSS goes like this 你的CSS是这样的

<style>
    #widgetWhite {
        height: 110px;
    }

    #widgetWhiteIcon {
        width: 82px;
        margin: 0 10px 0 20px;
        display: inline-block;
        height: 100%;
    }

    #widgetWhiteContent {
        clear: left;
        width: 108px;
        font: normal normal 11px/14px Arial, sans-serif; 
        height: 100%; 
        display: inline-block;
        vertical-align: middle;
    }

    #widgetWhiteContent a {
        color: #f37032;
    }
</style>

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

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