这三种布局其实都是大同小异,今天就全部总结成一篇文章。
1.右定宽左自适应
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
padding: 0;
margin: 0;
}
.layout article > div{
min-height: 100px;
}
</style>
</head>
<body>
<!-- float -->
<section class="layout float">
<style>
.layout.float .left{
margin-right: 200px;
background-color:red;
}
.layout.float .right{
float: right;
width: 200px;
background-color: green;
}
</style>
<article>
<div class="right"></div>
<div class="left"></div>
</article>
</section>
<!-- position -->
<section class="layout position">
<style>
.layout.position{
margin-top: 10px;
}
.layout.position .left{
margin-right: 200px;
background-color:red;
}
.layout.position .right{
position: absolute;
right: 0;
background-color: green;
width: 200px;
}
</style>
<article>
<div class="right"></div>
<div class="left"></div>
</article>
</section>
<!-- flex -->
<section class="layout flex">
<style>
.layout.flex{
margin-top: 10px;
}
.layout.flex article{
display: flex;
}
.layout.flex .left{
flex: 1;
background-color:red;
}
.layout.flex .right{
background-color: green;
width: 200px;
}
</style>
<article>
<div class="left"></div>
<div class="right"></div>
</article>
</section>
<!-- grid -->
<section class="layout grid">
<style>
.layout.grid{
margin-top: 10px;
}
.layout.grid article{
display: grid;
grid-template-columns: auto 200px;
}
.layout.grid .left{
background-color:red;
}
.layout.grid .right{
background-color: green;
}
</style>
<article>
<div class="left"></div>
<div class="right"></div>
</article>
</section>
<!-- table -->
<section class="layout table">
<style>
.layout.table{
margin-top: 10px;
}
.layout.table article{
display: table;
height: 100px;
width: 100%;
}
.layout.table .left{
display: table-cell;
background-color:red;
}
.layout.table .right{
display: table-cell;
background-color: green;
width: 200px;
}
</style>
<article>
<div class="left"></div>
<div class="right"></div>
</article>
</section>
</body>
</html>
2.上定高下自适应
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
padding: 0;
margin: 0;
}
html, body{
height: 100%;
}
.layout{
height: 100%;
}
.layout article > div{
min-width: 100px;
}
</style>
</head>
<body>
<!-- flex -->
<section class="layout flex">
<style>
.layout.flex article{
height: 100%;
width: 100px;
display: flex;
flex-direction: column;
}
.layout.flex .top{
height: 100px;
background-color: red;
}
.layout.flex .bottom{
flex: 1;
background-color: green;
}
</style>
<article>
<div class="top"></div>
<div class="bottom"></div>
</article>
</section>
<!-- absolute -->
<section class="layout absolute">
<style>
.layout.absolute{
margin-left: 200px;
}
.layout.absolute article{
height: 100%;
width: 100px;
}
.layout.absolute .top{
height: 100px;
background-color: red;
position: absolute;
top: 0;
}
.layout.absolute .bottom{
background-color: green;
position: absolute;
bottom: 0;
top: 100px;
}
</style>
<article>
<div class="top"></div>
<div class="bottom"></div>
</article>
</section>
<!-- grid -->
<section class="layout grid">
<style>
.layout.grid{
margin-left: 200px;
}
.layout.grid article{
height: 100%;
width: 100px;
display: grid;
grid-template-rows: 100px auto;
}
.layout.grid .top{
height: 100px;
background-color: red;
}
.layout.grid .bottom{
background-color: green;
}
</style>
<article>
<div class="top"></div>
<div class="bottom"></div>
</article>
</section>
<!-- table -->
<section class="layout table">
<style>
.layout.table{
margin-left: 200px;
}
.layout.table article{
height: 100%;
width: 100px;
display: table;
}
.layout.table .top{
height: 100px;
background-color: red;
display: table-row;
}
.layout.table .bottom{
display: table-row;
background-color: green;
}
</style>
<article>
<div class="top">1</div>
<div class="bottom">2</div>
</article>
</section>
</body>
</html>
3.下定高上自适应
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
padding: 0;
margin: 0;
}
html, body{
height: 100%;
}
.layout{
height: 100%;
}
.layout article > div{
min-width: 100px;
}
</style>
</head>
<body>
<!-- flex -->
<section class="layout flex">
<style>
.layout.flex article{
height: 100%;
width: 100px;
display: flex;
flex-direction: column;
}
.layout.flex .top{
flex: 1;
background-color: red;
}
.layout.flex .bottom{
height: 100px;
background-color: green;
}
</style>
<article>
<div class="top"></div>
<div class="bottom"></div>
</article>
</section>
<!-- absolute -->
<section class="layout absolute">
<style>
.layout.absolute{
margin-left: 200px;
}
.layout.absolute article{
height: 100%;
width: 100px;
}
.layout.absolute .top{
background-color: red;
position: absolute;
top: 0;
bottom: 100px;
}
.layout.absolute .bottom{
background-color: green;
height: 100px;
position: absolute;
bottom: 0;
}
</style>
<article>
<div class="top"></div>
<div class="bottom"></div>
</article>
</section>
<!-- grid -->
<section class="layout grid">
<style>
.layout.grid{
margin-left: 200px;
}
.layout.grid article{
height: 100%;
width: 100px;
display: grid;
grid-template-rows: auto 100px;
}
.layout.grid .top{
background-color: red;
}
.layout.grid .bottom{
height: 100px;
background-color: green;
}
</style>
<article>
<div class="top"></div>
<div class="bottom"></div>
</article>
</section>
<!-- table -->
<section class="layout table">
<style>
.layout.table{
margin-left: 200px;
}
.layout.table article{
height: 100%;
width: 100px;
display: table;
}
.layout.table .top{
background-color: red;
display: table-row;
}
.layout.table .bottom{
height: 100px;
display: table-row;
background-color: green;
}
</style>
<article>
<div class="top">1</div>
<div class="bottom">2</div>
</article>
</section>
</body>
</html>