dan2k3k4
dan2k3k4

Reputation: 1409

Rotating text and aligning it in 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?

Upvotes: 7

Views: 11180

Answers (3)

vhs
vhs

Reputation: 10051

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.

Upvotes: 3

Roko C. Buljan
Roko C. Buljan

Reputation: 205987

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>

Upvotes: 5

Explosion Pills
Explosion Pills

Reputation: 191729

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%;

Upvotes: 9

Related Questions