DIV 内垂直对齐,div 内有块元素
这一直让我发疯,从来没有找到正确的答案。
我想实现以下目标: 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在这里,我根据我链接的网站为您整理了一个解决方案。我没有费心将您现有的 css 映射到其中,但我想您会明白的。
http://jsfiddle.net/M3h6v/5/
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/
vertical-align
属性有两个使用先决条件:话虽如此,这实际上很容易解决:
请注意,
#widgetWhiteIcon
的结束 div 和#widgetWhiteContent
的开始 div 是接触的:。这允许您控制这两个元素之间的间距,因为通常标记中的
inline
元素之间的任何间距都会显示在演示文稿中。编辑: 您可以等效地在
#widgetWhite
上设置font-size: 0
而不必担心空格。font-size
在子元素中继承,因此您需要在之后显式设置它,如下所示:#widgetWhite { font-size: 0; } #widgetWhite * { 字体大小:12px; }
CSS:
预览:http://jsfiddle.net/Wexcode/DcWB8/< /a>
The
vertical-align
property has two prerequisites for use:That being said, this is actually quite easy to solve:
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 betweeninline
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:
Preview: http://jsfiddle.net/Wexcode/DcWB8/
您必须首先为包装器 div (div#widgetWhiteContent) 设置固定高度,以便垂直对齐工作。为了使 div#widgetWhiteContent 中的所有内容与 div#widgetWhiteIcon 垂直对齐,两个 div 的高度应相同。
因此,一个好的解决方案是设置外部 div 的高度,然后将两个子 div 的高度设置为 100%。
你的 CSS 是这样的
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