60

According to this article at W3 Schools, one can create a basic table in HTML like this:

<table border="1">
    <tr>
        <td>row 1, cell 1</td>
        <td>row 1, cell 2</td>
    </tr>
    <tr>
        <td>row 2, cell 1</td>
        <td>row 2, cell 2</td>
    </tr>
</table>

From above, it appears that one enters data by rows.

I have a situation where I need to enter all of the data by columns. Is something like this possible?

<table border="1">
    <tc>
        <td>row 1, cell 1</td>
        <td>row 2, cell 1</td>
    </tc>
    <tc>
        <td>row 1, cell 2</td>
        <td>row 2, cell 2</td>
    </tc>
</table>
5
  • Can your software read the <td> tags? furthermore could it read id's or classes? e.g. <td id="column_one"> Commented Apr 17, 2013 at 23:36
  • The manual does not say any details about how much HTML it knows, but I found it also supports CSS, so maybe it supports more than I guessed. Commented Apr 17, 2013 at 23:44
  • You might be able to feed it back to the program in "columns" by labling your cells with id's or classes Commented Apr 17, 2013 at 23:52
  • I'm not sure I don't know anything about this mysterious program of yours. You said it can control basic HTML so I made a guess. Commented Apr 18, 2013 at 23:30
  • 2
    @Village: I fixed your spelling errors from W3C Schools to just W3 Schools. Quite an important difference seeing W3Schools is not even W3C compliant most of the time. W3Schools == W3Fools! If you want to learn about tables or read HTML online documentation got straight to the source www.w3.org or MDN - LEARN HTML MDN is a great source for Html, JavaScript and more. Commented Apr 21, 2013 at 8:18

6 Answers 6

45

In modern browsers you can achieve this by redefining the TR and TD tags behavior in CSS. Given the HTML in your question attach the next CSS style:

table {
	display: table;
}
table tr {
	display: table-cell;
}
table tr td {
	display: block;
}
<table border="1">
    <tr>
        <td>row 1, cell 1</td>
        <td>row 2, cell 1</td>
    </tr>
    <tr>
        <td>row 1, cell 2</td>
        <td>row 2, cell 2</td>
    </tr>
</table>

Sign up to request clarification or add additional context in comments.

8 Comments

why not display table-row instead of display block?
I'm wondering why it is not the most suitable solution on the web. Does it has any drawbacks? @Istvan
I see it now it has issues with height of the cells. Not all cells in the newly cretaed row are of equal height.
'table-row' just does not work. The question is not about formatting the result but indeed I have not seen issues with setting height or any supported properties ever.
Tried this today and my mind was blown. I suspect it trashes accessibility but it's amazing no less.
|
27

You can render tables in columns by using a table within a table...

<table>
<tr>
<td>
    <table>
        <thead>
            <tr>
                <td>column 1 header 1</td>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>column 1 row 1</td>
            </tr>
            <tr>
                <td>column 1 row 2</td>
            </tr>
        </tbody>
    </table>
</td>
<td>
    <table>
        <thead>
            <tr>
                <td>column 2 header 1</td>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>column 2 row 1</td>
            </tr>
            <tr>
                <td>column 2 row 2</td>
            </tr>
        </tbody>
    </table>
</td>
</tr>
</table>

1 Comment

The only issue is that we lose the benefit of having height row aligned according to contents.
19

You're best bet is to render the table by rows, and then use javascript to invert the table

http://jsfiddle.net/CsgK9/2/

The following code will invert a table (this sample code uses jquery)


    $("table").each(function() {
        var $this = $(this);
        var newrows = [];

        $this.find("tr").each(function(){
            var i = 0;

            $(this).find("td").each(function(){
                i++;
                if(newrows[i] === undefined) { newrows[i] = $(""); }
                newrows[i].append($(this));
            });
        });

        $this.find("tr").remove();
        $.each(newrows, function(){
            $this.append(this);
        });
    });

UPDATE:

Here is a version that works with th elements as well: http://jsfiddle.net/zwdLj/

4 Comments

nice answer, would like to see you retain <th> elements though
I added a second jsfiddle that supports <th> elements
How would I go about this if I wanted to leave the headings at the top?
Not sure what you’re asking @Chubbypanda
1

Just did this by using a bunch of uls filled with lis, seemed a lot cleaner than anything I could find. You'll have to do something like adding a class to all the uls and setting its style to display: inline-table.

@* pseudo html/razor *@
@foreach(var col in columns){
    <ul class='tableColumn'>
        foreach(var data in col){
            <li>data</li>
        }
    </ul>    
}

and some styling

.tableColumn {
    border: 1px solid;
    display: inline-table;
}

Comments

1

You can always create a parent element (the table), and then inside the table you can create in-line block elements with limited width to serve as your columns, that way any content you add to those child columns will follow the downward pattern, then to make the grid-like pattern just make sure to set the height of the elements within the column, like so Using the list to display content:

<div id="your_table">
    <span style="width: 25%" id="fist_column"> <ul></ul></span>
    <span style="width: 25%" id="second_column"><ul></ul></span>
    <span style="width: 25%" id="third_column"><ul></ul></span>
    <span style="width: 25%" id="fourth_column"><ul></ul></span>
</div>

Comments

-2

I was in same situation where I have to add the data by column. But, this problem is solved in a simple method. I have used twig in this example, but you will get it easily.

<table>
<tr>
    <th>id</th>
    <th>title</th>
    <th>ISBN</th>
    <th>author_id</th>
    <th>publisher_id</th>
</tr>

{% for book in books %}   //for loop is used before the <tr> tag
    <tr>
        <td>{{ book.id }}</td>
        <td>{{ book.title }}</td>
        <td>{{ book.isbn }}</td>
        <td>{{ book.publisher.id }}</td>
        <td>{{ book.author.id }}</td>
    {% endfor %}
    </tr>

Note:{{ this is data to print }}

1 Comment

This is really bad. Don't do this. You've maybe discovered some browser quirk thats going to get patched out and then your html is just garbage. Balance your tags. Don't rely on bugs like this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.