请使用两种不同的CSS方法(要求dom结构不同)实现下图所示的条形图。从左到右的条形分别记为A,B,C,D,E。A的高度为30%,颜色为#f00;B的高度为80%,颜色为#ddd;C的高度为70%,颜色为#0fd;D的高度为60%,颜色为#ff0;E的高度为90%,颜色为#234,每个条形之间的距离可以任意设置(可以考虑使用CSS3新属性来实现)。
<!DOCTYPE html><html leng='en><head><meta charset='utf8'><title>条形图</title><style>.wrap{width: 232px; height: 400px; border: 1px solid #CCC; margin: 100px auto;}.wrap span{display: inline-block; width: 30px; margin: 40px 0 0 10px;}.wrap span:nth-of-type(1){height: 30%; background-color: #f00;}.wrap span:nth-of-type(2){height: 80%; background-color:#DDD}.wrap span:nth-of-type(3){height: 70%; background-color: #0fd;}.wrap span:nth-of-type(4){height: 60%; background-color: #ff0;}.wrap span:nth-of-type(5){height: 90%; background-color: #234;}</style></head><div class='wrap'><span></span><span></span><span></span><span></span><span></span></div><body></body></html>
* { margin: 0; padding: 0; } #context { width: 500px; height: 300px; margin: 0 auto; display: flex; justify-content: space-around; align-items: flex-end; } .flex-box { width: 15%; } .flex-box:nth-child(1) { height: 30%; background-color: #f00; } .flex-box:nth-child(2) { height: 80%; background-color: #ddd; } .flex-box:nth-child(3) { height: 70%; background-color: #0fd; } .flex-box:nth-child(4) { height: 60%; background-color: #ff0; } .flex-box:nth-child(5) { height: 90%; background-color: #234; }
<div id="context"> <div class="flex-box"></div> <div class="flex-box"></div> <div class="flex-box"></div> <div class="flex-box"></div> <div class="flex-box"></div> </div>方案二:
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title></title> <style type="text/css"> * { padding: 0; margin: 0 auto; } ul,li { list-style: none; } /* 方法一 */ .container { width: 480px; padding-left: 10px; border-bottom: 5px solid gray; } .boxs1 { width: 480px; height: 300px; display: flex; justify-content: space-around; align-items: flex-end; border-left: 1px solid #000; } .box { width: 10%; } .box:nth-child(1){ height: 30%; background-color: #f00; } .box:nth-child(2) { height: 80%; background-color: #ddd; } .box:nth-child(3) { height: 70%; background-color: #0fd; } .box:nth-child(4) { height: 60%; background-color: #ff0; } .box:nth-child(5) { height: 90%; background-color: #234; } .box2 { width: 480px; height: 300px; border-bottom: 5px solid gray; position: relative; padding: 10px; } .box2-ul { padding-left: 20px; position: absolute; bottom: 0; border-left: 1px solid #000; } .box2-ul>li { margin-right: 35px; display: inline-block; width: 50px; vertical-align: bottom; } .i1 { height: 90px; background-color: #f00; } .i2 { height: 240px; background-color: #ddd; } .i3 { height: 210px; background-color: #0fd; } .i4 { height: 180px; background-color: #ff0; } .i5 { height: 270px; background-color: #234; } </style> </head> <body> <div class="container"> <div class="boxs1"> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> </div> </div> <div class="box2"> <ul class="box2-ul"> <li class="i1"></li> <li class="i2"></li> <li class="i3"></li> <li class="i4"></li> <li class="i5"></li> </ul> </div> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> html, body { width: 100%; padding: 0; margin: 0; } .wrapper { width: 500px; height: 200px; display: flex; align-items: flex-end; margin: 30px auto; border-left: 1px solid black; } .red { background-color: #f00; width: 20px; height: 30%; } .grey { background-color: #ddd; width: 20px; height: 80%; } .qing { background-color: #0fd; width: 20px; height: 70%; } .yellow { background-color: #ff0; width: 20px; height: 60%; } .darkblue { background-color: #234; width: 20px; height: 90%; } </style> </head> <body> <div class="wrapper"> <div class="red"></div> <div class="grey"></div> <div class="qing"></div> <div class="yellow"></div> <div class="darkblue"></div> </div> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>柱形图</title> </head> <style> #display{ width: 250px; height: 280px; border-bottom: 1px #EBEBEB solid; padding: 0 20px; } #display ul{ margin: 0; padding: 0; list-style-type: none; height: 100%; } #border{ width: 2px; height: 100%; background-color: #2E2E2E; } li{ float: left; display: block; margin: 0 5px; width: 30px; } </style> <body> <div id="display"> <ul> <li id="border"></li> <li style="background-color: #f00;height:30%"></li> <li style="background-color: #ddd;height:80%"></li> <li style="background-color: #0fd;height:70%"></li> <li style="background-color: #ff0;height:60%"></li> <li style="background-color: #234;height:90%"></li> </ul> </div> </body> <script> var lis = document.getElementsByTagName("li"); for(let i = 0; i < lis.length; i++){ var height = lis[i].style.height; lis[i].style.marginTop = [100 - parseInt(height)] * 2.8 + "px"; } </script> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style > .outer { display:grid; grid-template-columns:repeat(5, 30px); justify-content: space-evenly; align-items: end; height: 400px; width: 500px; border: 1px solid black; } .inner1{ height: 30%; background: #f00; } .inner2{ height: 80%; background: #ddd; } .inner3{ height: 70%; background: #0fd; } .inner4{ height: 60%; background: #ff0; } .inner5{ height: 90%; background: #234; } </style> </head> <body> <div class="outer"> <div class="inner inner1"></div> <div class="inner inner2"></div> <div class="inner inner3"></div> <div class="inner inner4"></div> <div class="inner inner5"></div> </div> </body> </html>grid布局
<!DOCTYPE html> <html> <head> <title>aaaa</title> <style type="text/css"> .bigBox{ width: 300px; height: 300px; border-left: 2px solid black; border-bottom: 1px solid black; } .box{ float: left; } .box1{ margin-left: 10px; width: 30px; height: 30%; background-color: #f00; margin-top: 70%; } .box2{ margin-left: 10px; width: 30px; height: 80%; background-color: #ddd; margin-top: 20%; } .box3{ margin-left: 10px; width: 30px; height: 70%; background-color: #0fd; margin-top: 30%; } .box4{ margin-left: 10px; width: 30px; height: 60%; background-color: #ff0; margin-top: 40%; } .box5{ margin-left: 10px; width: 30px; height: 90%; background-color: #234; margin-top: 10%; } </style> </head> <body> <div class="bigBox"> <div class="box box1"></div> <div class="box box2"></div> <div class="box box3"></div> <div class="box box4"></div> <div class="box box5"></div> </div> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <!--方法1:最普通的方法--> <style> .box{ list-style: none; margin: 0; padding: 0; width:300px; height:300px; border-left:1px solid black; border-bottom: 1px solid gray; } .box > li{ float:left; width:50px; margin-left:10px; } .box_content1{ height:30%; margin-top:70%; background-color:#f00; } .box_content2{ height:80%; margin-top:20%; background-color:#ddd; } .box_content3{ height:70%; margin-top:30%; background-color:#0fd; } .box_content4{ height:60%; margin-top:40%; background-color:#ff0; } .box_content5{ height:90%; margin-top:10%; background-color:#234; } </style><!--方法2:弹性盒模型--> <style> .box{ list-style: none; margin: 0; padding: 0; width:300px; height:300px; border-left:1px solid black; border-bottom: 1px solid gray; /*弹性盒模型*/ display: flex; display:-webkit-flex; /*剩余空白空间均分*/ justify-content: space-around; /*子元素对齐底部*/ align-items:flex-end; } .box > li{ float:left; width:50px; } .box_content1{ height:30%; background-color:#f00; } .box_content2{ height:80%; background-color:#ddd; } .box_content3{ height:70%; background-color:#0fd; } .box_content4{ height:60%; background-color:#ff0; } .box_content5{ height:90%; background-color:#234; } </style>
<ul class="box"> <li class="box_content1"></li> <li class="box_content2"></li> <li class="box_content3"></li> <li class="box_content4"></li> <li class="box_content5"></li> </ul> </body> </html>