当前位置: 首页 > article >正文

CSS小玩意儿:目录

一,效果

在这里插入图片描述

二,代码

第 1 步:基础布局

创建一个显示内容的框框。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Category List</title>
    <style>
        .category-list-container {
            padding: 0;
            max-width: 300px;
        }

        .category-list {
            list-style: none;
            padding: 0;
            margin: 0;
        }

        .category-item {
            display: flex;  /* 让子元素使用 Flex 布局 */
            justify-content: space-between; /* 让 name 靠左,count 靠右 */
            align-items: center; /* 垂直居中 */
            padding: 8px 0;
            font-size: 16px;
        }
    </style>
</head>
<body>
<div class="category-list-container">
    <ul class="category-list">
        <li class="category-item">
            <span class="name">Category 1</span>
            <span class="count">12</span>
        </li>
        <li class="category-item">
            <span class="name">Category 2</span>
            <span class="count">34</span>
        </li>
    </ul>
</div>
</body>
</html>

在这里插入图片描述

✅ name 靠左
✅ count 靠右
✅ 但中间没有 - 号填充,下一步解决!

第 2 步:添加 - 号的分隔符

👉 在 category-item 内新增 dash 元素,并用 CSS 让它填充 name 和 count 之间的空隙。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Category List</title>
    <style>
        .category-list-container {
            padding: 0;
            max-width: 300px;
        }

        .category-list {
            list-style: none;
            padding: 0;
            margin: 0;
        }

        .category-item {
            display: flex; /* 让子元素使用 Flex 布局 */
            justify-content: space-between; /* 让 name 靠左,count 靠右 */
            align-items: center; /* 垂直居中 */
            padding: 8px 0;
            font-size: 16px;
        }

        .dash {
            flex-grow: 1; /* 让它填充空隙 */
            text-align: center; /* 让文本居中 */
            color: #999;
            font-weight: bold;
            margin: 0 8px; /* 控制 name 和 count 之间的间距 */
        }
    </style>
</head>
<body>
<div class="category-list-container">
    <ul class="category-list">
        <li class="category-item">
            <span class="name">Category 1</span>
            <span class="dash">-</span>
            <span class="count">12</span>
        </li>
        <li class="category-item">
            <span class="name">Category 2</span>
            <span class="dash">-</span>
            <span class="count">34</span>
        </li>
    </ul>
</div>
</body>
</html>

在这里插入图片描述
✅ dash 元素填充了 name 和 count 之间的空间
✅ 但目前 - 号数量固定,如果 name 或 count 变长,- 不会自动填充,下一步解决!

第 3 步:让 - 号自动填充

👉 用 ::before 伪元素,让 dash 内的 - 号动态增长。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Category List</title>

    <style>
        .category-list-container {
            padding: 0;
            max-width: 300px;
        }

        .category-list {
            color: #9a9999;
            list-style: none; /* 去掉默认列表样式 */
            padding: 0;
            margin: 0;
        }

        .category-item {
            display: flex; /* 使用 Flexbox 布局 */
            align-items: center; /* 垂直居中 */
            justify-content: space-between; /* 两端对齐 */
            padding: 8px 0; /* 调整间距 */
            font-size: 16px;
        }

        .name {
            flex-shrink: 0; /* 防止被压缩 */
            transition: transform 0.4s ease-out;
        }

        .dash {
            flex-grow: 1; /* 自动填充空白区域 */
            text-align: center;
            color: #999;
            font-weight: bold;
            margin: 0 8px; /* 控制分隔符与 name 和 count 的间距 */
            overflow: hidden;
            white-space: nowrap;
        }

        /* 伪元素创建动态的 '-' */
        .dash::before {
            content: "------------------------------------------------"; /* 长度足够的 - 号 */
            display: block;
            overflow: hidden;
            white-space: nowrap;
        }

        .count {
            flex-shrink: 0; /* 防止被压缩 */
        }
    </style>
</head>
<body>
<div class="category-list-container">
    <ul class="category-list">
        <li class="category-item">
            <span class="name">Category 1</span>
            <span class="dash"></span>
            <span class="count">2</span>
        </li>
        <li class="category-item">
            <span class="name">Category 2111</span>
            <span class="dash"></span>
            <span class="count">34111111</span>
        </li>
        <li class="category-item">
            <span class="name">Category 3</span>
            <span class="dash"></span>
            <span class="count">56</span>
        </li>
    </ul>
</div>
</body>
</html>

http://www.kler.cn/a/581337.html

相关文章:

  • 树莓派4B使用Ubuntu20.04连接不上热点
  • conda创建Python虚拟环境的原理
  • 基于 Vue 的Deepseek流式加载对话Demo
  • 基于python下载ERA5小时尺度和月尺度的数据
  • 【反无人机数据集】【目标检测】基于深度学习和距离分析的无人机检测图像处理技术应用
  • 基于MATLAB的冰块变化仿真
  • XTDrone调试报错问题集锦
  • 动态规划详解(二):从暴力递归到动态规划的完整优化之路
  • NLP常见任务专题介绍(2)-多项选择任务(MultipleChoice)训练与推理模板
  • SpringBoot开发——整合SpringReport开源报表工具
  • Git的命令学习——适用小白版
  • Android实现Socket通信
  • Chrome 扩展开发 API实战:Bookmarks(二)
  • Python高级之操作Mysql
  • 华为OD机试 - 平均像素值-贪心算法(Java 2024 E卷 100分)
  • 【区块链+ 医疗健康】基于区块链和AI 技术的儿童近视防控大数据平台 | FISCO BCOS 应用案例
  • iTextSharp-PDF批量导出
  • 3.3.2 用仿真图实现点灯效果
  • 软考高级信息系统项目管理师笔记-第22章组织通用治理
  • nginx的使用