代码之家  ›  专栏  ›  技术社区  ›  Luca

使div像表格一样

  •  2
  • Luca  · 技术社区  · 11 年前

    我试图让我的div像一张桌子,这样我就可以把我的“列”叠在一起,以获得良好的移动体验,但由于某种原因,我的桌子不能伸展到100%并均匀分布。

    现场演示:

    http://jsfiddle.net/7sqkgfuh/3/

    以下是HTML:

    <div class="rds-table-stacked">
        <div class="rds-column">
            <div class="rds-table-header">Header One</div>
            <div class="rds-cell">Cell Item</div>
            <div class="rds-cell">Cell Item</div>
            <div class="rds-cell">Cell Item</div>
            <div class="rds-cell">Cell Item</div>
            <div class="rds-cell">Cell Item</div>
        </div>
        <div class="rds-column">
            <div class="rds-table-header">Header Two</div>
            <div class="rds-cell">Cell Item</div>
            <div class="rds-cell">Cell Item</div>
            <div class="rds-cell">Cell Item</div>
            <div class="rds-cell">Cell Item</div>
            <div class="rds-cell">Cell Item</div>
        </div>
        <div class="rds-column">
            <div class="rds-table-header">Header Four</div>
            <div class="rds-cell">Cell Item</div>
            <div class="rds-cell">Cell Item</div>
            <div class="rds-cell">Cell Item</div>
            <div class="rds-cell">Cell Item</div>
            <div class="rds-cell">Cell Item</div>
        </div>
        <div class="rds-column">
            <div class="rds-table-header">Header Five</div>
            <div class="rds-cell">Cell Item</div>
            <div class="rds-cell">Cell Item</div>
            <div class="rds-cell">Cell Item</div>
            <div class="rds-cell">Cell Item</div>
            <div class="rds-cell">Cell Item</div>
        </div>
    </div>
    

    以及CSS:

    .rds-table-stacked {
        width:100%;
        border-bottom:3px blue solid;
        display: table;
    }
    .rds-column {
        float:left;
        width:auto;
        display:table-cell;
    }
    .rds-column > div {
        padding: 6px 12px 8px 12px;
        border-bottom:1px #d1d2d1 solid;
        width:100%;
    }
    .rds-column > div:nth-of-type(even){
        background: green;
    }
    
    .rds-cell {
        float:left;
        clear:both;
        display: table-cell;
    }
    
    @media (max-width: 500px) {
        .rds-column {
            clear:both;
            width:100%;
        }
    }
    
    .rds-table-header {
        background:blue;
        color:white;
    }
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   Josh Crozier HBP    11 年前

    去除 float: left 从…起 .rds-column :

    Updated Example

    .rds-column {
        /* float: left */
        display: table-cell;
    }
    

    我还建议添加 table-layout: fixed 到 .rds-table-stacked 元素,然后添加 box-sizing: border-box 以便在宽度/高度计算中包括填充。这样做,一切都会加起来 100% .