Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to suspress the position: relative when using translate

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.

like image 804
Gundon Avatar asked Mar 09 '15 17:03

Gundon


People also ask

How do I turn off relative position?

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.

When should you use Translate () instead of absolute positioning?

Using 2D transforms instead of absolute positioning will typically provide better FPS by way of smaller paint times and smoother animation.

How do you translate a position in CSS?

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.

Should I use position absolute or relative?

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.


1 Answers

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;
}
like image 51
Josh Crozier Avatar answered Sep 28 '22 07:09

Josh Crozier