Skip to content

数据样式

表格标题

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

边框间距

设置单元格间距border-spacing,设置间距上下 `10px ,左右 50px。

table {
    border-spacing: 50px 10px;
}

边框合并

默认表格边框间是有间距的,border-collapse可以将边框进行合并。

table {
  border-collapse: collapse;
}

隐藏单元格

empty-cells可以隐藏没有内容的单元格。

html
<style>
    table.love-table-cell-hide {
        empty-cells: hide;
    }
</style>
<table border="1" class="love-table-cell-hide">
    <tr>
        <td>第一列</td>
        <td>有内容</td>
    </tr>
    <tr>
        <td></td>
        <td>无内容</td>
    </tr>
</table>

image-20241103165145417

无边框表格

无边框表格就是利用元素的伪类属性作用在相应的tr元素上,通过:first-child:last-child等属性添加或者隐藏相应的边框。

数据表格

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

html
<style>
    table.love-table-data-table,
    td {
        border: none;
        font-size: 14px;
        border-collapse: collapse;
    }

    table.love-table-data-table tbody tr:hover {
        background: #ddd;
        cursor: pointer;
    }

    table.love-table-data-table td {
        border-top: solid 1px #ccc;
        padding: 10px;
    }
</style> 
<table border="1" class="love-table-data-table">
    <thead>
        <tr>
            <td>编号</td>
            <td>标题</td>
            <td>网址</td>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>1</td>
            <td>2</td>
            <td>3</td>
        </tr>
        <tr>
            <td>2</td>
            <td>3</td>
            <td>4</td>
        </tr>
        <tr>
            <td>3</td>
            <td>4</td>
            <td>5</td>
        </tr>
</table>

列表

列表符号

使用 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, 等。(日文片假名)

隐藏列表符号

css
ul {
  list-style: none; /* 任选其一 */
  list-style-type: none;
}

自定义列表样式

css
ul li {
  list-style-image: url(model.png);/* 任选其一 */
  list-style-image: radial-gradient(10px 10px, red, black); 
  list-style-image: linear-gradient(45deg, red, black);
}

符号位置

控制符号显示在内容外面还是内部,list-style-position属性指示如何相对于对象的内容绘制列表项标记。

选项说明
inside列表项目标记放置在文本以内,且环绕文本根据标记对齐。
outside默认值。保持标记位于文本的左侧。列表项目标记放置在文本以外,且环绕文本不根据标记对齐。
css
ul {
  	list-style-position: inside;
}

image-20241103180912498

组合定义

可以一次定义列表样式

ul {
    list-style: circle inside;
}

追加内容

基本使用

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

css
a::after {
  content: " (坚持努力) ";
}

提取属性

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

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

<a href="待提取内容">提取</a>

通过属性值添加标签提示

html
<style>
    a.love-pseudo-class-show {
    	position: relative;
    }
    a.love-pseudo-class-show:hover {
        &::before {
        content: "URL: " attr(data-url);
        background: #555;
        color: white;
        position: absolute;
        top: 20px;
        padding: 3px 10px;
        border-radius: 10px;
    }
}
</style>
<a class="love-pseudo-class-show" href="https://www.dotohi.com" data-url="https://www.dotohi.com">静思田园</a>

静思田园

定制表格

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

样式规则说明
table对应 table
table-caption对应 caption
table-row对表 tr
table-row-group对应 tbody
table-header-group对应 thead
table-footer-group对应 tfoot
html
<style>
    .love-table {
        display: table;
        border: 1px solid black;
    }

    .love-table nav {
        display: table-caption;
        text-align: center;
        color: gray;
        padding: 10px;
    }

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

    .love-table section:nth-of-type(2) {
        display: table-row-group;
    }

    .love-table section:nth-of-type(3) {
        display: table-footer-group;
        background: #f3f3f3;
    }

    .love-table section ul {
        display: table-row;
    }

    .love-table section ul li {
        display: table-cell;
        padding: 10px;
        border: 1px solid black;
    }
</style>
<article class="love-table">
    <nav>标题</nav>
    <section>
        <ul>
            <li>标题</li>
            <li>说明</li>
        </ul>
    </section>
    <section>
        <ul>
            <li>1</li>
            <li>一</li>
        </ul>
        <ul>
            <li>2</li>
            <li>二</li>
        </ul>
    </section>
    <section>
        <ul>
            <li>footer</li>
            <li>脚</li>
        </ul>
    </section>
</article>