114

I have been trying text to go in a vertical direction like we can do in ms-word tables but so far I have only been able to do THIS... which I am not happy with because it's a box rotated... Isn't there a way to have actual vertical direction text?

I only set the rotation to 305 degrees in the demo which doesn't make the text vertical. 270deg will but I only made the demo to show rotation.

misterManSam
  • 22,389
  • 10
  • 63
  • 82
Moon
  • 17,794
  • 55
  • 130
  • 193
  • Hello, could you review your accepted answer to fit with a non obsolete syntax to improve quality. Your question is seen as a duplicate, – G-Cyrillus Nov 28 '19 at 12:59

28 Answers28

120

Alternative approach: http://www.thecssninja.com/css/real-text-rotation-with-css

p { writing-mode: tb-rl; }
swan
  • 2,339
  • 3
  • 22
  • 39
  • 2
    I was able to find this: http://generatedcontent.org/post/45384206019/writing-modes. Though it feels so hacky. – Crystal Miller Jul 29 '14 at 19:49
  • 8
    @CrystalMiller and BTW, w3schools can be a good helper, but it is not the source/original documentation (so it can miss some things), the "official" is w3.org, or the closest, more human-readable, to it is mozilla's developer network - "MDN" - https://developer.mozilla.org/ – jave.web Sep 01 '16 at 21:36
  • It works in chrome, mozilla and IE edge but not safari for windows. – HalfWebDev Sep 21 '16 at 10:48
  • 12
    `tb-rl` is deprecated, please use `vertical-rl` instead. – Jared Chu Jul 11 '18 at 09:15
  • 4
    sadly, if the vertical text is on the left side of the screen, most roman texts are read bottom-to-top. And then top-to-bottom if the text is on the right side of the screen. Having some values deprecated, we end up with the sole vertical-rl that is top to bottom for the right side of the screen. For the left side, we have to add a transform: rotate(180deg); – Kir Kanos Jul 11 '18 at 13:55
  • I am using writing-mode:vertical-lr; but in FF it still works wierd, can see example here: https://www.efsdk.com/help/exm/tab.htm – Xerix Feb 22 '21 at 01:41
84
-webkit-transform: rotate(90deg);

The other answers are correct but they led to some alignment problems. On trying out different things this CSS piece code worked perfectly for me.

.vertical{
    writing-mode:tb-rl;
    -webkit-transform:rotate(90deg);
    -moz-transform:rotate(90deg);
    -o-transform: rotate(90deg);
    -ms-transform:rotate(90deg);
    transform: rotate(90deg);
    white-space:nowrap;
    display:block;
    bottom:0;
    width:20px;
    height:20px;
}
halfer
  • 18,701
  • 13
  • 79
  • 158
Amit
  • 2,305
  • 11
  • 21
72

I was searching for an actual vertical text and not the rotated text in HTML as shown below. So I could achieve it by using the following method.

enter image description here HTML:-

<p class="vericaltext">
Hi This is Vertical Text!
</p>

CSS:-

.vericaltext{
    width:1px;
    word-wrap: break-word;
    font-family: monospace; /* this is just for good looks */
}

JSFiddle! Demo.

Update:- If you need the whitespaces to be displayed, then add the following property to your css.

white-space: pre;

So, the css class shall be

.vericaltext{
    width:1px;
    word-wrap: break-word;
    font-family: monospace; /* this is just for good looks */
    white-space: pre;/* this is for displaying whitespaces */
}

JSFiddle! Demo With Whitespace

Update 2 (28-JUN-2015)

Since white-space: pre; doesnt seem to work (for this specific use) on Firefox(as of now), just change that line to

white-space: pre-wrap;

So, the css class shall be

.vericaltext{
    width:1px;
    word-wrap: break-word;
    font-family: monospace; /* this is just for good looks */
    white-space:pre-wrap; /* this is for displaying whitespaces including Moz-FF.*/
}

JsFiddle Demo FF Compatible.

Mohd Abdul Mujib
  • 10,585
  • 8
  • 50
  • 78
28

To rotate text 90 degrees:

-webkit-transform: rotate(90deg);   
-moz-transform: rotate(90deg);
-ms-transform: rotate(90deg);
-o-transform: rotate(90deg);
transform: rotate(90deg);

Also, it appears that the span tag can't be rotated without being set to display:block.

Jbrown
  • 667
  • 9
  • 8
11

For vertical text with characters one below another in firefox use:

text-orientation: upright;
writing-mode: vertical-rl;
Iggy
  • 1,536
  • 17
  • 20
8

To display text in vertical (Bottom-top) we can simply use:

writing-mode: vertical-lr; 

transform: rotate(180deg);

#myDiv{
text-align: center;
}

#mySpan{
writing-mode: vertical-lr; 
transform: rotate(180deg);
}
<div id="myDiv"> 

<span id="mySpan"> Here We gooooo !!! </span>

</div>

Note we can add this to ensure Browser Compatibility:

-webkit-transform: rotate(180deg);   
-moz-transform: rotate(180deg);
-ms-transform: rotate(180deg);
-o-transform: rotate(180deg);
transform: rotate(180deg);

we can also read more about writing-mode property here on Mozilla docs.

Abdallah Okasha
  • 1,227
  • 14
  • 18
7

Try using:

writing-mode: lr-tb;
Mladen Janjetovic
  • 11,254
  • 8
  • 64
  • 74
4

#myDiv{
text-align: center;
}

#mySpan{
writing-mode: vertical-lr; 
transform: rotate(180deg);
}
<div id="myDiv"> 

<span id="mySpan"> Here We gooooo !!! </span>

</div>

enter image description here

pushkin
  • 7,514
  • 14
  • 41
  • 74
  • Hello thank you for trying to answer this question, can you briefly explain how your solution addresses OP's problem? – James Wong Jun 19 '18 at 02:58
3

I'm new at this, it helped me a lot. Just change width, height, top and left to make it fit:

.vertical-text {
display: block;
position:absolute;
width: 0px;
height: 0px;
top: 0px;
left: 0px;
transform: rotate(90deg);
}

You can also go here and see another way to do it. The author does it like this:

.vertical-text {
transform: rotate(90deg);
transform-origin: left top 0;
float: left;
}
halfer
  • 18,701
  • 13
  • 79
  • 158
NICK
  • 53
  • 9
2

Can use CSS3 Transform property

.txtdiv{
  transform:rotate(7deg);
  -ms-transform:rotate(7deg); /* IE 9 */
  -ms-filter: "progid:DXImageTransform.Microsoft.Matrix(M11=0.93969262, M12=0.34202014, M21=-0.34202014, M22=0.93969262,sizingMethod='auto expand')"; /* IE6-8 */
  -webkit-transform:rotate(7deg); /* Opera, Chrome, and Safari */
}
Sadee
  • 2,280
  • 27
  • 33
2

Add the class

.rotate {
        -webkit-transform: rotate(-90deg);
        -moz-transform: rotate(-90deg);

}

I use this pretty much everyday and not had any issues whatsoever with it.

https://css-tricks.com/snippets/css/text-rotation/

josliber
  • 41,865
  • 12
  • 88
  • 126
MrJoshFisher
  • 1,071
  • 5
  • 20
  • 46
2

I've manage to have a working solution with this :

(I have a title within a middleItem class div)

.middleItem > .title{
    width: 5px;
    height: auto;
    word-break:break-all;
    font-size: 150%;
}
RVA
  • 539
  • 1
  • 11
  • 28
2

Here is an example of some SVG code I used to get three lines of vertical text into a table column heading. Other angles are possible with a bit of tweaking. I believe most browsers support SVG these days.

<svg height="150" width="40">
  <text font-weight="bold" x="-150" y="10" transform="rotate(-90 0 0)">Jane Doe</text>
  <text x="-150" y="25" transform="rotate(-90 0 0)">0/0&nbsp;&nbsp;&nbsp;&nbsp;0/0</text>
  <text x="-150" y="40" transform="rotate(-90 0 0)">2015-06-06</text>
  Sorry, your browser does not support inline SVG.
</svg>
2

rotation, like you did, is the way to go - but note that not all browsers support that. if you wan't to get a cross-browser solution, you'll have to generate pictures for that.

Michael Irigoyen
  • 21,233
  • 17
  • 82
  • 125
oezi
  • 47,789
  • 10
  • 94
  • 113
2

You can achieve the same with the below CSS properties:

writing-mode: vertical-rl;
text-orientation: upright;
סטנלי גרונן
  • 2,740
  • 21
  • 43
  • 62
2

If you want an alignement like

S
T
A
R
T

Then follow https://www.w3.org/International/articles/vertical-text/#upright-latin

Example:

div.vertical-sentence{
  -ms-writing-mode: tb-rl; /* for IE */
  -webkit-writing-mode: vertical-rl; /* for Webkit */
  writing-mode: vertical-rl;
  
}
.rotate-characters-back-to-horizontal{ 
  -webkit-text-orientation: upright;  /* for Webkit */
  text-orientation: upright; 
}
<div class="vertical-sentence">
  <p><span class="rotate-characters-back-to-horizontal" lang="en">Whatever</span></p>
  <p><span class="rotate-characters-back-to-horizontal" lang="fr">Latin</span></p>
  <p><span class="rotate-characters-back-to-horizontal" lang="hi">वर्डप्रेस </span></p>
</div>

Note the Hindi has an accent in my example and that will be rendered as a single character. That's the only issue I faced with this solution.

Beda Schmid
  • 408
  • 6
  • 25
1

Best solution would be to use writing-mode writing-mode: vertical-rl; https://developer.mozilla.org/en-US/docs/Web/CSS/writing-mode

It defines whether lines of text are laid out horizontally or vertically and the direction in which blocks progress.

It has good browser support, but will not work on IE8 (if you care about IE) http://caniuse.com/#feat=css-writing-mode

Vasil Enchev
  • 1,310
  • 13
  • 17
1
.vertical-text {
    transform: rotate(90deg);
    transform-origin: left top 0;
    float: left;
}
Code
  • 467
  • 4
  • 8
1
<!DOCTYPE html>
<html>
    <style>
        h2 {
           margin: 0 0 0 0;
           transform: rotate(270deg);
           transform-origin: top left;
           color: #852c98;
           position: absolute;
           top: 200px;
        }
    </style>
    <body>
        <h2>It’s all in the curd</h2>
    </body>
</html>
1

From developer.mozilla.org

The text-orientation CSS property sets the orientation of the text characters in a line. It only affects text in vertical mode (when writing-mode is not horizontal-tb). It is useful for controlling the display of languages that use vertical script, and also for making vertical table headers.

writing-mode: vertical-rl;
text-orientation: mixed;

You can also review all the Syntax here

/* Keyword values */
text-orientation: mixed;
text-orientation: upright;
text-orientation: sideways-right;
text-orientation: sideways;
text-orientation: use-glyph-orientation;

/* Global values */
text-orientation: inherit;
text-orientation: initial;
text-orientation: unset;
Yousef Altaf
  • 2,176
  • 4
  • 41
  • 62
1

You do with this too...

.p{
   writing-mode: vertical-rl;
   text-orientation: upright;
 }
Abdul Rahman
  • 1,399
  • 2
  • 17
  • 35
0

This works as well:

transform: rotate(90deg);
Pang
  • 8,605
  • 144
  • 77
  • 113
fresh5447
  • 1,072
  • 2
  • 12
  • 25
  • Duplicate answer to https://stackoverflow.com/a/12482721/1549818 2 years earlier – grg Jul 03 '20 at 18:30
0

You can use word-wrap:break-word to get vertical text use following snippete

HTML:

<div class='verticalText mydiv'>Here is your text</div>

css:

.verticalText {
word-wrap: break-word;
  font-size: 18px;
}
.mydiv {
  height: 300px;
  width: 10px;
}
0

<style>
    #text_orientation{
        writing-mode:tb-rl;
        transform: rotate(90deg);
        white-space:nowrap;
        display:block;
        bottom:0;
        width:20px;
        height:20px;
    }
</style>
</head>
<body>

<p id="text_orientation">Welcome</p>
</body>

Ayyappa
  • 11
0
h1{word-break:break-all;display:block;width:40px;} 

H E L L O

NOTE: Browser Supported - IE browser (8,9,10,11) - Firefox browser (38,39,40,41,42,43,44) - Chrome browser (44,45,46,47,48) - Safari browser (8,9) - Opera browser (Not Supported) - Android browser (44)

Salehin
  • 3
  • 4
0

Try using an SVG file, it seems to have better browser compatibility, and won't break your responsive designs.

I tried the CSS transform, and had much trouble with the transform-origin; and ended up going with an SVG file. It took like 10 minutes, and I could control it a bit with CSS too.

You can use Inkscape to make the SVG if you don't have Adobe Illustrator.

b01
  • 3,476
  • 1
  • 21
  • 26
0

This is a bit hacky but cross browser solution which requires no CSS

<div>
  <div>h</div>
  <div>e</div>
  <div>l</div>
  <div>l</div>
  <div>o</div>
<div>
grg
  • 3,915
  • 3
  • 27
  • 38
Luke Preston
  • 109
  • 1
  • 3
0

You can try like this

-webkit-transform: rotate(270deg);   
-moz-transform: rotate(270deg);
-ms-transform: rotate(270deg);
-o-transform: rotate(270deg);
transform: rotate(270deg);
Bablu Ahmed
  • 2,638
  • 1
  • 26
  • 46