使用伪元素(::after)结合绝对定位实现步骤间连接线,通过left和width的calc计算精准定位线条起止位置;2. 为每个步骤设置position: relative作为定位上下文,伪元素通过top: 50%和transform: translatey(-50%)垂直居中;3. 利用z-index确保圆圈和内容显示在连接线上层;4. 通过.active类控制激活状态的颜色变化,并使用.step.active::after和.step.active ~ .step::after使当前及之前步骤的连接线同步变色;5. 最后一个步骤通过display: none隐藏伪元素避免多余线条;该方案无需额外html标签,保持结构简洁且样式灵活可控,完整实现了美观的步骤进度连接线效果。

创建CSS步骤进度连接线,通常会利用伪元素(
::before
::after
position: absolute
要实现步骤进度连接线,我们的核心思路是为每个步骤元素(或者除了最后一个之外的步骤元素)添加一个伪元素,然后通过绝对定位将这个伪元素放置在它应该连接的位置。
以下是一个经典的水平进度条实现方案:
立即学习“前端免费学习笔记(深入)”;
首先,我们有一个包裹所有步骤的容器,每个步骤内部包含一个表示进度的圆圈和一个标题。
<div class="progress-steps">
<div class="step active">
<div class="step-circle">1</div>
<div class="step-title">购物车</div>
</div>
<div class="step">
<div class="step-circle">2</div>
<div class="step-title">确认订单</div>
</div>
<div class="step">
<div class="step-circle">3</div>
<div class="step-title">付款</div>
</div>
<div class="step">
<div class="step-circle">4</div>
<div class="step-title">完成</div>
</div>
</div>接着是关键的CSS部分。这里面包含了我们如何利用伪元素和绝对定位来绘制连接线,以及如何处理不同状态下的样式。
.progress-steps {
display: flex; /* 使用Flexbox让步骤横向排列 */
justify-content: space-between; /* 步骤之间均匀分布空间 */
align-items: center; /* 垂直居中对齐 */
padding: 20px;
background-color: #f9f9f9;
border-radius: 8px;
position: relative; /* 为可能存在的整体进度条或未来扩展留出定位上下文 */
overflow: hidden; /* 防止伪元素超出容器 */
}
.step {
flex: 1; /* 每个步骤占据等宽空间 */
text-align: center;
position: relative; /* 为伪元素提供定位上下文 */
display: flex; /* 内部元素(圆圈和标题)垂直居中 */
flex-direction: column;
align-items: center;
z-index: 1; /* 确保步骤内容(特别是圆圈)在连接线之上 */
}
.step-circle {
width: 40px;
height: 40px;
border-radius: 50%;
background-color: #ccc; /* 默认颜色 */
color: #fff;
display: flex;
justify-content: center;
align-items: center;
font-weight: bold;
font-size: 1.1em;
transition: background-color 0.3s ease; /* 动画效果 */
position: relative; /* 确保圆圈在连接线之上 */
z-index: 2; /* 确保圆圈始终在最上层 */
}
.step-title {
margin-top: 10px;
color: #666;
font-size: 0.9em;
transition: color 0.3s ease;
}
/* 核心:使用::after伪元素创建连接线 */
.step:not(:last-child)::after {
content: '';
position: absolute;
top: 50%; /* 垂直居中 */
left: calc(50% + 20px); /* 从当前圆圈的右边缘开始(圆圈宽度40px,半径20px) */
width: calc(100% - 40px); /* 线的长度,覆盖到下一个圆圈的左边缘 */
height: 4px; /* 线的粗细 */
background-color: #ccc; /* 默认线的颜色 */
transform: translateY(-50%); /* 垂直方向的精确居中 */
z-index: 0; /* 将线放在圆圈的下方 */
transition: background-color 0.3s ease; /* 动画效果 */
}
/* 激活状态的样式 */
.step.active .step-circle {
background-color: #007bff; /* 激活状态的圆圈颜色 */
}
.step.active .step-title {
color: #007bff; /* 激活状态的文字颜色 */
}
/* 激活步骤后的连接线也变为激活状态的颜色 */
.step.active::after, /* 激活步骤自己的连接线 */
.step.active ~ .step::after { /* 激活步骤后面所有步骤的连接线(已完成的线) */
background-color: #007bff;
}
/* 针对最后一个步骤,它不需要连接线 */
.progress-steps .step:last-child::after {
display: none;
}这段CSS的巧妙之处在于
::after
left
width
left: calc(50% + 20px)
width: calc(100% - 40px)
z-index
说实话,当我第一次尝试制作这种进度条时,也考虑过直接在HTML里加
<div>
::before
::after
伪元素是CSS提供的一种机制,允许你在不增加额外HTML标签的情况下,生成并插入内容。对于进度条的连接线这种纯粹的装饰性元素,它们简直是完美的选择。它们依附于现有的HTML元素,却又独立于正常的文档流,非常适合用来绘制这些“非内容”的视觉组件。
而绝对定位(
position: absolute
position: absolute
position: relative
top
left
right
bottom
width
以上就是CSS如何创建步骤进度连接线?伪元素+绝对定位技巧的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号