嵌套CSS表 - 嵌套表宽度是第一列的宽度

时间:2015-03-04 17:56:28

标签: html css nested width css-tables

我正在尝试为列中的元素(示例中的一个,两个,三个,四个)提供一些信息。该元素具有子元素(在示例中为五个,六个,七个,八个)。我试图让子元素为父table_div的95%宽度,但是,它是父table_div的第一列的95%宽度。

目标是在缩进方面看起来像Reddit评论布局。如何将该子元素与父元素的宽度相同,而不是与第一列的宽度相同?

HTML:

<div class='table_div root'>
    <div class='table_row'>
        <span class='table_cell'>one</span>
        <span class='table_cell'>two</span>
        <span class='table_cell'>three</span>
        <span class='table_cell'>four</span>
    </div>

    <div class='table_div child'>
        <div class='table_row'>
            <span class='table_cell'>five</span>
            <span class='table_cell'>six</span>
            <span class='table_cell'>seven</span>
            <span class='table_cell'>eight</span>
        </div>
    </div>
</div>

CSS:

.table_div {
    display: table;
    width:100%;
}

.table_row {
    display: table-row;
    width: 100%;
}

.table_cell {
    display: table-cell;
}

.root {
    width: 100%;
}

.child {
    width: 95%;  
    margin-left: 5%;
}

http://jsfiddle.net/z168q2xg/2/

1 个答案:

答案 0 :(得分:1)

HTML:

<div class='comments'>
    <div class='comment-container'>
        <div class='comment-header'>
            <div class='comment-user'>Test User 1</div>
            <div class='comment-date'>03/04/2015 2:12 PM</div>
            <div class='comment-id'>3221</div>
        </div>
        <div class='comment-body'>This is my sample comment.</div>
        <div class='comment-replies'>
            <div class='comment-container'>
                <div class='comment-header'>
                    <div class='comment-user'>Test User 3</div>
                    <div class='comment-date'>03/04/2015 2:13 PM</div>
                    <div class='comment-id'>3232</div>
                </div>
                <div class='comment-body'>Replying to commentor</div>
                <div class='comment-replies'></div>
            </div>
            <div class='comment-container'>
                <div class='comment-header'>
                    <div class='comment-user'>Test User 4</div>
                    <div class='comment-date'>03/04/2015 2:15 PM</div>
                    <div class='comment-id'>3241</div>
                </div>
                <div class='comment-body'>OMG Another comment</div>
                <div class='comment-replies'></div>
            </div>
            <div class='comment-container'>
                <div class='comment-header'>
                    <div class='comment-user'>Test User 7</div>
                    <div class='comment-date'>03/04/2015 2:16 PM</div>
                    <div class='comment-id'>3521</div>
                </div>
                <div class='comment-body'>This is my sample comment.</div>
                <div class='comment-replies'>
                    <div class='comment-container'>
                        <div class='comment-header'>
                            <div class='comment-user'>Test User 1</div>
                            <div class='comment-date'>03/04/2015 2:12 PM</div>
                            <div class='comment-id'>3621</div>
                        </div>
                        <div class='comment-body'>This is my sample comment.</div>
                        <div class='comment-replies'></div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class='comment-container'>
        <div class='comment-header'>
            <div class='comment-user'>Test User 2</div>
            <div class='comment-date'>03/04/2015 2:15 PM</div>
            <div class='comment-id'>4221</div>
        </div>
        <div class='comment-body'>Replying to OP</div>
        <div class='comment-replies'></div>
    </div>
</div>

CSS:

.comments {
    display: block;
    width:100%;
    background-color: #CCFFFF;
}
.comment-container {
    display: block;
    width:95%;
    border: 1px solid black;
}
.comment-container + .comment-container {
    border-top: 0px;
}
.comment-replies {
    border-right: 0px solid black;
}
.comment-replies > .comment-container {
    margin-left:5%;
    border-right: 1px ;
    border-bottom: 0px;
}
.comment-replies .comment-replies {
     border-right: 0px;
}
.comment-user {
    width: 25%;
    background-color: #99FFCC;
}
.comment-date {
    width: 40%;
    background-color: #C2C2FF;
}
.comment-id {
    float: right;
    background-color: #FFCC99;
    width: 35px;
    margin-right: 0px !important;
}
.comment-header > div {
    display: inline-block;
    margin-right: 5px;
}

http://jsfiddle.net/z168q2xg/15/