CSS rotate text in div

Hi Guys,

Everyday we learn something new and this is those day that I learned something and would like to share. I never really had this situation or use case before that I had to rotate a text inside a DIV or in any other container.

So the requirement came up with the new WordPress plugin I am developing for myself. Let see how this is done.

First the proposed property is called transform and is available in webkit and Mozilla browser with their prop prefixes i.e. -webkit and -moz

Lets check out the CSS to anticlockwise rotate text by 90 degrees i.e.
Anticlockwise rotation

  1. .text-rotation {
  2.       -webkit-transform: rotate(-90deg);
  3.       -moz-transform: rotate(-90deg);
  4.        -ms-transform: rotate(-90deg);
  5.         -o-transform: rotate(-90deg);
  6.         transform: rotate(-90deg);
  7. }

 

Now lets check the markup

  1. <div>
  2.         <div class="text-rotation" style="font-size:20px;width:200px;height:200px;text-align:center"><!-- I've add these extra inline styles to get myscreenshots, these are not required -->
  3.             HELLO ROTATION
  4.           </div>
  5. </div>

Ok So when you try to load the above in FireFox or Chrome or Safari all works well but in IE it doesn’t work that well because as usual IE has no support for transform property. But IE has its own filter for this stuff to work. Here is what you can do.

You can do a check on IE and add IE filter to the style above. This is how its done

  1. <!--[if IE]>
  2.         <style>
  3.              .text-rotation {
  4.                     /*rotation=0 means Zero degrees, 1 is 90deg, 2 is 180 and 3 is 270 */
  5.                     filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=3); 
  6.               }
  7.            </style>
  8. <![endif]-->

 

The end result is shown below

anticlock text rotation

 

I hope this helps

Advertisement

 

Further reading on this topic

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

http://css-tricks.com/date-display-with-sprites/

Cheers

VN:F [1.9.22_1171]
Rating: 4.5/5 (1 vote cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)
CSS rotate text in div, 4.5 out of 5 based on 1 rating

Comments

  1. Gulzaar gee says:

    While browsing some well design blogs I’m able to see some nice use of text rotation CSS property. And then I thought lets share post about how to rotate text with CSS.

    handycss.com/how/how-to-rotate-text-using-css/

Speak Your Mind

*

CommentLuv badge