css3flex布局实现平均分配元素例⼦⼀:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>flex 布局</title>
<style>
*{
padding:0;
margin:0;
}
body,html{
height:100vh;
min-width:800px;
}
.container{
display:flex;
flex-wrap:wrap;
display:-webkit-flex;/* Safari */
}
.container>.item{
border:1px solid black;
flex:1;
height:100px;
background: #abcdef;
}
</style>
</head>
<body>
<div class="container">
<div class="item"></div>
<main class="item"></main>
<div class="item"></div>
</div>
</body>
</html>
例⼦⼆:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>flex 布局</title>
<style>
*{
padding:0;
margin:0;
box-sizing: border-box;
}
body,html{
width:100%;
height:100vh;
min-width:800px;
display:flex;
display:-webkit-flex;/* Safari */ justify-content: center;
align-items: center;
}
.container{
width:300px;
height:300px;
display:flex;
display:-webkit-flex;/* Safari */ flex-wrap: wrap;
}
.container>.item{
flex:0033.3%;
height:100px;
background: #abcdef;
border:1px solid red;
}
main{
flex:0033.3%;
height:100px;
background-color: #ccc;
border:1px solid red;
}
</style>
</head>
<body>
flex布局详细讲解
<div class="container">
<div class="item">left</div>
<main>main</main>
<div class="item">right</div>
<div class="item">left</div>
<main>main</main>
<div class="item">right</div>
<div class="item">left</div>
<main>main</main>
<div class="item">right</div>
</div>
</body>
</html>
flex:0 0 33.3% 相当于flex-basis:33.3%,使每⼀个元素的宽度占外层容器的33.3%,因此每⾏最多能够排开三个元素。flex-wrap:wrap 表⽰每⾏填满时会⾃动换⾏。
例⼦三:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>flex 布局</title>
<style>
*{
padding:0;
margin:0;
box-sizing: border-box;
}
body,html{
width:100%;
height:100vh;
min-width:800px;
display:flex;
display:-webkit-flex;/* Safari */ justify-content: center;
align-items: center;
}
.container{
width:300px;
height:300px;
margin:50px;
display:flex;
display:-webkit-flex;/* Safari */ flex-wrap: wrap;
justify-content: space-between; }
.container>.item{
flex:0030%;
height:90px;
background: #abcdef;
border:1px solid red;
}
main{
flex:0030%;
height:90px;
background-color: #ccc;
border:1px solid red;
}
</style>
</head>
<body>
<div class="container">
<div class="item">left</div>
<main>main</main>
<div class="item">right</div>
<div class="item">left</div>
<main>main</main>
<div class="item">right</div>
<div class="item">left</div>
<main>main</main>
<div class="item">right</div>
</div>
</body>
</html>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论