CSS Vertical Text
Almost every HTML element we create is thought of in the frame of horizontal. We pay much more attention to widths than we do heights, especially when it comes to text within them. Sometimes we do, however, want to display element text in a vertical fashion. In the old days of crippled Internet Explorers, this was mostly a losing battle. These days, however, vertical text is a breeze. Here's how to do it!
The CSS
Vertical text is accomplished easily these days with CSS transforms:
.vertical-text { transform: rotate(90deg); transform-origin: left top 0; }
Depending on which direction you'd like the text to display vertically, the rotation will be different, but it's that rotate value which will make the text vertical. Here's a helpful tip:
.vertical-text { float: left; }
Floating the element will essentially emulate an auto width!
CSS transforms make loads possible, and are much more powerful than simply turning text vertical, but with the code provided above, the job is done. Isn't progress grand?
To create vertical text for IE8 and older, you can rely on the CSS property
writing-mode: tb-rl;
eventually adding the legacyfilter: flipv fliph;
thanks, i tried writing-mode: tb-rl; it worked perfectly
Great tip!
This is great. Though I tend to think of vertical text as the orientation of the letters being upright, just beneath each other.
You sir, have always something cool here!
Greetz
For RTL langs, rotate -90deg
display: inline-block;
also works instead offloat: left;
What about vertical text with letters not rotated, just every one below another? Like:
R
O
T
A
T
E
I think this really can only be achieved with JS to wrap each letter to and then:
I highly doubt it’s possible just with CSS
Sure but not cross browser: https://developer.mozilla.org/de/docs/Web/CSS/text-orientation
It’s possible, just create a container with the fixed width of one letter size according to the size of the font and the text will wrap line by line (letter by letter) if you set word-wrap to break-word.
http://www.w3schools.com/cssref/tryit.asp?filename=trycss3_js_word-wrap
=> Change #mydiv to
Width can be smaller then font width, will work too (ie.
1px
)Isti37: great solution, very elegant. Thanks!
Add this css also to help complete the experience:
@Jozef Remen props! The only issue is spaces between words this way. They are huge and line-height only works as letter spacing. I guess you’d have to add for each return line. Any other ideas?
With CSS, how would I rotate the “Signup” text?
how do I show the heading in 2 lines when rotating text?
Try to squeeze your vertical text for example into
4% width DIV
and it won’t work. I wanted to make a vertical thin text line withwidth=4%;
andheight=100%;
(height=vertical so I thought I’d be fine) but it takes the real width of your !horizontal! text and simply rotates it. Thus, all your neighboring divs are completely messed up. You have to measure (somehow?) and assign the width of your horizontal text first and only then you will get the mess this solution creates, otherwise it won’t work at all. Awkward. This method will only work OK if the width property of the place where you want to set your vertical text to this way doesn’t matter.For those wanting non-rotated, one letter below the other vertical text, try this little hack:
Use “word-wrap:break-word” to allow separation of letters between lines, and declare the width to the same value as your font-size (e.g: 1em)
That will get you 90% of the way there, but some letters like “i” will still be shown 2 in the same line. Adding padding to just a tad less than your width while using border box box-sizing will force it to show 1 and only 1 letter per line.
You’ll probably want to use “vertical align: top” and “display:inline-block” to correctly position the text and surrounding elements.
CodePen example: http://codepen.io/facundocorradini/pen/LxJVNQ
I was able to get the ‘one letter under another’ without the smaller letters getting on one line AND have it recognize the spacing between words with this css code:
JFiddle: https://jsfiddle.net/costumingdiary/q1zsqewd/
text-orientation
+writing-mode
also good combination for non-break letter text90deg
with no big space problemmore reference
https://developer.mozilla.org/en/docs/Web/CSS/text-orientation
if text-orientation:upright
https://jsfiddle.net/api/mdn/
This code was a lifesaver for me for button testing. Although the client didn’t end up using it, it was still extremely helpful for the vertical text test. Thank you!
Also mention that “word-break” shouldn’t be set to “break-word”