Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotating text and aligning it in CSS?

Tags:

css

What is the preferred solution to rotating then aligning text to the side of some content?

Example: http://jsfiddle.net/nVtpz/

<style>
.wrapper {
    width: 100px;
    height: 100px;
    border: 1px solid #009;
    margin: 50px;
}
.text
{
    border: 1px solid #900;
      -webkit-transform: rotate(-90deg);
     -moz-transform: rotate(-90deg);
      -ms-transform: rotate(-90deg);
       -o-transform: rotate(-90deg);
          transform: rotate(-90deg);

}
.image {
    border: 1px solid #090;
}
</style>

<div class="wrapper">    
    <div class="text">
        Text to rotate
    </div>
    <div class="image">
        <img src="http://lorempixel.com/100/100"/>
    </div>
</div>

I want the text to be aligned next to the bottom left corner of the image (but not on top of the image, i.e. without float)

I guess I could use position: relative; bottom: 0px or something similar, but is there a better way?

like image 567
dan2k3k4 Avatar asked May 29 '13 23:05

dan2k3k4


3 Answers

I think that this is what you are going for, but you can change the transform origin using transform-origin (with related vendor prefixes as needed). This is situational, but in your case:

transform: rotate(90deg) translateX(100%);
transform-origin: 100% 100%;
like image 128
Explosion Pills Avatar answered Oct 14 '22 07:10

Explosion Pills


enter image description here

.wrapper {
  position: relative;
  width: 100px;
  height: 100px;
}
.text{
  position: absolute;
  width: 100%;
  background:rgba(0,0,0,0.5);
  color:#fff;
  -webkit-transform: rotate(270deg) translateX(-100%);
          transform: rotate(270deg) translateX(-100%);
  -webkit-transform-origin: 0px 0px;
          transform-origin: 0px 0px;
}
.image img{
  vertical-align:middle;
}
<div class="wrapper">    
  <div class="text">
    Text to rotate
  </div>
  <div class="image">
    <img src="http://lorempixel.com/100/100"/>
  </div>
</div>
like image 24
Roko C. Buljan Avatar answered Oct 14 '22 05:10

Roko C. Buljan


To rotate text simply:

.container {
  writing-mode: vertical-rl;
  text-orientation: sideways;
}

Then set it in a CSS grid so it can track the viewport using sticky if you want it to move on scroll.

like image 3
vhs Avatar answered Oct 14 '22 05:10

vhs