tdoakiiii
tdoakiiii

Reputation: 372

In-line tables with CSS

I am having a major problem trying to create in-line tables with divs instead of tables and CSS. My issue is that I have 3 types of rows that aren't really in sync, so expectedly, do not line up. I am wondering if there is a solution or a better approach.

As you'll see in the fiddle, I am trying to get the header, row, and child row to line up. All columns and headers have the same width %, but their overall widths will be different. Also, the child row should be indented to appear under the parent.

Fiddle

http://jsfiddle.net/b6ptag0b/15/

HTML

<div id = "wrapper">
<div id = "headers">
    <div class = "col header_name">Name</div>
    <div class = "col header_id">ID</div>
    <div class = "col header_country">Country</div>
</div>

<div id = "table">
    <div class = "row" data-id = "1">
        <div class = "col button">+</div>
        <div class = "col name">Bob Dolle</div>
        <div class = "col id">1234</div>
        <div class = "col country">America</div>
    </div>
    <div class = "row_child" data-parent = "1">
        <div class = "col name">Joe Dolle</div>
        <div class = "col id">67788</div>
        <div class = "col country">America</div>                
    </div>
</div>

CSS

#headers{
    background-color:#ccc;
}
.row{
    background-color:#ddd;
}
.row_child{
    background-color:#AAA8A8;
    margin-left:40px;
}
.col{
    display:inline-block;
}
#table{
    padding:5px;
}
.header_name, .name{
    width:30%
}
.header_id, .id{
    width:10%;
}
.header_country, .country{
    width:25%;
}
.button{
    padding:0 10px;
}

Upvotes: 1

Views: 82

Answers (2)

Holf
Holf

Reputation: 6431

Modern browsers will allow you to use new CSS Table Layouts, for example:

Fiddle

http://jsfiddle.net/b6ptag0b/30/

HTML

<div id = "wrapper">
    <div id = "headers">
        <div class = "col header_name">Name</div>
        <div class = "col header_id">ID</div>
        <div class = "col header_country">Country</div>
    </div>

    <div class = "row" data-id = "1">
        <div class = "col name">Bob Dolle</div>
        <div class = "col id">1234</div>
        <div class = "col country">America</div>
    </div>

    <div class = "row_child" data-parent = "1">
        <div class = "col name">Joe Dolle</div>
        <div class = "col id">67788</div>
        <div class = "col country">America</div>                
    </div>
</div>

CSS

#wrapper{
    display: table;
}

#headers{
    background-color:#ccc;
    display: table-row;
}

.row{
    background-color:#ddd;
    display: table-row;
}

.row_child{
    background-color:#AAA8A8;
    display: table-row;
}

.col{
    display: table-cell;
    padding: 2px;
}

Upvotes: 0

Devin
Devin

Reputation: 7720

well, despite the "use tables" argument (I can see why you want to use DIVS), what you want to do is incredibly easy with CSS, but you're using a really convoluted logic. You can use a columns approach instead of rows, or using your rows approach, just give each columns the same width and include the .button class in the same div as the name , then simply add some table logic to your divs

#table, #headers {
    display:table;
    width:100%;
}
.row {
    display:table-row;
    width:100%;
}
.col {
    display:table-cell;
    width:30%;
}
#headers {
    background-color:#ccc;
}
.row {
    background-color:#ddd;
}
.row_child {
    background-color:#AAA8A8;
    margin-left:40px;
}
.header_id, .id {
    width:10%;
}
.header_country, .country {
    width:25%;
}
.button {
    padding:0 10px;
    float:left;
}
.row_child .name {
    padding-left:50px;
}

See fiddle here

Upvotes: 2

Related Questions