html - 垂直对齐图像和多行文本

标签 html css image multiline vertical-alignment

我试图垂直对齐图像和文本:

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

相关文章:

ios - 如何调整图像大小而不影响其在 ios 中的分辨率

html - 在Excel vba中实现HTMLBaseElement

html - Bootstrap 自定义布局

css - 是否可以为行反转设置 flexbox 流,但最后一项从右上角开始?

javascript - 我编写了一个在 Microsoft Edge 中工作但在 Google Chrome 中不工作的滚动指示器

css - 如何在一行中对齐 3 张图像?

java - 添加 JLabel 作为 JPanel 的背景

javascript - 如何根据此编码显示隐藏

javascript - 显示和隐藏 div 以更改页面内容

php - 将点击跳过图库更改为滑动照片库?