简体   繁体   English

如何在不影响下方元素布局的情况下将HTML元素放置在另一个元素之上?

[英]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.

相关问题 如何使元素出现在html的顶部,而不影响布局 - How to make element appear on top of html, without affecting layout 如何在不影响元素 2 的 position 的情况下将元素 1 的 position 与元素 2 隔离? - How do you base Element 1's position off Element 2 without affecting Element 2's position? 如何在不使用位置和边距的情况下将元素放置在另一个元素的顶部? - How to position an element on top of another element without using position and margin? 如何将元素放在另一个元素的顶部? - how to position an element on top of another element? 如何在父元素的上方(和旁边)放置一个:: before伪元素,而不影响父元素的位置 - How to put a ::before pseudo-element above (and next to) the parent element without affecting the parent's position 如何在不影响同级内联元素的情况下将内联元素定位在元素之外 - How to position an inline element outside of an element without affecting a sibling inline element 如何添加HTML元素而不影响现有内容的位置? - How can I add an HTML element without affecting the position of existing content? 如何在CSS中将一个元素放置在另一个元素的顶部 - How to position one element top of another in css 将svg元素置于另一个元素的上方 - Position svg element higher on top of another element 如何在不移位原始位置的情况下将html元素插入另一个元素 - How to insert an html element before another without shifting the originals position
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM