Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change orientation to vertical, table rows HTML + CSS

Tags:

html

css

tablerow

I have this table:

<table id="tabla">
    <tbody>
        <tr><th>row1</th><td>aaaa</td></tr>
        <tr><th>row2</th><td>bbbb</td></tr>
        <tr><th>row3</th><td>cccc</td></tr>
        <figure><img src="image.png"/></figure></td></tr>
    </tbody>
</table>

This organizes the information in rows... But I want to rotate it and display it in columns... I hope this is enough explanation for what I want:

How I have it now:

row1: aaaa
row2: bbbb
row3: cccc imag

How I want it:

 row1 | row2 | row3
 aaaa | bbbb | cccc
             | imag  

How can I do this with CSS?

like image 913
Sergio Avatar asked Jan 04 '17 16:01

Sergio


People also ask

How do I make a vertical row in a table in HTML?

To place an item at the top or bottom of its cell, insert the "VALIGN=" attribute within the code for that cell. To vertically align an entire row (e.g., placing all data in that row at the tops of the cells), insert the "VALIGN=" attribute within the code for that row.

How do you display vertical data in HTML?

The trick is to use display: inline-block; on the table rows and white-space: nowrap; on the table body.

How do you vertically 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) . The default transform origin is the center of the element, so this works out great.

How do you change from vertical to horizontal in CSS?

Using float property: Use float property to set the alignment of element. The float value can be set to left or right. Output: Use Padding property Horizontally: Padding property is used to set the element align to Horizontally by using left and right padding.


2 Answers

If you are forced only to use CSS you can play with rotate :)

#table {
    transform:rotate(90deg);  
}
#table th, #table td{
    transform:rotate(-90deg);
}
td {
  height: 50px;
}
<table id="table">
    <tbody>
        <tr><th>row1</th><td>aaaa</td></tr>
        <tr><th>row2</th><td>bbbb</td><td>bbbb</td><td>bbbb</td></tr>
        <tr><th>row3</th><td>bbbb</td><td>bbbb</td><td>bbbb</td></tr>
    </tbody>
</table>
like image 196
Troyer Avatar answered Sep 19 '22 12:09

Troyer


This probably isn't the solution you're expecting, I would really encourage you to look at the new CSS Flexible Box Model instead of using HTML tables as it'll make your life much easier in these sort of scenarios.

If you're interested, take a look at my answer for a very similar question at Vertical Menu (+ Sub-Menu) stacks unnaturally.

like image 44
Zak Avatar answered Sep 20 '22 12:09

Zak