div {
width: 300px;
height: 300px;
border:1px solid black;
}
h1 {
width: 300px;
transform: rotate(-90deg)
}
<div>
<h1>Hola</h1>
</div>
If you try this snippet, you will see that the h1
is rotated and placed in the center of the div (makes sense, they have same width)
But how to align it to the left? (flexible container's width)
check this out it will give a direction to your required solution..
Updated
Or you can do in this way also
You can position the
h1
element absolutely with respect to the parentdiv
and then usetransform-origin
property to specify the axis about which the rotation should happen.In the below snippet, the element is positioned at the bottom of the parent and because the origin is set at left-bottom, the left-bottom of the element (
h1
) stays at its position during rotation.Now because of the rotation, the element would go outside of the parent after rotation. To bring it back into position add
translateY(100%)
to the transform stack. Atext-align: right
is added to set the content at left-top. Thetext-align
makes it look a bit more hackish than it actually is but otherwise it is difficult to position atleft-top
.Note to future visitors: Unlike using static values for positioning, this solution using
translateY()
would be able to adapt itself automatically even if the length of the content increases or spans multiple lines like in the below snippet. Again, the only drawback would be that the text would be right aligned.