Search code examples
htmlcss-tables

How to use divs to get html table?


I know it is probably dumb to create tables with divs when I can actually use tables easily. But, this is necessary for now, later on there will possibly be some CSS changes added to a div.

For now I want the layout to look like the following:

table layout

  1. This is like 3 by 3 table with some padding in between
  2. I need to have the whole thing absolutely centered.

Solution

  • I don't know about your browser support. This will work in modern browsers:

    Working Demo

    Html:

    <div class="DivTable">
        <div class="Row">
            <div class="Cell"></div>
            <div class="Cell"></div>
            <div class="Cell"></div>
        </div>
        <div class="Row">
            <div class="Cell"></div>
            <div class="Cell"></div>
            <div class="Cell"></div>
        </div>
    </div>
    

    CSS:

    .Cell{
        width:100px;
        height:50px;
        border:1px solid blue;
        float:left;
        margin-left:10px;
    }
    
    .Cell:first-child{
        margin-left:0px;
    }
    
    .Row{
        margin-top:10px;
        overflow:hidden;
    }
    
    .Row:first-child{
        margin-top:0px;
    }
    
    .DivTable{
        float:left;
        border:1px solid red;
    }