DIV 内垂直对齐,div 内有块元素

发布于 2024-12-26 01:01:21 字数 997 浏览 1 评论 0原文

这一直让我发疯,从来没有找到正确的答案。

我想实现以下目标: http://juicybyte.com/stack-overflow.jpg

意思是,我想要一张图像在左侧的 div 上,以及根据内容量很好地垂直对齐的文本。文本div的高度可以固定。

然而,一切都行不通。

<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>

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;
}

不太关心IE6.0,但不幸的是需要IE7.0。

感谢您的帮助!

This has always driven me crazy and never found the right answer.

I want to achieve the following:
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. Height of the text div can be fixed.

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:

#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.

Thanks for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

俏︾媚 2025-01-02 01:01:21

在这里,我根据我链接的网站为您整理了一个解决方案。我没有费心将您现有的 css 映射到其中,但我想您会明白的。

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;
}

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.

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;
}
旧故 2025-01-02 01:01:21

vertical-align 属性有两个使用先决条件:

  • 您尝试垂直对齐的元素必须是同级元素。
  • 您尝试垂直对齐的元素不能是块级元素。

话虽如此,这实际上很容易解决:

<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>

请注意,#widgetWhiteIcon 的结束 div 和 #widgetWhiteContent 的开始 div 是接触的:

。这允许您控制这两个元素之间的间距,因为通常标记中的 inline 元素之间的任何间距都会显示在演示文稿中。

编辑: 您可以等效地在 #widgetWhite 上设置 font-size: 0 而不必担心空格。 font-size 在子元素中继承,因此您需要在之后显式设置它,如下所示: #widgetWhite { font-size: 0; } #widgetWhite * { 字体大小:12px; }

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; }

预览:http://jsfiddle.net/Wexcode/DcWB8/< /a>

The vertical-align property has two prerequisites for use:

  • 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">. 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.

Edit: You could equivalently set font-size: 0 on #widgetWhite without worrying about whitespace. 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; }

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/

书信已泛黄 2025-01-02 01:01:21

您必须首先为包装器 div (div#widgetWhiteContent) 设置固定高度,以便垂直对齐工作。为了使 div#widgetWhiteContent 中的所有内容与 div#widgetWhiteIcon 垂直对齐,两个 div 的高度应相同。

因此,一个好的解决方案是设置外部 div 的高度,然后将两个子 div 的高度设置为 100%。

你的 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>

You have to set a fixed height for the wrapper div (div#widgetWhiteContent) first in order for vertical-align to work. To keep everything in div#widgetWhiteContent vertically aligned with div#widgetWhiteIcon, both div's should be at the same height.

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%.

Your CSS goes like this

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文