纯css实现圆柱体-超简单!
写在前面的话:
在日常的工作中遇到一个需求,需要用圆柱体来表现每个项目的比例,首先想到的会事使用echarts中的柱状图来表现,但是为了一个圆柱体引用那么庞大的js库有点不合时宜的味道。其次是想到使用canvas画布工具里封装一个小插件供系统使用,可是要百度啥的,目前俺还不是很能熟练使用canvas,而且考虑再三之后使用纯css 是可以满足目前的开发要求的。
正文:
首先定义结构:
点击查看代码
<div class="lui-column-bg"> <div class="lui-inner" style="height: 50%;"> </div> </div>
.lui-column-bg 用来做父容器其中的.lui-inner 作为子容器其高度和父容器的高度的百分比作为 项目比。
其次是css,使用伪类元素before和after作为圆柱的上下底面。使用渐变的背景使得圆柱体更加立体一点。
点击查看代码
.lui-column-bg {
position: relative;
width: 50px;
height: 90px;
margin: 100px auto;
background-color: transparent;
background-color: rgba(0, 199, 159, 0.2);
}
.lui-column-bg:before {
position: absolute;
content: '';
display: block;
height: 20px;
width: 100%;
border-radius: 50%;
top: -10.5px;
z-index: 1;
background-color: rgb(101 221 197);
}
.lui-column-bg:after {
position: absolute;
content: '';
display: block;
height: 15px;
width: 100%;
border-radius: 50%;
bottom: -10px;
background-color: rgb(0, 255, 204);
}
.lui-inner {
position: absolute;
bottom: 0;
width: 100%;
height: 50%;
background-image: linear-gradient(to top, rgb(0, 255, 204), rgb(0, 199, 159));
text-align: center;
}
.lui-inner::before {
position: absolute;
content: '';
display: block;
height: 20px;
width: 100%;
background-color: rgb(0, 199, 159);
border-radius: 50%;
top: -10.5px;
z-index: 1;
}
.lui-inner:after {
position: absolute;
content: '';
display: block;
height: 15px;
width: 100%;
border-radius: 50%;
background-color: white;
bottom: -10px;
}
效果如下:
最后附上完整的代码:
点击查看代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>css实现圆柱体</title> <style> .lui-column-bg { position: relative; width: 50px; height: 90px; margin: 100px auto; background-color: transparent; background-color: rgba(0, 199, 159, 0.2); } .lui-column-bg:before { position: absolute; content: ''; display: block; height: 20px; width: 100%; border-radius: 50%; top: -10.5px; z-index: 1; background-color: rgb(101 221 197); } .lui-column-bg:after { position: absolute; content: ''; display: block; height: 15px; width: 100%; border-radius: 50%; bottom: -10px; background-color: rgb(0, 255, 204); } .lui-inner { position: absolute; bottom: 0; width: 100%; height: 50%; background-image: linear-gradient(to top, rgb(0, 255, 204), rgb(0, 199, 159)); text-align: center; } .lui-inner::before { position: absolute; content: ''; display: block; height: 20px; width: 100%; background-color: rgb(0, 199, 159); border-radius: 50%; top: -10.5px; z-index: 1; } .lui-inner:after { position: absolute; content: ''; display: block; height: 15px; width: 100%; border-radius: 50%; background-color: white; bottom: -10px; } </style> </head> <body> <div class="lui-column-bg"> <div class="lui-inner" style="height: 50%;"> </div> </div> </body> </html>
感觉还可以的话留下你的赞是对我最大的鼓励@_@
微信公公众号 搜索 “不正经小前端” 并关注,和我一起学习海量前端技术
本文来自博客园,作者:七分暖,转载请注明原文链接:https://www.cnblogs.com/lin494910940/p/15101341.html
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· [翻译] 为什么 Tracebit 用 C# 开发
· 腾讯ima接入deepseek-r1,借用别人脑子用用成真了~
· Deepseek官网太卡,教你白嫖阿里云的Deepseek-R1满血版
· DeepSeek崛起:程序员“饭碗”被抢,还是职业进化新起点?
· RFID实践——.NET IoT程序读取高频RFID卡/标签