简体   繁体   English

如何在不使用位置和边距的情况下将元素放置在另一个元素的顶部?

[英]How to position an element on top of another element without using position and margin?

This is my code: 这是我的代码:

<table>...Some content...</table>
<table>...Another content...</table>

I want to put the second table on top of the first table. 我想将第二张桌子放在第一张桌子的顶部。 This is to be used as an email template (in some clients position and margin are not available). 这将用作电子邮件模板(在某些客户中,头寸和保证金不可用)。

Those are the only two options available (outside of transform , which definitely won't work if position isn't available) that will allow one element to invade another element's space. 这是仅有的两个可用选项(在transform之外,如果没有position ,则肯定不起作用)将允许一个元素侵入另一个元素的空间。 If you can't use position or margin , then you're out of luck, and you need to re-evaluate what you are trying to achieve and why. 如果您不能使用positionmargin ,那么您就走运了,您需要重新评估您想要实现的目标以及原因。 Any chance you could do this with images? 您是否有可能对图像执行此操作?

There are always ways...not always elegant, but when you have limited options, 'works' is often all you really need. 总有一些方法……并不总是那么优雅,但是当您的选择有限时,“作品”通常就是您真正需要的。 IMO, creativity is as much about solving a problem with limited options as it is thinking 'outside the box'. IMO认为,创造力不仅在于解决“选择框框式”问题,还在于其解决方案有限的问题。

Most email clients allow you to set 'height', so simply wrap the first table (the background) in a div and give that div height:0px; 大多数电子邮件客户端允许您设置“高度”,因此只需将第一个表(背景)包装在div中,然后将该div的height:0px; . the table will overflow the div, but the next element won't respect it's space because it has 0 height, and will effectively be layered in front. 该表将使div溢出,但是下一个元素将不尊重它的空间,因为它的高度为0,并将有效地放在前面。

http://jsfiddle.net/L0d3tnzu/ http://jsfiddle.net/L0d3tnzu/

If you want the size of the tables to match exactly, you'll probably have to explicitly set heights and widths, but the fiddle above illustrates the basic concept. 如果希望表的大小完全匹配,则可能必须显式设置高度和宽度,但是上面的小提琴说明了基本概念。 Hope this helps! 希望这可以帮助!

EDIT: Based on the additional info in the comment (the second table should only partly overlap the first table) here is an updated fiddle: https://jsfiddle.net/acq3ob6y/1/ 编辑:根据注释中的附加信息(第二个表应仅部分重叠第一个表),这里是更新的小提琴: https : //jsfiddle.net/acq3ob6y/1/

EDIT #2: Dang. 编辑2:当当。 Outlook switching to the Word/Office rendering engine for HTML/CSS might be the only way possible to get WORSE than the IE version. 将Outlook切换为HTML / CSS的Word / Office渲染引擎可能是获得WORSE的唯一方法,而不是IE版本。 Sigh. 叹。 (Thanks to @Gortonington for the comment/clarification, though!) (不过,感谢@Gortonington的评论/澄清!)

Ok, then, the idea of a background image is only a problem for retina displays (if you want them to be all crisp and beautiful and retina-ie), and retina devices are going to be handling CSS in a more modern way (hopefully!), so how about this as a solution: Media Query targeting device resolution loads CSS with the double-size img and uses css background-size to constrain it: http://jsfiddle.net/tcyjo7ok 好吧,那么,背景图像的想法只是视网膜显示器的问题(如果您希望它们都清晰美观,视网膜即是),并且视网膜设备将以更现代的方式处理CSS(希望!),那么作为解决方案如何:以设备分辨率为目标的媒体查询以CSS大小的img加载CSS,并使用CSS background-size进行约束: http : //jsfiddle.net/tcyjo7ok

Third try is a charm? 第三次尝试是魅力吗? At least the list of options is growing... 至少选项列表正在增长...

The only way to overlay two elements across email clients is through use of background images. 在电子邮件客户端上覆盖两个元素的唯一方法是使用背景图像。 Even this can be broken in some clients and requires a lot of conditional and reiterate code ( backgrounds.cm is good resource for email bg images). 甚至在某些客户端中这也可能被破坏,并且需要很多有条件的和重复的代码( background.cm是电子邮件bg图像的良好资源)。

This is the only option that will display in MOST clients. 这是将显示在MOST客户端中的唯一选项。 Even this is still very restricted and not very agile to use (but that is true in ALL email coding). 即使这仍然非常受限制,并且使用起来也不十分敏捷(但这在所有电子邮件编码中都是如此)。 Most other techniques will only work for a couple clients and break completely in all others. 大多数其他技术仅对几个客户有效,而在其他所有客户中则完全失效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM