是否可以进行显示:表格单元布局是否响应?

时间:2013-12-25 14:46:45

标签: html css responsive-design html-table

在我的代码中有一个表格,其中我有分区,其中表格行包含水平复选框。这是我的示例代码,完整代码在小提琴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;
}

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

2 个答案:

答案 0 :(得分:11)

您可以使用媒体查询将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;
    }
}

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

答案 1 :(得分:4)

这是你想要的:

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

基本上,将桌面从桌面查看

enter image description here

观看手机/平板电脑

enter image description here

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

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