数据样式

表格

📗表格可以非常快速的部署数据,灵活控制表格样式是必要的。

表格的单元格不能设置外边距 📌

定制表格 💡

📗 除了使用 table 标签绘制表格外,也可以使用样式绘制。

样式规则说明
table对应 table
table-caption对应 caption
table-row对应 表格行 tr
table-row-group对应 tbody
table-header-group对应 thead
table-footer-group对应 tfoot
<article class="table">
    <nav>表格标题</nav>
    <section>
        <ul>
            <li>标题</li>
            <li>说明</li>
        </ul>
    </section>
    <section>
        <ul>
            <li>MDN</li>
            <li>mdn.com</li>
        </ul>
        <ul>
            <li>TS</li>
            <li>typescript.cn</li>
        </ul>
    </section>
    <section>
        <ul>
            <li>hello</li>
            <li>world</li>
        </ul>
    </section>
</article>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

/* table */ 
.table {
    display: table;
    border: solid 1px #ddd;
}

/* table-caption 将元素定位到表格上方的标题位置 */ 
.table nav {
    display: table-caption;
    text-align: center;
    background: black;
    color: white;
    padding: 10px;
}

/* table-header-group 表格 header */
.table section:nth-of-type(1) {
    font-weight: bold;
    display: table-header-group;
    background: #555;
    color: white;
}

/* table-row-group 表格 body */
.table section:nth-of-type(2) {
    display: table-row-group;
}
/* table-footer-group 表格 footer */
.table section:nth-of-type(3) {
    display: table-footer-group;
    background: #f3f3f3;
}

/* table-row 表格 行 */
.table section ul {
    display: table-row;
}

/* table-cell 单元格 */
.table section ul li {
    padding: 10px;
    display: table-cell;
    border: solid 1px #ddd;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

表格标题

通过 caption-side 可以设置标题位置,值可以设置为 top | bootom

<style>
    table caption {
        background: black;
        color: white;
        padding: 10px;
        caption-side: top;
    }
</style>

<table border="1">
    <caption>表格标题</caption>
    <tr>
        <td>单元格区域</td>
        <td>单元格区域</td>
    </tr>
</table>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

内容对齐

水平对齐使用 text-align 文本对齐规则

table tr td {
    height: 100px;
    text-align: center;
}
1
2
3
4

垂直对齐使用 vertical-align 处理

属性说明
top顶对齐
middle垂直居中(默认)
bottom底部对齐
table tr td {
    height: 100px;
    vertical-align: bottom;
    text-align: center;
}
1
2
3
4
5

颜色设置

为表格设置颜色与普通标签相似,可以为 table | thead | tbody | caption | tfoot| tr| td 设置颜色样式。

table tr {
    color: white;
}
table tr:nth-child(odd) {
    background: red;
}
table tr:nth-child(even) {
    background: #067db4;
}
1
2
3
4
5
6
7
8
9

边框间距

设置单元格间距,设置间距上下左右 20px10px

table {
    border-spacing: 20px 10px;
}
1
2
3

边框合并

默认表格边框间是有间距的,以下示例将边框合并形成细线表格 📌

table {
  border-collapse: collapse;
}
1
2
3

隐藏单元格

table {
    empty-cells: hide;
}
1
2
3

无边框表格

table {
    border: none;
    border-collapse: collapse;
}

table td {
    border: none;
    border-right: solid 1px #ddd;
    border-bottom: solid 1px #ddd;
}

table tr:first-child td {
    border-top: solid 1px #ddd;
}

table td:last-child {
    border-right: none;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

数据表格

可以为表格元素使用伪类控制样式,下例中使用 hover 伪类样式

table tr:hover {
    background: #ddd;
    cursor: pointer;
}
1
2
3
4

列表

列表符号

📗 使用 list-style-type 来设置列表样式,规则是继承的,所以在ul 标签上设置即可。

描述
none无标记。
disc默认。标记是实心圆。
circle标记是空心圆。
square标记是实心方块。
decimal标记是数字。
decimal-leading-zero0开头的数字标记。(01, 02, 03, 等。)
lower-roman小写罗马数字(i, ii, iii, iv, v, 等。)
upper-roman大写罗马数字(I, II, III, IV, V, 等。)
lower-alpha小写英文字母The marker is lower-alpha (a, b, c, d, e, 等。)
upper-alpha大写英文字母The marker is upper-alpha (A, B, C, D, E, 等。)
lower-greek小写希腊字母(alpha, beta, gamma, 等。)
lower-latin小写拉丁字母(a, b, c, d, e, 等。)
upper-latin大写拉丁字母(A, B, C, D, E, 等。)
hebrew传统的希伯来编号方式
armenian传统的亚美尼亚编号方式
georgian传统的乔治亚编号方式(an, ban, gan, 等。)
cjk-ideographic简单的表意数字
hiragana标记是:a, i, u, e, o, ka, ki, 等。(日文片假名)
katakana标记是:A, I, U, E, O, KA, KI, 等。(日文片假名)
hiragana-iroha标记是:i, ro, ha, ni, ho, he, to, 等。(日文片假名)
katakana-iroha标记是:I, RO, HA, NI, HO, HE, TO, 等。(日文片假名)

隐藏列表符号(实际上设置的是li元素,继承了该属性)

ul {
    list-style-type: none;
}
1
2
3

👾 自定义列表样式

ul li {
    /* list-style-image: url(item.png); */
    list-style-image: radial-gradient(10px 10px, red, black);
}
1
2
3
4

符号位置

控制符号显示在内容外面还是内部

选项说明
inside内部 (::marker伪元素处在li元素内部)
outside外部 (默认)
ul {
    list-style-position: inside;
}
1
2
3

组合定义

可以一次定义列表样式

ul {
    list-style: circle inside;
}
1
2
3

背景符号 💡

通过为li元素设置背景图片,也可以达到效果

ul li {
    background: url(item.png) no-repeat 0 6px;
    background-size: 20px 20px;
    list-style: none;
    text-indent: 2em;
    border-bottom: 1px solid #ddd;
    padding: 6px;
}
1
2
3
4
5
6
7
8

多图背景定义

ul {
    list-style-type: none;
}

ul li {
    background-image: url(item.png), url(bg.png);
    background-repeat: no-repeat, repeat;
    background-size: 20px 20px, 100%;
    background-position: 2px 2px, 0 0;
    text-indent: 2em;
    border-bottom: solid 1px #ddd;
    margin-bottom: 10px;
    padding-bottom: 5px;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

追加内容 👾

基本使用

使用伪类 ::before 向前添加内容,使用 ::after 向后面添加内容。

a::after {
  content: " (坚持努力) ";
}
1
2
3

提取属性 💡

attr使用属性值添加内容,可以使用标准属性与自定义属性。

<style>
  a::after {
    content: attr(href);
  }
</style>

<a href="blog.caffreygo.com">博客</a>
1
2
3
4
5
6
7

通过属性值添加标签提示

<style>
    a {
        position: relative;
    }

    a:hover::before {
        /* content: attr(data-link); */
        content: "URL: "attr(data-link);
        background: #555;
        color: white;
        position: absolute;
        top: 20px;
        padding: 3px 10px;
        border-radius: 10px;
    }
</style>
<a data-link="blog.caffreygo.com">博客</a>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

自定义表单

<style>
    body {
        padding: 80px;
    }

    .field {
        position: relative;
    }

    input {
        border: none;
        outline: none;
    }

    .field::after {
        content: '';
        background: linear-gradient(to right, white, red, green, blue, white);
        height: 30px;
        position: relative;
        width: 100%;
        height: 1px;
        display: block;
        left: 0px;
        right: 0px;
    }

    .field:hover::before {
        content: attr(data-placeholder);
        position: absolute;
        top: -20px;
        left: 0px;
        color: #555;
        font-size: 12px;
    }
</style>

<div class="field" data-placeholder="请输入少于100字的标题">
    <input type="text" id="name">
</div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
上次更新: 2022/4/9 03:39:22
贡献者: Jerry Chen, JerryChen