【问题标题】:Is it possible to make display:table-cell layout responsive?是否可以使 display:table-cell 布局响应?
【发布时间】:2014-01-13 11:10:46
【问题描述】:

在我的代码中,有一个表格,其中我有多个部门,表格行包含水平复选框。这是我的示例代码,完整代码在小提琴Here

HTML:

<table cellpadding="0" border="0" width="100%" cellspacing="0">
    <tr>
        <td style="text-align:left" width="65px"><strong> Color: </strong>
        </td>
        <td style="float:left; text-align:left; width:100%">
            <div style="display:table; width:100%">
                <div style="width:100%;display:table-row">
                    <input type="checkbox" />
                    <label class="btn"> <span>A</span> 
                    </label>
                    <input type="checkbox" />
                    <label class="btn"> <span>B</span> 
                    </label>
                    <input type="checkbox" />
                    <label class="btn"> <span>C</span> 
                    </label>
                    <input type="checkbox" />
                    <label class="btn"> <span>D</span> 
                    </label>
                    <input type="checkbox" />
                    <label class="btn"> <span>E</span> 
                    </label>
                    <input type="checkbox" />
                    <label class="btn"> <span>F</span> 
                    </label>
                    <input type="checkbox" />
                    <label class="btn"> <span>G</span> 
                    </label>
                    <input type="checkbox" />
                    <label class="btn"> <span>H</span> 
                    </label>
                    <input type="checkbox" />
                    <label class="btn"> <span>I</span> 
                    </label>
                </div>
            </div>
    </tr>
</table>

CSS:

.btn {
    display: table - cell;
}

在 pc 和平板电脑视图中,它看起来很完美,从左侧和右侧都合理,但在移动视图中,是否可以将其分成两行以使其具有响应性?请看小提琴。

【问题讨论】:

    标签: html css responsive-design html-table


    【解决方案1】:

    这是你想要的:

    HTML

    <table class="table" cellpadding="0" border="0" width="100%" cellspacing="0">
        <thead>
            <tr>
                <th style="text-align:left" colspan="9"><strong> Color: </strong>
    
                </th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>
                    <input type="checkbox" />
                    <label class="btn"> <span>A</span> 
                    </label>
                </td>
                <td>
                    <input type="checkbox" />
                    <label class="btn"> <span>B</span> 
                    </label>
                </td>
                <td>
                    <input type="checkbox" />
                    <label class="btn"> <span>C</span> 
                    </label>
                </td>
                <td>
                    <input type="checkbox" />
                    <label class="btn"> <span>D</span> 
                    </label>
                </td>
                <td>
                    <input type="checkbox" />
                    <label class="btn"> <span>E</span> 
                    </label>
                </td>
                <td>
                    <input type="checkbox" />
                    <label class="btn"> <span>F</span> 
                    </label>
                </td>
                <td>
                    <input type="checkbox" />
                    <label class="btn"> <span>G</span> 
                    </label>
                </td>
                <td>
                    <input type="checkbox" />
                    <label class="btn"> <span>H</span> 
                    </label>
                </td>
                <td>
                    <input type="checkbox" />
                    <label class="btn"> <span>I</span> 
                    </label>
                </td>
            </tr>
        </tbody>
    </table>
    

    CSS

    .table {
        width: 100%;
    }
    .table caption {
        font-size: 15px;
        font-weight: 700;
        text-align: left;
        margin-bottom: 20px;
        color: #333;
        text-transform: uppercase;
    }
    .table thead {
        background-color: #444;
    }
    .table thead tr th {
        padding: 10px;
        font-weight: 700;
        color: #f2f2f2;
        text-transform: uppercase;
    }
    .table thead tr th:first-child {
        text-align: left;
    }
    .table tbody {
        background-color: #fcfcfc;
    }
    .table tbody tr td {
        padding: 10px;
        text-align: left;
    }
    .table tbody tr td:first-child {
        text-align: left;
        color: #333;
        font-weight: 700;
    }
    .table tbody tr:nth-of-type(odd) {
        background: #eee;
    }
    
    @media only screen and (min-width: 320px) and (max-width: 767px) {
        .table tbody {
            border: 0;
        }
        .table tbody tr {
            margin-bottom: 10px;
            display: block;
            border-bottom: 2px solid #ddd;
        }
        .table tbody td {
            display: block;
            text-align: right;
            font-size: 13px;
            border-bottom: 1px dotted #ccc;
        }
        .table tbody td:last-child {
            border-bottom: 0;
        }
        .table tbody td:before {
            content: attr(data-label);
            float: left;
            text-transform: uppercase;
            font-weight: bold;
        }
    }
    

    如您所见,手机和平板电脑的媒体查询最小宽度为 320 到最大宽度为 767

    基本上,从桌面查看转桌

    到手机/平板查看

    JSFiddle:http://jsfiddle.net/loginet/nctzk4f3/3/

    资源:https://css-tricks.com/accessible-simple-responsive-tables/?utm_source=dlvr.it&utm_medium=facebook

    【讨论】:

    • 感谢您的回答。这是一个更好的方法。它将帮助有需要的人,因为我已经离开了 IT 领域本身。再次感谢阿德里安。
    【解决方案2】:

    您可以使用媒体查询将 div 设置为 display: block;Demo

    将 CSS 留给较大的显示器,然后使用媒体查询来定位较小的显示器。我建议将标签和复选框包装在一起,以防止它们分开:

    HTML

    <div class="table-cell">
        <input type="checkbox" class="checkbox" />
        <label class="btn"> <span>A</span> 
        </label>
    </div>
    

    CSS

    .table {
        display: table;
        width: 100%;
    }
    
    .table-row {
        display: table-row;
        width: 100%;
    }
    
    .table-cell {
        display: table-cell;
    }
    
    @media screen and (max-width: 479px) {
        .table, .table-row {
            display: block;
        }
        .table-cell {
            display:inline-block;
        }
    }
    

    您可能需要根据自己的喜好更改标签的对齐方式。

    【讨论】:

    • 看看这是否有帮助Responsive Tables Demo
    • @brouxhaha 谢谢伙计..它可以按我的意愿工作..只需要根据要求进行一些修改..再次感谢。
    • @San 感谢您的建议。我已经检查过了,但无法解决上述问题。无论如何,上面的答案有效。
    猜你喜欢
    • 2011-09-01
    • 2014-09-23
    • 2014-06-29
    • 1970-01-01
    • 2021-01-02
    • 2015-12-04
    • 1970-01-01
    • 2013-08-05
    • 2013-02-13
    相关资源
    最近更新 更多