it seems that using transform: translateY(1px);
also causes the element to gain an extra position: relative;
-behaviour.
Is there a way to suspress this?
Here is a example on codepen.io.
I would like to position the whitebox absolutely to the green one, not the parent (red) one.
To turn off the relative positioning of an element in CSS, you can set the position property of that element to static , this will attach that element back to the whole document flow.
Using 2D transforms instead of absolute positioning will typically provide better FPS by way of smaller paint times and smoother animation.
The CSS translate() function is used to move elements in a two-dimensional space. It moves the position of the element on the plane by the amount provided by tx and ty . The translate() function accepts two arguments, indicating how much to move the element along the x and y axes respectively.
If you specify position:relative, then you can use top or bottom, and left or right to move the element relative to where it would normally occur in the document. Position Absolute: When you specify position:absolute, the element is removed from the document and placed exactly where you tell it to go.
One option would be to displace/negate the parent's positioning by wrapping an element around #three
(in this case, I added the .displacement
element).
Absolutely position this wrapper element, and position it to cover the parent (using top: 0
/right: 0
/bottom: 0
/left: 0
). Then displace the element by giving it negative translation values, relative to the parent's.
<div class="displacement">
<div id="three"></div>
</div>
.displacement {
-webkit-transform: translateY(-25px) translateX(-25px);
transform: translateY(-25px) translateX(-25px);
position: absolute;
top: 0; right: 0;
bottom: 0; left: 0;
width: 200%; height: 200%;
}
In doing so, the element #three
is positioned absolutely relative to #one
, and the parent #two
's translated positioning is effectively displaced.
Updated Example
.displacement {
-webkit-transform: translateY(-25px) translateX(-25px);
transform: translateY(-25px) translateX(-25px);
position: absolute;
top: 0; right: 0;
bottom: 0; left: 0;
width: 200%; height: 200%;
}
#three {
background-color: white;
height: 25px;
width: 25px;
position: absolute;
left: 0;
bottom: 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With