html5和css3实现下面效果,总宽度是页面的90%,1,2,3宽度相等,注意移动端不同分辨率宽度自适应。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>宽度自适应</title> <style type="text/css"> .flex-container{ display: flex; margin: 0 auto; width: 90%; height: 50px; border: 0.5px solid #ccc; } .flex-item{ width: 33.33%; text-align: center; border: 0.5px solid #ccc; } </style> </head> <body> <div class="flex-container"> <div class="flex-item">1</div> <div class="flex-item">2</div> <div class="flex-item">3</div> </div> </body> </html>
<!DOCTYPE html> <html> <head> <style> .container{ width: 90vw; height: 50px; display: flex; } .child{ display: flex; border: 1px solid #CCC; flex: 1; justify-content: center; align-item: flex-start; } </style> </head> <body> <div class="container"> <div class="child"> <span>1</span> </div> <div class="child"> <span>2</span> </div> <div class="child"> <span>3</span> </div> </div> </body> <html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> #main{ display:grid; width: 90%; height: 50px; grid-template-columns: 1fr 1fr 1fr; } #main > *{ text-align: center; border: 1px solid #ccc; } </style> </head> <body> <div id="main"> <div id="box1">1</div> <div id="box2">2</div> <div id="box3">3</div> </div> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> #content{ width:90vw; display:flex; margin: 0 auto; } .box{ height:50px; border:1px solid #ccc; flex:1; text-align: center; line-height: 50px; } </style> </head> <body> <div id='content'> <div class='box'>1</div> <div class='box'>2</div> <div class='box'>3</div> </div> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> *{ margin: 0px; padding: 0px; box-sizing: border-box; } .container{ width: 90%; text-align: center; margin: auto; overflow: hidden; } .box{ border: 1px solid #ccc; float: left; height: 50px; } @media screen and (max-width:767px){ .box{ width: 100%; } /* 去重复边框 */ .box:nth-child(2){ border-top: 0px; } .box:nth-child(3){ border-top: 0px; } } @media screen and (min-width:768px){ .box{ width: 33.3%; } .box:nth-child(2){ border-left: 0px; } .box:nth-child(3){ border-left: 0px; } } </style> </head> <body> <div class="container"> <div class="box">1</div> <div class="box">2</div> <div class="box">3</div> </div> </body> </html>
<html> <head> div{ weight : 90%; margin : o auto; } .sec{ float : left; weight : 33.3%; height : 50px; border : 1px solid #ccc; } </head> <body> <div> <section id="sec1" class="sec"></section> <section id="sec2" class="sec"></section> <section id="sec3" class="sec"></section> </div> </body> </html>
不知道是否是这么个意思,主要思想是:三个div宽度设成百分比,估摸取个33.33%,左右两个分别设置做浮动和右浮动,中间div通过设置margin值来完成布局。凑合能实现效果吧,但是,自适应要怎么做
*{margin:0;padding:0}
.parent{width:90%;margin:0 auto} .parent div{height:50px;box-sizing: border-box} .left{border:1px solid #ccc;width:33.33%;float:left} .center{border:1px solid #ccc;margin:0 33.335%;width:33.33%;border-left:none;border-right:none} .right{border:1px solid #ccc;width:33.33%;float:right} <div class="parent"> <div class="left"></div> <div class="right"></div> <div class="center"></div> </div>