Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aligning an element rotated in css

I trying to rotate a div on a page and have it rest right up against the left side of its parent element (the body in this case). I know about transform-origin but no matter what values I insert it doesn't align correctly.

http://jsfiddle.net/QpHCM/

HTML

<div class="handle">Text</div>

CSS (Sass)

$transform: rotate(90deg);
$transform-origin: 0 0;

body {
    border: 1px solid red;
}

.handle {
    width: 50px;
    height: 15px;
    background: blue;
    color: white;
    text-align: center;
    padding: 5px;
    line-height: 15px;
    transform: $transform;
    -moz-transform: $transform;
    -webkit-transform: $transform;
    transform-origin: $transform-origin;
    -moz-transform-origin: $transform-origin;
    -webkit-transform-origin: $transform-origin;
}

This is driving me mad. Can anyone get the rotated element aligned to top: 0, left:0 in the body when rotated?

like image 749
Kylee Avatar asked Oct 04 '13 15:10

Kylee


People also ask

How do you rotate a element by Center in CSS?

Since the rotation is around the center of the element, its not aligned with left: 0. use: $transform: rotate(90deg) translate(0, -25px);

How do I align horizontally in CSS?

To horizontally center a block element (like <div>), use margin: auto; Setting the width of the element will prevent it from stretching out to the edges of its container.

How do I rotate a Div 90 degrees?

An element can be rotated 90 degrees by using the transform property. This property is used to move, rotate, scale and others to perform various kinds of transformation to elements. The rotate() transformation function can be used as the value to rotate the element.

How do you rotate an object 90 degrees counterclockwise CSS?

We can add the following to a particular tag in CSS: -webkit-transform: rotate(90deg); -moz-transform: rotate(90deg); -o-transform: rotate(90deg); -ms-transform: rotate(90deg); transform: rotate(90deg);


1 Answers

Since the rotation is around the center of the element, its not aligned with left: 0.

use:

$transform: rotate(90deg) translate(0, -25px);

the negative half of element width gets you there.

working example.

like image 63
shem86 Avatar answered Sep 22 '22 23:09

shem86