如何在不绝对定位或将图像设置为背景的情况下将文本放在图像上
我想看看是否可以在不使用position:absolute或让图像成为元素背景的情况下在图像上放置一些文本。
限制的原因是 HTML 代码将进入电子邮件,而事实证明 hotmail 两者都不支持。
我记得当我第一次开始学习 CSS 时,摆弄图像周围的浮动文本,结果常常是文本愉快地遍布图像。可悲的是,我无法重现这种行为。
全文(编辑于):
我从图形设计师那里收到了一个精美的布局。它基本上是一个漂亮的背景图片,带有链接到网站的徽标,中间基本上是一个“文本在这里”区域。
像往常一样,在这些情况下,我使用表格来确保一切都保持在原位并且可以跨浏览器+跨邮件客户端工作。
问题的根源是中间的“文本在这里”区域不是白色矩形,而是有一些背景图形。
经过一些测试,Live Hotmail 似乎不喜欢这两种位置:绝对位置或背景图像;相对边距也不好,因为它们会破坏布局的其余部分。
当前版本,适用于任何其他邮件客户端/网站:
...
<td>
<img src='myimage.jpg' width='600' height='400' alt=''>
<p style="position: absolute; top: 120px; width: 500px; padding-left: 30px;">
blablabla<br>
yadda yadda<br>
</p>
</td>
...
当然,“不可能”可能是一个完全可以接受的答案,但我希望不是;)
I'm trying to see if it is possible to put some text over an image without using position: absolute or having the image being, the background of an element.
The reason for the constraints is that the HTML code is going into an e-mail, and it turns out that hotmail supports neither.
I remember that when I first began studying CSS, fiddling around with float-ing text around images I often ended up with the text merrily going all over the image. Sadly, I can't reproduce that behavior.
Full story (edited in):
I received a fancy layout from the graphics designer. It's basically a nice background picture, with logos linking to websites and what basically is a "text goes here" area in the middle.
As usual in these cases, I'm using tables to make sure that everything stays in place AND works crossbrowser+crossmailclient.
The problem arises from the fact that the middle "text goes here" area is not a white rectangle, but has a some background graphics.
Having made some test, it appears that Live Hotmail does not seem to like neither position: absolute or background-image; relative margins are also not good because they'd ruin the rest of the layout.
Current version, works in any other mail client/website:
...
<td>
<img src='myimage.jpg' width='600' height='400' alt=''>
<p style="position: absolute; top: 120px; width: 500px; padding-left: 30px;">
blablabla<br>
yadda yadda<br>
</p>
</td>
...
Of course, “it's not possible” could be a perfectly acceptable answer, but I hope not ;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我以前总是在桌子一到就做出这样的特技。真的很难看,并且可能会严重让您运行它的任何验证器感到尴尬:重叠的表格单元格。即使没有 CSS,也可以在大多数浏览器中工作。
例子:
我知道,我应该对此投反对票。
I used to pull stunts like this all the time as soon as tables came. Really ugly, and may seriously embarrass any validator you run it trough: overlapping table cells. Works in most browsers though and even without css.
Example:
I know, I deserve a downvote for this.
如果使用绝对定位,则应同时指定
left
和top
属性。另外,您应该在某个父元素上设置position:relative;
以使其成为一个图层,以便绝对定位使用该元素作为原点。如果您不指定原点,它可能是窗口,也可能是其他一些元素,并且您不知道绝对定位的元素最终会在哪里。还有一些其他替代方案可以将元素彼此叠加,但每种方案都有其自身的局限性。
您可以使用相对定位使文本显示在图像顶部:
您可以在另一个元素内使用浮动元素将文本放置在图像顶部:
If you use absolute positioning, you should specify both the
left
andtop
attributes. Also, you should setposition:relative;
on some parent element to make it a layer, so that the absolute positioning uses that element as origin. If you don't specify the origin, it may be the window or it may be some other element, and you don't know where your absolutely positioned element will end up.There are some other alternatives to place elements on top of each other, each with their own limitations.
You can use relative positioning to make text display on top of an image:
You can use a floating element inside another element to put text on top of an image:
您可以尝试设置负边距。除了最不复杂的布局之外,这在所有布局中都很难维护。负边距有效地将元素“拉”向该方向。
You can try setting negative margins. This is very difficult to maintain in all but the least complex layouts. A negative margin effective "pulls" the element in that direction.
控制事物外观的最佳方法是将文本作为图像本身的一部分。当然,如果您使用高压缩的 jpeg 这样的有损图像格式,它看起来可能会很糟糕,但也许 PNG 适合您?或者,根据颜色的数量,GIF 也可能有效。
这也为您提供了一致的字体、过滤等。
The best way to control the look of things would be to make the text part of the image itself. Of course, if you use a lossy image format like jpeg with high compression it could look really bad, but maybe a PNG will work for you? Or, depending on the number of colors, a gif might work.
This also gives you consistent-looking fonts, filtering, etc.
假设您有一个父 div 以及其中的图像和段落,则为所有三个 div 提供“相对”位置,并为子级提供“z-index”,例如“20”图像和“21”文本。文本将出现在图像上方。您可以使用“顶部或左侧或右侧或底部”调整文本
say if you have a parent div and an image and paragraph inside it, give position "relative" to all three of them, and give "z-index" to children, say "20" image and and "21" to Text. the text will appear over the image. and you can adjust the text with "top or left or right or bottom"