垂直对齐图像和多行文本
我正在尝试垂直对齐图像和文本:
+-------------------------+ -- Viewport | Text text text | | +-----+ text text text | | |IMAGE| text text text | | +-----+ text text text | | text text text | +-------------------------+
如果文本未换行,则效果很好。如果文本比视口宽度宽,则它不再起作用。我认为这是由于设置 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 | +-------------------------+
如果我尝试浮动元素,图像将始终位于顶部,但不会在文本中间垂直对齐:
<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),但这不是一个选项,因为它必须适用于所有类型的浏览器(桌面和移动)。
对此,我不知道任何尺寸。无论是图像还是文字。所以我不能使用任何“边距或填充解决方案”。
编辑:我创建了一个演示小提琴(基于NGLN 已经创建了,顺便说一句:谢谢你!),它显示了我正在寻找的结果。但我尝试在不使用 display: table-cell 的情况下存档此内容...有什么想法吗?
I´m trying to align an image and a text vertically:
+-------------------------+ -- 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>
The result:
+---------------------------------------------------------------------+ -- 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>
The result:
+-------------------------+ -- Viewport | +-----+ Text text text | | |IMAGE| text text text | | +-----+ text text text | | text text text | | text text text | | text text text | +-------------------------+
I´ve seen several solutions for this problem, using html-tables or css-tables (display: table and display: table-cell), but this is not an option, because it must work with all types of browsers (desktop and mobile).
To that, I do not know any sizes. Neither of the image nor of the text. So I can't use any "margin- or padding-Solution".
EDIT: I´ve created a demo-fiddle (based on the one NGLN has created, BTW: Thanks for that!) that show the result i´m looking for. But I try to archive this without using display: table-cell... any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的意思是像这个演示小提琴之类的东西吗?
HTML:
CSS:
Do you mean something like this demo fiddle?
HTML:
CSS:
这是一种我并不引以为豪的方式,但这是我知道的在没有 js 或 flexbox 的情况下存档的唯一方法。
这样无论哪个元素的高度更高,文本和图像将始终居中对齐。
This is a way that I'm not proud of, but it's the only way I know to archive this without js or flexbox.
This way no matter which element has more height, the text and the image will always be aligned to the middle.
如果您可以估计图像宽度和文本宽度之间的比率,我建议在文本跨度上设置百分比宽度。
If you can estimate the ratio between the image width and text width I'd recommend setting a percentage width on the text span.
你说你不能使用边距/填充答案,因为不知道大小。但是,为什么不直接使用百分比将图像放在中间,然后将所有内容分割成 div 呢?
然后在 CSS 中执行以下操作:
显然,所有宽度和高度都可以是百分比,或者您希望如何处理它们。但是,这应该按照您想要的方式呈现。希望我正确理解你的问题。
You said you can't use margin/padding answers because of not knowing size. However, why not just use percentage to put the image halfway down, then split everything up into divs?
And then in your CSS, do this:
Obviously all the widths and heights can be percentages or however you wish to handle them. However, this should render how you want it to. Hope I am understanding your question correctly.