Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS transform:scale(2) + position:absolute + right:0px is not working

Tags:

css

transform

I have noticed, that right:0px is positioning element incorrectly. transform:scale doesn't recalculate element width.

Is there a way to properly stick this element to the right side?

HTML:

<div id="stick_me">
    blah blah blah<br />
    blah blah blah<br />
    blah blah blah<br />
</div>

CSS:

#stick_me {
    border:1px solid red;
    display:inline-block;
    transform:scale(3);
    position:absolute;
    right:0px;
    top:0px;
}

Thank you.

like image 350
Somebody Avatar asked Aug 07 '14 03:08

Somebody


People also ask

How do you move an absolute position in CSS?

Absolute Positioning You can use two values top and left along with the position property to move an HTML element anywhere in the HTML document. Move Left - Use a negative value for left. Move Right - Use a positive value for left. Move Up - Use a negative value for top.

How do you apply position absolute?

An element with position: absolute; is positioned relative to the nearest positioned ancestor (instead of positioned relative to the viewport, like fixed). However; if an absolute positioned element has no positioned ancestors, it uses the document body, and moves along with page scrolling.

What can I use instead of absolute position?

We can use CSS absolute positioning to overlap elements, but there's some advantages to using CSS grid instead.


1 Answers

You want to use the transform-origin (https://developer.mozilla.org/en-US/docs/Web/CSS/transform-origin) property set to top right. As you are positioning the element to the top and right, you need it to scale from there, i.e. down and to the left.

#stick_me {
  border:1px solid red;
  display:inline-block;
  transform:scale(3);
  position:absolute;
  right:0px;
  top:0px;
  transform-origin:top right;
}

Demo

like image 185
Jon P Avatar answered Nov 04 '22 10:11

Jon P