CSS中如何用Flex布局做柱状图的效果?
Admin 2022-02-22 群英技术资讯 1131 次浏览
这篇文章给大家分享的是CSS中用Flex布局做柱状图效果的方法。小编觉得挺实用的,因此分享给大家做个参考,对大家学习和了解Flex布局会有帮助,文中的示例代码介绍得很详细,有需要的朋友可以参考,接下来就跟随小编一起了解看看吧。
以下是一个用Flex布局的柱状图:

HTML:
<div class="his_box">
<div>成绩分布直方图</div>
<div class="histogram">
<div><div>10</div></div>
<div><div>8</div></div>
<div><div>15</div></div>
<div><div>12</div></div>
<div><div>5</div></div>
</div>
</div>
CSS:
.his_box{ /*盒子*/
width: 400px;
height: 220px;
border: solid 1px #1E90FF;
display: flex;
flex-direction: column;
align-items: center;
}
.histogram{ /*直方图*/
display: flex;
}
.histogram>div{ /*一条图块*/
width: 30px;
height: 200px; /*100%时的块高度*/
font-size: 14px;
text-align: center;
margin-right: 5px;
display: flex;
flex-direction: column-reverse;
}
.histogram>div:nth-child(3n) div{ /*图块颜色*/
background-color: #ff404b;
}
.histogram>div:nth-child(3n+1) div{
background-color: #99CCFF;
}
.histogram>div:nth-child(3n+2) div{
background-color: #F0AD4E;
}
.histogram>div:nth-child(1) div{
flex: 0 0 50%; /*20为100%,50%就是10*/
}
.histogram>div:nth-child(2) div{
flex: 0 0 40%; /*8/20*/
}
.histogram>div:nth-child(3) div{
flex: 0 0 75%; /*15/20*/
}
.histogram>div:nth-child(4) div{
flex: 0 0 60%; /*12/20*/
}
.histogram>div:nth-child(5) div{
flex: 0 0 25%; /*5/20*/
}
本例中,图块的最高点为20,每条柱子的高度按比例定义:第1条数据为10,高度是50%;第2条数据为8,高度为40%,以此类推。
图块颜色采用3色循环使用。
布局时,最外层容器使用了align-items: center;使容器内元素整体居中。
直方图模块使用了display: flex;让模块中的柱子横向排列。
每条柱子也是flex模块,但它的布局方向是纵向的,且方向是从下到上的 flex-direction: column-reverse;
如果想做成纵向排列的直方图:

CSS:
.his_box{ /*盒子*/
width: 400px;
height: 220px;
border: solid 1px #1E90FF;
display: flex;
flex-direction: column;
justify-content: space-between;
}
.his_box>div{
text-align: center;
}
.histogram{ /*直方图*/
display: flex;
flex-direction: column;
}
.histogram>div{ /*一条图块*/
height: 30px;
width: 200px; /*100%时的块宽度*/
line-height: 30px;
font-size: 14px;
text-align: right;
margin-bottom: 5px;
display: flex;
}
.histogram>div:nth-child(3n) div{ /*图块颜色*/
background-color: #ff404b;
}
.histogram>div:nth-child(3n+1) div{
background-color: #99CCFF;
}
.histogram>div:nth-child(3n+2) div{
background-color: #F0AD4E;
}
.histogram>div:nth-child(1) div{
flex: 0 0 50%; /*20为100%,50%就是10*/
}
.histogram>div:nth-child(2) div{
flex: 0 0 40%; /*8/20*/
}
.histogram>div:nth-child(3) div{
flex: 0 0 75%; /*15/20*/
}
.histogram>div:nth-child(4) div{
flex: 0 0 60%; /*12/20*/
}
.histogram>div:nth-child(5) div{
flex: 0 0 25%; /*5/20*/
}
以上就是CSS中用Flex布局做柱状图效果示例,上述示例具有一定的参考价值,有需要的朋友可以了解看看,希望对大家学习Flex布局有帮助,想要了解更多可以继续浏览群英网络其他相关的文章。
文本转载自脚本之家
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
什么是盒子呢?拿下举例,我们可以把每个红框都比作一个盒子,他们可以是任意的HTML元素。盒子模型所谓盒子模型:就是把HTML页面中的布局元素看作是一个矩形的盒子,也就...
css中下划线样式能设置长度吗能。在css中可以通过设置文字元素的下边框来设置文字元素的下划线,只需要通过width元素设置文字元素的宽度,也即设置文字元素的下划线。示例如下:<
CSS中定位的方式:1、在静态定位的情况下,每个元素都处于常规文档流中。2、相对定位相对于文档流中的原始位置。3、绝对定位下,元素从文档流中被“连根拔起”。4、固定定位元素的定位上下文是视口。
这篇文章主要介绍了CSS动画实现背景无缝无限循环的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
css实现背景虚化效果?一些朋友可能会遇到这方面的问题,对此在下文小编向大家来讲解一下,内容详细,易于理解,希望大家阅读完这篇能有收获哦,有需要的朋友就往下看吧!
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008