[英]How to position an HTML element on top of another element without affecting the layout of the element(s) beneath?
Generally speaking, HTML layout is flow-based. 一般来说,HTML布局是基于流的。 Each element gets positioned after the one before it, either to the right of it or beneath it. 每个元素都定位在它之前或之后的右边或下面。 There are plenty of exceptions, of course, that you can get by playing with styles, but even then, if you change the order of something, most things "flow" around it and make room for it. 当然,您可以通过使用样式来获得很多例外,但是即使那样,即使您更改某些事物的顺序,大多数事物也会在其周围“流动”并为其腾出空间。
But occasionally I see things that behave very differently, such as pages coming up with "dialog boxes" that float in the middle of the screen, that aren't constrained by the dimensions of the div they're parented by and don't displace other layout elements around them. 但是偶尔我会看到行为有很大的不同,例如浮在屏幕中间的带有“对话框”的页面,不受它们作为父对象且不会移位的div尺寸的限制他们周围的其他布局元素。
I'm trying to figure out a way to do something similar, but not quite the same. 我试图找出一种方法来做类似但不完全相同的事情。 I've got a table that I'm using to display a grid (yes, actually using tables correctly) and I'd like to place an image on top of one of the grid cells. 我有一张要用来显示网格的表(是的,实际上是正确使用了表),我想将图像放在一个网格单元的顶部。 I can't put it in the cell, because it's larger than the cell and I don't want to stretch my grid out, but I want it to always display at the same position relative to the grid, even if the browser window scrolls or is resized. 我不能将其放在单元格中,因为它比单元格大,并且我不想拉伸网格,但是我希望它始终显示在相对于网格的相同位置,即使浏览器窗口滚动或调整大小。
I figure there has to be some way that I can do this. 我认为必须有某种方法可以做到这一点。 If I put an ID or Class on one of the <TD>
cells, how do I create an <Image>
that is not part of the <TD>
or even the <TABLE>
that it belongs to, but will always position itself over the top of that <TD>
cell without displacing anything or affecting its layout? 如果我将ID或Class放在<TD>
单元格之一上,该如何创建一个<Image>
,它不属于<TD>
甚至不是其所属的<TABLE>
,但始终将其自身定位在<TD>
单元的顶部,而不放置任何东西或不影响其布局?
If you use 如果您使用
table {position:relative;}
then you can use: 那么您可以使用:
table img {
position:absolute;
top: #px;
left: #px;
}
This will offset the image to a particular location within the containing table and take it out of the flow of the rest of the table around it. 这会将图像偏移到包含表中的特定位置,并将其从围绕它的表的其余部分中移出。
To expand on both CJGreen and Napolux's suggestions, you can still place the image in the table cell, but position it absolutely. 为了扩展CJGreen和Napolux的建议,您仍然可以将图像放置在表格单元格中,但要绝对放置。
[Edit] Since defining the position of table cells is supposedly illegal (therefore ignored by Firefox), you can wrap the content of each <td>
in a <div>
element (preferably with JS so you don't have to make massive changes) and then set the <div>
position to relative: [编辑]由于定义表格单元格的位置被认为是非法的(因此被Firefox忽略),因此您可以将每个<td>
的内容包装在<div>
元素中(最好使用JS,因此您不必进行大量更改) ),然后将<div>
位置设置为相对:
CSS: CSS:
table td > div {
position: relative;
}
table td > div img {
position: absolute;
z-index: 999;
}
JS: JS:
$(document).ready(function() {
$("td").wrapInner('<div />');
});
See the (updated) fiddle here - http://jsfiddle.net/teddyrised/qyu3g/ 在这里查看(更新的)小提琴-http: //jsfiddle.net/teddyrised/qyu3g/
If I understand it correctly you need to use offset
properties together with position:absolute
. 如果我正确理解它,则需要将offset
属性与position:absolute
一起使用。
The absolute position takes your image out of the flow, the offset can give you the position of the element you want to overlay (the TD in your question). 绝对位置使您的图像脱离流程,偏移量可以为您提供要覆盖的元素(问题中的TD)的位置。
Having the offset (px from left and top of the page for the TD) you can the move the image to the correct position. 具有偏移量(距TD页面左侧和顶部px),您可以将图像移动到正确的位置。
Look here: http://jsfiddle.net/jrUsM/ 在这里看: http : //jsfiddle.net/jrUsM/
jQuery documentation explains it very well. jQuery文档对此进行了很好的解释 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.