Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display vertical text in table headers with auto height / without text overflow?

Tags:

html

css

I want to display rotated text as table headers, using the CSS transform property. The header row should adjust its height as needed, but instead the rotated text just overflows:

example wrong

demo fiddle

My question is, how to get the table header to grow as needed? Essentially it should look like this:

example right

like image 505
Tim Büthe Avatar asked Aug 09 '11 14:08

Tim Büthe


People also ask

What is a vertical display of text?

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.

How do I make a horizontal table in HTML?

Tables in HTML can have horizontal as well as the vertical header. For the horizontal header, you need to set all <th> inside a single <tr> tag i.e. row, that will be the topmost.


1 Answers

If you use a pseudo element and vertical-padding, you may basicly draw a square box or <td> : http://jsfiddle.net/qjzwG/319/

.verticalTableHeader {     text-align:center;     white-space:nowrap;     transform-origin:50% 50%;     transform: rotate(90deg);  } .verticalTableHeader:before {     content:'';     padding-top:110%;/* takes width as reference, + 10% for faking some extra padding */     display:inline-block;     vertical-align:middle; } 

If you want to keep <td> ith a small width, table-layout:fixed + width might help. http://jsfiddle.net/qjzwG/320/

.verticalTableHeader {     text-align:center;     white-space:nowrap;     transform: rotate(90deg);  } .verticalTableHeader p {     margin:0 -100% ;     display:inline-block; } .verticalTableHeader p:before{     content:'';     width:0;     padding-top:110%;/* takes width as reference, + 10% for faking some extra padding */     display:inline-block;     vertical-align:middle; } table {     text-align:center;     table-layout : fixed;     width:150px } 

If you want table to still be able to grow from it's content but not from width of <th> , using a wrapper with a hudge negative margin opposite to dir/direction of document might do : apparently, the closest to your needs, http://jsfiddle.net/qjzwG/320/

<table border="1">     <tr>       <th class="verticalTableHeader"><p>First</p></th>       <th class="verticalTableHeader"><p>Second-long-header</p></th>       <th class="verticalTableHeader"><p>Third</p></th>     </tr> 
.verticalTableHeader {     text-align:center;     white-space:nowrap;     transform: rotate(90deg); } .verticalTableHeader p {     margin:0 -999px;/* virtually reduce space needed on width to very little */     display:inline-block; } .verticalTableHeader p:before {     content:'';     width:0;     padding-top:110%;     /* takes width as reference, + 10% for faking some extra padding */     display:inline-block;     vertical-align:middle; } table {     text-align:center; } 

HTML from demo and base :

<table border="1">     <tr>       <th class="verticalTableHeader">First</th>       <th class="verticalTableHeader">Second</th>       <th class="verticalTableHeader">Third</th>     </tr>     <tr>       <td>foo</td>       <td>foo</td>       <td>foo</td>     </tr>     <tr>       <td>foo</td>       <td>foo</td>       <td>foo</td>     </tr>     <tr>       <td>foo</td>       <td>foo</td>       <td>foo</td>     </tr> </table> 

For older IE , you need to use writing-mode (CSS) :http://msdn.microsoft.com/en-us/library/ie/ms531187%28v=vs.85%29.aspx

like image 62
G-Cyrillus Avatar answered Oct 01 '22 18:10

G-Cyrillus