UIkit3.x文档

表格

轻松创建外观精美不同风格的表格。

用法

要应用此组件,请将.uk-table class 添加到 <table> 元素。

<table class="uk-table">
    <caption></caption>
    <thead>
        <tr>
            <th></th>
        </tr>
    </thead>
    <tfoot>
        <tr>
            <td></td>
        </tr>
    </tfoot>
    <tbody>
        <tr>
            <td></td>
        </tr>
    </tbody>
</table>
  • Table Caption
    Table Heading Table Heading Table Heading
    Table Footer Table Footer Table Footer
    Table Data Table Data Table Data
    Table Data Table Data Table Data
  • <table class="uk-table">
        <caption>Table Caption</caption>
        <thead>
            <tr>
                <th>Table Heading</th>
                <th>Table Heading</th>
                <th>Table Heading</th>
            </tr>
        </thead>
        <tfoot>
            <tr>
                <td>Table Footer</td>
                <td>Table Footer</td>
                <td>Table Footer</td>
            </tr>
        </tfoot>
        <tbody>
            <tr>
                <td>Table Data</td>
                <td>Table Data</td>
                <td>Table Data</td>
            </tr>
            <tr>
                <td>Table Data</td>
                <td>Table Data</td>
                <td>Table Data</td>
            </tr>
        </tbody>
    </table>

分隔线

添加.uk-table-divider class 在表格的行与行之间显示分隔线。

<table class="uk-table uk-table-divider">...</table>
  • Table Heading Table Heading Table Heading
    Table Data Table Data Table Data
    Table Data Table Data Table Data
    Table Data Table Data Table Data
  • <table class="uk-table uk-table-divider">
        <thead>
            <tr>
                <th>Table Heading</th>
                <th>Table Heading</th>
                <th>Table Heading</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Table Data</td>
                <td>Table Data</td>
                <td>Table Data</td>
            </tr>
            <tr>
                <td>Table Data</td>
                <td>Table Data</td>
                <td>Table Data</td>
            </tr>
            <tr>
                <td>Table Data</td>
                <td>Table Data</td>
                <td>Table Data</td>
            </tr>
        </tbody>
    </table>

条纹修饰

添加.uk-table-striped class为表格应用斑马条纹效果。

<table class="uk-table uk-table-striped">...</table>
  • Table Heading Table Heading Table Heading
    Table Data Table Data Table Data
    Table Data Table Data Table Data
    Table Data Table Data Table Data
  • <table class="uk-table uk-table-striped">
        <thead>
            <tr>
                <th>Table Heading</th>
                <th>Table Heading</th>
                <th>Table Heading</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Table Data</td>
                <td>Table Data</td>
                <td>Table Data</td>
            </tr>
            <tr>
                <td>Table Data</td>
                <td>Table Data</td>
                <td>Table Data</td>
            </tr>
            <tr>
                <td>Table Data</td>
                <td>Table Data</td>
                <td>Table Data</td>
            </tr>
        </tbody>
    </table>

悬停效果

添加.uk-table-hover class以在表行上显示鼠标悬停时的状态。

<table class="uk-table uk-table-hover">...</table>
  • Table Heading Table Heading Table Heading
    Table Data Table Data Table Data
    Table Data Table Data Table Data
    Table Data Table Data Table Data
  • <table class="uk-table uk-table-hover uk-table-divider">
        <thead>
            <tr>
                <th>Table Heading</th>
                <th>Table Heading</th>
                <th>Table Heading</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Table Data</td>
                <td>Table Data</td>
                <td>Table Data</td>
            </tr>
            <tr>
    
                <td>Table Data</td>
                <td>Table Data</td>
                <td>Table Data</td>
            </tr>
            <tr>
                <td>Table Data</td>
                <td>Table Data</td>
                <td>Table Data</td>
            </tr>
        </tbody>
    </table>

尺寸修改

添加.uk-table-small 或者 .uk-table-large class 到表格使其变得更大或更小。

<table class="uk-table uk-table-small">...</table>
  • Table Heading Table Heading Table Heading
    Table Data Table Data Table Data
    Table Data Table Data Table Data
    Table Data Table Data Table Data
  • <table class="uk-table uk-table-small uk-table-divider">
        <thead>
            <tr>
                <th>Table Heading</th>
                <th>Table Heading</th>
                <th>Table Heading</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Table Data</td>
                <td>Table Data</td>
                <td>Table Data</td>
            </tr>
            <tr>
                <td>Table Data</td>
                <td>Table Data</td>
                <td>Table Data</td>
            </tr>
            <tr>
                <td>Table Data</td>
                <td>Table Data</td>
                <td>Table Data</td>
            </tr>
        </tbody>
    </table>

边缘对齐

如果要移除第一列和最后一列内容外面的padding,使内容与表格边缘齐平,请添加.uk-table-justify class。

<table class="uk-table uk-table-justify">...</table>
  • Table Heading Table Heading Table Heading
    Table Data Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
    Table Data Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
  • <table class="uk-table uk-table-justify uk-table-divider">
        <thead>
            <tr>
                <th class="uk-width-small">Table Heading</th>
                <th>Table Heading</th>
                <th>Table Heading</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Table Data</td>
                <td>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</td>
                <td><button class="uk-button uk-button-default" type="button">Button</button></td>
            </tr>
            <tr>
                <td>Table Data</td>
                <td>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</td>
                <td><button class="uk-button uk-button-default" type="button">Button</button></td>
            </tr>
        </tbody>
    </table>

垂直对齐

要垂直居中放置表格内容,只需添加.uk-table-middle class。您也可以将该class应用在<tr> or <td> 元素上以进行更具体的选择。

<table class="uk-table uk-table-middle">...</table>
  • Table Heading Table Heading Table Heading
    Table Data Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
    Table Data Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
  • <table class="uk-table uk-table-middle uk-table-divider">
        <thead>
            <tr>
                <th class="uk-width-small">Table Heading</th>
                <th>Table Heading</th>
                <th>Table Heading</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Table Data</td>
                <td>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</td>
                <td><button class="uk-button uk-button-default" type="button">Button</button></td>
            </tr>
            <tr>
                <td>Table Data</td>
                <td>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</td>
                <td><button class="uk-button uk-button-default" type="button">Button</button></td>
            </tr>
        </tbody>
    </table>

响应式表格

如果你的表格的宽度大于它的容器元素,或者在特定的视口宽度上显得太大,那么只需将表格放入<div>元素中,并添加Utility 组件中的.uk-overflow-auto class到这个<div>元素。这样会创建一个容器,当其中的元素宽度大于容器本身时,该容器将提供一个水平的滚动条。

<div class="uk-overflow-auto">
    <table class="uk-table">...</table>
</div>
  • Table Heading Table Heading Table Heading Table Heading Table Heading Table Heading Table Heading Table Heading Table Heading Table Heading Table Heading Table Heading
    Table Data Table Data Table Data Table Data Table Data Table Data Table Data Table Data Table Data Table Data Table Data Table Data
    Table Data Table Data Table Data Table Data Table Data Table Data Table Data Table Data Table Data Table Data Table Data Table Data
    Table Data Table Data Table Data Table Data Table Data Table Data Table Data Table Data Table Data Table Data Table Data Table Data
  • <div class="uk-overflow-auto">
        <table class="uk-table uk-table-small uk-table-divider">
            <thead>
                <tr>
                    <th>Table Heading</th>
                    <th>Table Heading</th>
                    <th>Table Heading</th>
                    <th>Table Heading</th>
                    <th>Table Heading</th>
                    <th>Table Heading</th>
                    <th>Table Heading</th>
                    <th>Table Heading</th>
                    <th>Table Heading</th>
                    <th>Table Heading</th>
                    <th>Table Heading</th>
                    <th>Table Heading</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>Table Data</td>
                    <td>Table Data</td>
                    <td>Table Data</td>
                    <td>Table Data</td>
                    <td>Table Data</td>
                    <td>Table Data</td>
                    <td>Table Data</td>
                    <td>Table Data</td>
                    <td>Table Data</td>
                    <td>Table Data</td>
                    <td>Table Data</td>
                    <td>Table Data</td>
                </tr>
                <tr>
                    <td>Table Data</td>
                    <td>Table Data</td>
                    <td>Table Data</td>
                    <td>Table Data</td>
                    <td>Table Data</td>
                    <td>Table Data</td>
                    <td>Table Data</td>
                    <td>Table Data</td>
                    <td>Table Data</td>
                    <td>Table Data</td>
                    <td>Table Data</td>
                    <td>Table Data</td>
                </tr>
                <tr>
                    <td>Table Data</td>
                    <td>Table Data</td>
                    <td>Table Data</td>
                    <td>Table Data</td>
                    <td>Table Data</td>
                    <td>Table Data</td>
                    <td>Table Data</td>
                    <td>Table Data</td>
                    <td>Table Data</td>
                    <td>Table Data</td>
                    <td>Table Data</td>
                    <td>Table Data</td>
                </tr>
            </tbody>
        </table>
    </div>

小视口内堆叠

如果希望表格列在小屏幕上堆叠显示,请添加.uk-table-responsive class。

<table class="uk-table uk-table-responsive">...</table>
  • Table Heading Table Heading Table Heading
    Table Data Table Data Table Data
    Table Data Table Data Table Data
    Table Data Table Data Table Data
  • <table class="uk-table uk-table-responsive uk-table-divider">
        <thead>
            <tr>
                <th>Table Heading</th>
                <th>Table Heading</th>
                <th>Table Heading</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Table Data</td>
                <td>Table Data</td>
                <td>Table Data</td>
            </tr>
            <tr>
                <td>Table Data</td>
                <td>Table Data</td>
                <td>Table Data</td>
            </tr>
            <tr>
                <td>Table Data</td>
                <td>Table Data</td>
                <td>Table Data</td>
            </tr>
        </tbody>
    </table>

Note Resize the browser window to see the columns stack.


列宽

如果要修改列的宽度或内容,可以使用以下class之一。您只需要将class添加到列钟的其中一个单元格即可。建议添加到表头单元格上面。

Class Description
.uk-table-shrink 添加到<th><td>元素以减小列宽以适应其内容。
.uk-table-expand 添加到<th><td>元素扩展列的宽度以填满剩余空间,并应用最小宽度。
.uk-width-* 添加Width component中的这些class之一到<th><td>元素来调整列宽。
<th class="uk-table-shrink"></th>
  • Shrink Expand Width Small
    Table Data Table Data Table Data
    Table Data Table Data Table Data
  • <table class="uk-table uk-table-divider">
        <thead>
            <tr>
                <th class="uk-table-shrink">Shrink</th>
                <th class="uk-table-expand">Expand</th>
                <th class="uk-width-small">Width Small</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Table Data</td>
                <td>Table Data</td>
                <td>Table Data</td>
            </tr>
            <tr>
                <td>Table Data</td>
                <td>Table Data</td>
                <td>Table Data</td>
            </tr>
        </tbody>
    </table>

实用效果

若要优化表单元格及其特定内容的显示,可以添加以下class之一。

Class Description
.uk-table-link 如果要使整个单元格都成为链接,请添加这个class到<th><td>元素并插入<a>元素,添加Link 组件中的.uk-link-resetclass来重置链接的默认样式。
.uk-preserve-width 由于在UIkit中默认情况下图像是响应式的,因此在带有code>.uk-table-shrink class的表单元格中使用图片时会将图像宽度缩小到0。要避免这种情况,请将Base 组件中的.uk-preserve-width class 添加到<img> 元素。
.uk-text-truncate 当表格单元格使用固定宽度时,您可能会希望将Text 组件中的这个class添加到<td>元素来截断文本。
.uk-text-nowrap 添加Text 组件中的这个class来防止文本换行,例如在使用.uk-table-shrink class时。
<!-- Table link -->
<td class="uk-table-link">
    <a class="uk-link-reset" href=""></a>
</td>

<!-- Preserve width -->
<td>
    <img class="uk-preserve-width">
</td>

<!-- Text truncate -->
<td class="uk-text-truncate"></td>

<!-- Text nowrap -->
<td class="uk-text-nowrap"></td>
  •   Preserve Expand + Link Truncate Shrink + Nowrap
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Lorem ipsum dolor
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Lorem ipsum dolor
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Lorem ipsum dolor
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Lorem ipsum dolor
  • 
    <div class="uk-overflow-auto">
        <table class="uk-table uk-table-hover uk-table-middle uk-table-divider">
            <thead>
                <tr>
                    <th class="uk-table-shrink"></th>
                    <th class="uk-table-shrink">Preserve</th>
                    <th class="uk-table-expand">Expand + Link</th>
                    <th class="uk-width-small">Truncate</th>
                    <th class="uk-table-shrink uk-text-nowrap">Shrink + Nowrap</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td><input class="uk-checkbox" type="checkbox"></td>
                    <td><img class="uk-preserve-width uk-border-circle" src="/skin/ukv3/images/avatar.jpg" width="40" alt=""></td>
                    <td class="uk-table-link">
                        <a class="uk-link-reset" href="">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor.</a>
                    </td>
                    <td class="uk-text-truncate">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor.</td>
                    <td class="uk-text-nowrap">Lorem ipsum dolor</td>
                </tr>
                <tr>
                    <td><input class="uk-checkbox" type="checkbox"></td>
                    <td><img class="uk-preserve-width uk-border-circle" src="/skin/ukv3/images/avatar.jpg" width="40" alt=""></td>
                    <td class="uk-table-link">
                        <a class="uk-link-reset" href="">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor.</a>
                    </td>
                    <td class="uk-text-truncate">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor.</td>
                    <td class="uk-text-nowrap">Lorem ipsum dolor</td>
                </tr>
                <tr>
                    <td><input class="uk-checkbox" type="checkbox"></td>
                    <td><img class="uk-preserve-width uk-border-circle" src="/skin/ukv3/images/avatar.jpg" width="40" alt=""></td>
                    <td class="uk-table-link">
                        <a class="uk-link-reset" href="">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor.</a>
                    </td>
                    <td class="uk-text-truncate">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor.</td>
                    <td class="uk-text-nowrap">Lorem ipsum dolor</td>
                </tr>
                <tr>
                    <td><input class="uk-checkbox" type="checkbox"></td>
                    <td><img class="uk-preserve-width uk-border-circle" src="/skin/ukv3/images/avatar.jpg" width="40" alt=""></td>
                    <td class="uk-table-link">
                        <a class="uk-link-reset" href="">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor.</a>
                    </td>
                    <td class="uk-text-truncate">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor.</td>
                    <td class="uk-text-nowrap">Lorem ipsum dolor</td>
                </tr>
            </tbody>
        </table>
    </div>