I don't understand how text rotation works in CSS3.
For instance, you can see it here, the rotated text is never on the right location as you want it,
right: 0;
bottom: 0;
it will always have the gap/ margin to the right. and always overrun at the bottom of the box that contain it.
How can I fix this?
Can I use jquery to fix it or any reliable jquery plugin for text rotation out there ?
Thanks.
Positioning the text is not very difficult when you realize you can control the rotation point through...
transform-origin: 0 0 || top left
In your specific case, it works out like this:
.year
{
display: block;
writing-mode: tb-rl;
-webkit-transform: rotate(270deg);
-webkit-transform-origin: bottom left;
-moz-transform: rotate(270deg);
-moz-transform-origin: bottom left;
-o-transform: rotate(270deg);
-o-transform-origin: bottom left;
-ms-transform: rotate(270deg);
-ms-transform-origin: bottom left;
transform: rotate(270deg);
transform-origin: bottom left;
font-size: 24px;
position: absolute;
right: -50px; /*.year width*/
bottom: 0;
border: 1px solid #000000;
}
If you take out the transformation, you will notice that .year is positioned right next to it's parent box, bottom aligned. Then you specify the bottom left corner to be the "rotation point" and voila! Absolute control over your text positioning.
http://jsfiddle.net/PQ3Ga/
Positioning rotated text in CSS is very difficult. You have to remember that the position takes effect BEFORE the text is rotated. If you remove the rotation in your example, you will see that the unrotated text is correctly positioned.
To correctly position rotated text, you have to calculate how the rotation will affect the position...and then correct that position in the stylesheet.
In your case, you need:
position: absolute;
right: -11px;
bottom: 9px;
Related
I have a square and its animation code up with pure html and css. Here is the jsbin url to the code: https://jsbin.com/medupun/edit?html,output
And following is the most relevant CSS part:
.foo {
height: 100px;
width: 100px;
-webkit-transform-origin: 100px 0%;
}
.foo:hover {
-webkit-transform: rotate(-180deg);
}
The square is 100px by 100 px. I want its transform origin to be the
top right corner of itself . I am seeking to do it with just x/y-offset. So I do it with "-webkit-transform-origin: 100px 0%;".
However, I see the square does not rotate around the top right corner, rather a point close to it. And what surprises me is, if I change the origin to 130px 0%, it will work.
Can someone help me understand where is the extra 30px comes from?
You have padding on your square that adds the 30px.
padding: 1em;
Remove that to get the results you are looking for.
.foo {
position: absolute;
top: 400px;
left: 400px;
height: 100px;
width: 100px;
text-align: center;
background: #ffea61;
-webkit-transition: all 750ms ease-in-out;
-webkit-transform-origin: 100px 0;
}
.foo:hover {
-webkit-transform: rotate(-180deg);
}
https://jsbin.com/zovucefuxi/edit?html,output
If I apply this rule to a div (with variable content)
position: absolute;
left: 0;
top: 0;
transform: rotate(90deg) translateY(-100%);
transform-origin: 0 0 0;
It rotates from its top left corner and 'hangs' in the top left corner of its parent. It doesn't move no matter what size the div
How can I do the same in the right hand corner, ie rotate it so it 'hangs' in the right hand corner of its parent and rotates from its top left.
the starting point is
position: absolute;
right: 0;
top: 0;
transform: rotate(90deg);
transform-origin: 100% 0 ;
but it 'sticks up' rather than 'hangs down'.
I can, of course, move it with right and top but those values are different for different size of content.
The answer is, as all are when you know, very simple. Thanks to Sahil for making me mock it up in Codepen, easier to see the issue than when on a live page full of data.
position: absolute;
right: 0;
top: 0;
transform: rotate(90deg) translateX(100%);
transform-origin: 100% 0 ;
ie the addition of translateX(100%); to the transform
I would like to rotate a full-width div (from side to side without free space) in which will be some content.
I want the corners on the right side to touch the right side of the page and the corners on the left side to touch the left side of the page. I don't think width:200% and overflow-x:hidden is the best solution.
How can I achieve this?
Here is an example. Note that the corners don't touch the sides of the page.
.rotated {
width: 100%;
height: 100px;
background-color: red;
-moz-transform: rotate(-6deg);
-webkit-transform: rotate(-6deg);
-o-transform: rotate(-6deg);
-ms-transform: rotate(-6deg);
transform: rotate(-6deg);
}
<div class="rotated"></div>
You might find the CSS transform skewY() helpful. It will skew the element without rotating the corners.
I've also set the transform-origin to the top right so that the element doesn't skew off the top of the page.
html,body {
margin: 0;
}
.rotated {
height: 100px;
background-color: red;
-webkit-transform-origin: top right;
-ms-transform-origin: top right;
transform-origin: top right;
-webkit-transform: skewY(-6deg);
-ms-transform: skewY(-6deg);
transform: skewY(-6deg);
}
<div class="rotated"></div>
For further reference, see the Skewing and Translating example at MDN.
You could increase the horizontal proportion with scale, but the content will be scaled as well (as long as you know it you can compensate)
.rotated {
width: 100%;
height: 100px;
background-color: red;
transform: scale(1.2 , 1) rotate(-6deg);
}
<div class="rotated"></div>
I need vertical text for a website. Thats my css code:
.vertical-category span {
display: block;
position: absolute;
top: 30px;
left: -37px;
font-size: 20px;
text-transform: uppercase;
color: #ffffff;
-webkit-transform: rotate(90deg);
-moz-transform: rotate(90deg);
-ms-transform: rotate(90deg);
-o-transform: rotate(90deg);
transform: rotate(90deg);
-webkit-transform-origin: 50% 50%;
-moz-transform-origin: 50% 50%;
-ms-transform-origin: 50% 50%;
-o-transform-origin: 50% 50%;
transform-origin: 50% 50%;
filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=1);
}
Thats well working, but the start position of the first letter depends on the length of the word:
As you can see, the left vertical text is on the red background. the right vertical text is longer and so not at the red background.
what to do, that the position of the vertical text is always fix and not depends on the text length?
It's hard to be completely sure without further context (for instance, where is that background color even coming from), but I believe this issue is your transform-origin. The first 50% is moving the element to the right. Try 0 or some static value:
transform-origin: 0 50%;
http://jsfiddle.net/dAUrF/
EDIT: This fiddle may help you visualize what is happening. The red element is the element before rotation and the yellow is after. Tweak the origin values and see how it affects the rotation.
transform-origin defines the point at which the rotation occurs. With 50% 50% the rotation occurs around the center of the element.
Consider the following attempt to rotate a paragraph 90 degrees and position it so that the corner that was initially its top-left corner (and which therefore becomes its top-right corner after the rotation) ends up located at the top-right corner of the parent block.
HTML:
<!DOCTYPE html>
<html>
<body>
<div id="outer">
<p id="text">Foo bar</p>
</div>
</body>
</html>
CSS:
#outer {
border: solid 1px red;
width:600px;
height: 600px;
position: relative;
}
#text {
transform: rotate(90deg);
position: absolute;
top: 0;
right: 0;
}
In Firefox 19.0.2 on OS X 10.6.8, it fails. This appears to be because, despite the order in which the CSS properties were given, the transformation is applied after the positioning. In other words, the browser:
places #text such that its top-right corner is located at the top-right corner of the parent block, but only then
rotates it, with the result that what is now its top-right corner is not located at the top-right corner of the parent block.
As a result, the transform-origin property isn't much use here. If, for instance, one used transform-origin: top right; then #text would need to be moved downwards by the width it had before it was rotated.
My question: is there a way to tell the browser to apply the CSS positioning properties after the rotation; and if not, then is there instead a way to move #text downwards (e.g. using top:) by the width it had before it was rotated?
NB. Ideally the solution should not require setting a fixed width: for #text, and must not require JavaScript.
You can apply more than one transform to an element, and the order does matter. This is the simplest solution: http://jsfiddle.net/aNscn/41/
#outer {
border: solid 1px red;
width:600px;
height: 600px;
position: relative;
}
#text {
background: lightBlue;
position: absolute;
top: 0;
left: 0;
right: 0;
transform: translate(100%) rotate(90deg);
transform-origin: left top;
-webkit-transform: translate(100%) rotate(90deg);
-webkit-transform-origin: left top;
}
The transform origin is the point around which a transformation is applied. For example, the transform origin of the rotate() function is the center of rotation - https://developer.mozilla.org/en-US/docs/Web/CSS/transform-origin
Rotating -90deg.
.rotate {
position:absolute;
-webkit-transform-origin: left top;
/* Safari */
-webkit-transform: rotate(-90deg) translateX(-100%);
/* Firefox */
-moz-transform: rotate(-90deg) translateX(-100%);
/* IE */
-ms-transform: rotate(-90deg) translateX(-100%);
/* Opera */
-o-transform: rotate(-90deg) translateX(-100%);
}
Solved: here
This is the code I've added:
left: 100%;
width: 100%;
-webkit-transform-origin: left top;
I've also added some prefixed transform properties so it will be cross browser
-webkit-transform:rotate(90deg);
-moz-transform:rotate(90deg);
-ms-transform:rotate(90deg);
-o-transform:rotate(90deg);
transform:rotate(90deg);
How I did it:
I've found this question and, as the name of the website says, "fiddled" with the code to obtain this behavior. I guess the solution is left: 100%; instead of right: 0;.
(the width: 100%; is there because for some reason it wasn't 100% and the text would overflow to the next line)
You may want to try using CSS3 #keyframes animation. It will allow you to rotate and reposition in any order you like. Here is a tutorial that may help: [CSS-Tricks][1]
.container {
position: relative;
width: 200px;
height: 200px;
border: 1px solid red;
}
p {
border: 1px solid blue;
position: absolute;
top: auto;
right: 0;
display: inline-block;
margin: 0;
animation: 1s rotate 1s both;
}
#keyframes rotate {
0% {
transform-origin: top left;
transform: rotate(0deg);
right:0;
}
50% {
right:0;
}
100% {
transform-origin: top left;
transform: rotate(90deg);
right: -64px;
}
}
<div class="container">
<p>some text</p>
</div>
You might want to play around with the translate option which you can apply as the second transform function after rotate and place your element at the exact position that you want to.
There is no other way I guess to tell the browser to use the position properties after the transform function is used using plain css.
See this demo - http://codepen.io/anon/pen/klImq
Place "!important" at the end of the transform line.