Making a Column-Oriented Table in HTML

流过昼夜 提交于 2019-11-30 15:48:31

You can achieve this layout with the usual markup of course. But I don't think that's what you're after. You want to nest the elements differently.

You can use div to achieve this, and the markup at the end is just as economical as table markup.

<div class="table">
    <div class="column">
        <div class="row colspan2">

        </div>
        <div class="row"></div>
    </div>
    <div class="column">
        <div class="row"></div>
        <div class="row"></div>
        <div class="row"></div>
    </div>
    <div class="column">
        <div class="row"></div>
        <div class="row colspan2"></div>
    </div>
</div>

The CSS classes would need some mucking around to get right though. EDIT: I had a go with jsfiddle, but the sizes are static, so it's not very flexible.

CSS is

.row{
    background-color:red;
    height: 50px;
    border: 1px black solid;
}
.column{
    width: 100px;
    height: 100px;
    text-align:left;
    float:left;
}
.table{
    height: 200px;
    float:left;
}

.colspan2{
    height: 100px;
}

I sort of agree with husbas's master table idea except I would do it a tiny bit differently with lists instead of nested tables. I think it depends on what type of data you're trying to represent or if you're simply using the table as a way to lay out the page.

<table>
  <tr>
    <td>
      <ul>
        <li>Row 1</li>
        <li>Row 2</li>
        <li>Row 3</li>
      </ul>
    </td>
    <td>
      <ul>
        <li>Row 1</li>
        <li>Row 2</li>
        <li>Row 3</li>
      </ul>
    </td>
    <td>
      <ul>
        <li>Row 1</li>
        <li>Row 2</li>
        <li>Row 3</li>
      </ul>
    </td>
  </tr>
</table>

You can define a master table with single row and three columns. And then each column in the master table can contain a table (or any other HTML container) within. Something like this:

<table>
  <tr><td>
        <table>This is your first columns</table>
  </td></tr>
  <tr><td>
        <table>This is your second columns</table>
  </td></tr>
  <tr><td>
        <table>This is your third columns</table>
  </td></tr>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!