Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotating table header text with CSS transforms

This looks like it should be possible with the following:

.verticalText  {                /* IE-only DX filter */     writing-mode: tb-rl;     filter: flipv fliph;      /* Safari/Chrome function */     -webkit-transform: rotate(270deg);      /* works in latest FX builds */     -moz-transform: rotate(270deg); } 

This works in IE.

It goes wrong in a bizarre way in Safari, Chrome and FX - the cell's size is calculated before the text is rotated!

screenshot of bug

Here is a demo: http://jsfiddle.net/HSKws/

I'm using dynamic images as a workaround, although that also has its problems. I'm happy with that as a fall-back, but it seems like there should be a way to make this CSS work - it's almost there.

Anyone know a way to make the cells fit the content after the transform has been applied?

like image 795
Keith Avatar asked Apr 04 '09 12:04

Keith


People also ask

How do you rotate text in CSS?

Rotate text can be done by using the rotate() function. We can rotate the text in clockwise and anti-clockwise direction. The rotate function can also rotate HTML elements as well.

How do I rotate text in HTML table?

The first trick is to use writing-mode: vertical-lr to get the text to run vertically. By itself, the text runs top to bottom, but we want it to run bottom to top, so we spin it around it with the transform: rotate(180deg) .

What is the CSS property for rotating an HTML element?

The transform property applies a 2D or 3D transformation to an element. This property allows you to rotate, scale, move, skew, etc., elements.

How do you rotate vertically in CSS?

If what you are looking for is a way to set type vertically, you're best bet is probably CSS writing-mode . The rotation property of Internet Explorer's BasicImage filter can accept one of four values: 0, 1, 2, or 3 which will rotate the element 0, 90, 180 or 270 degrees respectively.


2 Answers

‘transform’ alters the orientation of the entire element you declare it on, not the text content inside it. It's more like IE's ‘matrix’ property than ‘writing-mode’.

Crucially, transforming an element doesn't change how its content size is calculated (or how its parent's layout is affected by that size). CSS's algorithms for vertical and horizontal sizing are different and difficult enough to get right to being with; there's no real consistent way they could accomodate content with arbitrary rotation. So ‘transform’ is like using ‘position: relative’: it changes where the content is rendered, but not anything to do with layout size.

So if you want to include one in a table you'll need to set the cell's ‘height’ explicitly to accomodate the expected rotated ‘width’. If you don't know that in advance you could potentially hack it up with JavaScript, perhaps.

FWIW: for me on Fx3.1b3 the span is also rotated like the others. However on Windows with its horizontal-only anti-aliasing (ClearType) the rendering doesn't look great... a well-rendered image could come out considerably better.

like image 67
bobince Avatar answered Oct 03 '22 10:10

bobince


It's possible using inline SVG in a XHTML document (I only tested Safari and Firefox):

<html xmlns="http://www.w3.org/1999/xhtml">   <body>     <table border="1">         <tr>             <td>&#160;</td>             <td>                 <svg xmlns="http://www.w3.org/2000/svg" width="16" height="150">                   <text id="thetext" transform="rotate(270, 12, 0) translate(-140,0)">Example column header</text>                 </svg>             </td>             <td>                 <svg xmlns="http://www.w3.org/2000/svg" width="16" height="150">                   <text id="thetext" transform="rotate(270, 12, 0) translate(-140,0)">Example column header</text>                 </svg>             </td>             <td>                 <svg xmlns="http://www.w3.org/2000/svg" width="16" height="150">                   <text id="thetext" transform="rotate(270, 12, 0) translate(-140,0)">Example column header</text>                 </svg>             </td>         </tr>         <tr>             <td>Example row header</td>             <td>1</td>             <td>2</td>             <td>3</td>         </tr>     </table>   </body> </html> 

Unfortunately, you do have to explicitly set the width and height of your table cells and the translation of the text rendered using SVG. Also, the file extension must be xhtml.

like image 31
otto.poellath Avatar answered Oct 03 '22 10:10

otto.poellath