我试图垂直对齐图像和文本:
+-------------------------+ -- Viewport | Text text text | | +-----+ text text text | | |IMAGE| text text text | | +-----+ text text text | | text text text | +-------------------------+
This works fine, if the text is not wrapped. If the Text is wider than the viewport-width, it does not work anymore. I think this is caused by setting display: inline-block:
<a href="#">
<img style="display: inline-block; vertical-align: middle; margin-right: 8px;" src="images/arrow_black.png" />
<span style="display: inline-block; vertical-align: middle;">
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonum eirmod tempor invidunt ut labore et dolore
</span>
</a>
结果:
+---------------------------------------------------------------------+ -- Viewport | | | +-----+ | | |IMAGE| text text text text text text text text text text text text | | +-----+ | | | +---------------------------------------------------------------------+ +-------------------------+ -- Viewport | +-----+ Text text text | | |IMAGE| text text text | | +-----+ text text text | | text text text text | +-------------------------+
If I try to float the elements, the image will always be on top, but not vertical-aligend in the middle of the text:
<a href="#">
<img style="display: block; vertical-align: middle; margin-right: 8px; float: left;" src="/images/arrow_black.png" />
<span style="display: block; overflow: auto;">
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
</span>
</a>
结果:
+-------------------------+ -- Viewport | +-----+ Text text text | | |IMAGE| text text text | | +-----+ text text text | | text text text | | text text text | | text text text | +-------------------------+
我看到了这个问题的几种解决方案,使用 html-tables 或 css-tables(display: table 和 display: table-cell),但这不是一个选项,因为它必须适用于所有类型的浏览器(台式机和移动设备)。
为此,我不知道任何尺寸。图片和文字都没有。所以我不能使用任何“边距或填充解决方案”。
编辑:我创建了一个 demo-fiddle (基于 NGLN 创建的那个,顺便说一句:谢谢!)显示我正在寻找的结果。但是我尝试在不使用 display: table-cell 的情况下将其存档...有什么想法吗?
最佳答案
你的意思是像 this demo fiddle ?
HTML:
<div id="viewport">
<a href="#">
<img src="images/arrow_black.png" alt="" />
<span>Lorem ipsum dolor...</span>
</a>
</div>
CSS:
#viewport {
background: #bbb;
width: 350px;
padding: 5px;
position: relative;
}
#viewport img {
position: absolute;
top: 50%;
margin-top: -30px; /* = image height div 2 */
}
#viewport span {
margin-left: 68px; /* = image width + 8 */
display: block;
}
关于html - 垂直对齐图像和多行文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6524029/