css - 如何在 CSS 中创建进度步骤
问题描述
我有一个项目列表,我想在 CSS 中将其转化为进度步骤。
ol {
width: 800px;
}
li {
display: inline-block;
color: transparent;
height: 25px;
width: 25px;
background-color: #abc;
border-radius: 50%;
border: 2px solid #08f;
margin-right: 150px;
}
li:last-child {
margin-right: 0;
}
li:not(:last-child)::before {
content: "";
border: 2px solid #08f;
margin-left:25px;
width: 153px;
display: inline-block;
}
<ol>
<li>Step 1</li>
<li>Step 2</li>
<li>Step 3</li>
<li>Step 4</li>
</ol>
我理想地想做的是:
- 阻止第 4 步从底部消失
- 将 的内容
<li>
用作圆圈上方的标签 - 使总宽度等于视口宽度的 80%
我只是想教自己一些更高级的 CSS,而且我已经在其他地方看到过这种模式 - 但我已经尝试了一个小时左右才到达那里,没有任何乐趣。
这对我来说是一个学习练习,所以如果你有时间,我会喜欢答案的一些解释。
谢谢,
解决方案
body {
display: grid;
place-items: center;
margin: 0;
padding: 36px;
}
ol {
display: flex;
width: 80%;
padding: 12px 0;
}
li {
position: relative;
padding: 0;
flex-grow: 1;
height: 2px;
background: #08f;
display: flex;
align-items: center;
}
li:last-child {
margin-right: 0;
flex-grow: 0;
}
li::after {
content: "";
position: absolute;
left: 0;
display: inline-block;
color: transparent;
height: 24px;
width: 24px;
background: #abc;
border-radius: 50%;
border: 2px solid #08f;
}
span {
position: absolute;
top: -36px;
left: 12px;
width: max-content;
transform: translateX(-50%);
z-index: 1;
}
<ol>
<li><span>Step 1</span></li>
<li><span>Step 2</span></li>
<li><span>Step 3</span></li>
<li><span>Step 4</span></li>
</ol>
在我的代码中,节点是伪元素,我使用了 flex-grow 属性,以便正确分布规则(即 li 标签)。font-size: 0
隐藏文本并将其从元素的内容大小中删除。
- - 编辑:
我删除了 font-size: 0 并为标签和 css 添加了 span 标签来定位它。
推荐阅读
- vue.js - 无法在 Storybook 自己的模板中使用 Pug
- python - Python steamlit 选择框菜单返回字符串,但我需要 dict 或 list
- python - sklearn 自定义变压器拟合问题
- ios - 来自委托方法的值传递是 nil Swift
- mysql - MySQL表更新后获取受影响行的数据
- django - django 应用程序的 VScode unittest 测试发现设置
- java - 如何在 IRIS Sql 中使用 %MATCHES 进行正则表达式
- node.js - 针对这种特定情况在 EJS 中注入变量
- amazon-web-services - Aws cloudformation join 和 sub 在一起
- android - RecyclerView.Adapter。重写的方法 onCreateViewHolder 参数在 viewType 参数中始终为零