使用弹性盒实现多种常见布局
一、左侧定宽,右侧响应式
<!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: 0;
padding: 0;
}
body, html {
width: 100%;
height: 100%;
}
.container {
width: 100%;
height: 100%;
display: flex;
}
.slider {
width: 100px;
height: 100%;
background-color: red;
}
.content {
flex: 1;
background-color: blue;
}
</style>
</head>
<body>
<div class="container">
<div class="slider"></div>
<div class="content"></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: 0;
padding: 0;
}
body, html {
width: 100%;
height: 100%;
}
.container {
width: 100%;
height: 100%;
display: flex;
}
.left, .right {
width: 100px;
height: 100%;
background-color: red;
}
.content {
flex: 1;
background-color: blue;
}
</style>
</head>
<body>
<div class="container">
<div class="left"></div>
<div class="content"></div>
<div class="right"></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: 0;
padding: 0;
}
body, html {
width: 100%;
height: 100%;
}
.container {
width: 100%;
height: 100%;
display: flex;
}
.box {
flex: 1;
height: 20px;
background-color: red;
margin: 20px;
}
</style>
</head>
<body>
<div class="container">
<div class="box left"></div>
<div class="box content"></div>
<div class="box right"></div>
</div>
</body>
</html>
效果如下:
四、搜索历史不定宽
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Flexbox wrap 0 — children overflowing</title>
<style>
* {
margin: 0;
padding: 0;
}
body, html {
width: 100%;
height: 100%;
}
div {
padding: 10px;
margin: 10px;
background: aqua;
}
.container {
display: flex;
flex-wrap: wrap
}
</style>
</head>
<body>
<main class="container">
<div>First article</div>
<div>Second article</div>
<div>Second article</div>
<div>article</div>
<div>Second article</div>
<div>Eighth article</div>
<div>Second article</div>
<div>article</div>
<div>Second article</div>
<div>Twelfth article</div>
<div>Second article</div>
<div>Second article</div>
<div>Ninth article</div>
<div>Second article</div>
<div>article</div>
<div>Tenth article</div>
<div>Second article</div>
<div>Second</div>
<div>Second article</div>
</main>
</body>
</html>
五、多列布局等宽
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Flexbox wrap 0 — children overflowing</title>
<style>
* {
margin: 0;
padding: 0;
}
body, html {
width: 100%;
height: 100%;
}
.container {
display: flex;
flex-wrap: wrap;
}
div {
flex: 200px;
height: 70px;
margin: 10px;
background: aqua;
}
</style>
</head>
<body>
<main class="container">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</main>
</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: 0;
padding: 0;
}
body, html {
width: 100%;
height: 100%;
}
.container {
width: 200px;
height: 200px;
border: 2px solid gray;
border-radius: 10px;
padding: 20px;
display: flex;
justify-content: space-between;
margin: 20px;
}
.item {
display: block;
width: 40px;
height: 40px;
border-radius: 50%;
background-color: #666;
}
.item:nth-child(2) {
align-self: center;
}
.item:nth-child(3) {
align-self: flex-end;
}
</style>
</head>
<body>
<div class="container">
<span class="item"></span>
<span class="item"></span>
<span class="item"></span>
</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: 0;
padding: 0;
}
body, html {
width: 100%;
height: 100%;
}
.container {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
}
header, footer {
height: 50px;
background-color: red;
}
main {
flex: 1;
background-color: blue;
}
</style>
</head>
<body>
<div class="container">
<header></header>
<main></main>
<footer></footer>
</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: 0;
padding: 0;
}
body, html{
width: 100%;
height: 100%;
}
.container {
width: 800px;
height: 300px;
display: flex;
justify-content: space-between;
flex-wrap: wrap;
align-content: space-between;
background-color: darkgray;
}
.container_item {
width: 200px;
height: 40px;
background-color: red;
/* 根据父元素宽度计算得出 */
margin-right: 400px;
}
.container_item:nth-child(2n) {
margin-right: 0;
}
</style>
</head>
<body>
<div class="container">
<div class="container_item"></div>
<div class="container_item"></div>
<div class="container_item"></div>
<div class="container_item"></div>
<div class="container_item"></div>
<div class="container_item"></div>
</div>
</body>
</html>
如下: