float浏览器
body,
.container{
margin: 0;
padding: 0;
}
.left{
float: left;
width: 100px;
height: 100px;
background: red;
}
.right{
margin-left: 100px;
height: 100px;
background: blue;
}
<div class="container">
<div class="left"></div>
<div class="right"></div>
</div>
复制代码
position: absolute布局
body,
.container{
margin: 0;
padding: 0;
}
.left{
position: absolute;
left: 0;
top: 0;
width: 100px;
height: 100px;
background: red;
}
.right{
margin-left: 100px;
height: 100px;
background: blue;
}
<div class="container">
<div class="left"></div>
<div class="right"></div>
</div>
复制代码
BFCflex
.container{
margin: 0;
padding: 0;
}
.left{
float: left;
width: 100px;
height: 100px;
background: red;
}
.right{
overflow: hidden;
height: 100px;
background: blue;
}
<div class="container">
<div class="left"></div>
<div class="right"></div>
</div>
复制代码
flexspa
.container{
margin: 0;
padding: 0;
display: flex;
}
.left{
width: 100px;
height: 100px;
background: red;
}
.right{
flex-grow: 1;
height: 100px;
background: blue;
}
<div class="container">
<div class="left"></div>
<div class="right"></div>
</div>
复制代码
float-overflow:hiddencode
.container{
zoom: 1;
}
.left{
float: left;
background: red;
}
.right{
overflow: hidden;
height: 100px;
background: blue;
zoom: 1;
}
<div class="container">
<div class="left">
<p>11111111111111111111</p>
</div>
<div class="right"></div>
</div>
复制代码
flex 布局it
.container{
display: flex;
}
.left{
background: red;
}
.right{
flex-grow: 1;
background: blue;
}
<div class="container">
<div class="left">
<p>11111111111111111111</p>
</div>
<div class="right"></div>
</div>
复制代码
grid布局io
.container{
display: grid;
grid-template-columns:auto 1fr;
}
.left{
background: red;
}
.right{
background: blue;
}
<div class="container">
<div class="left">
<p>11111111111111111111</p>
</div>
<div class="right"></div>
</div>
复制代码
流体布局table
.left{
float: left;
width: 100px;
height: 100px;
background: red;
}
.right{
float: right;
width: 100px;
height: 100px;
background: blue;
}
.center{
margin-left: 100px;
margin-right: 100px;
height: 100px;
background: orange;
}
<div class="container">
<div class="left"></div>
<div class="right"></div>
<div class="center"></div>
</div>
复制代码
BFC 三栏布局class
.left{
float: left;
width: 100px;
height: 100px;
background: red;
}
.right{
float: right;
width: 100px;
height: 100px;
background: blue;
}
.center{
overflow: hidden;
height: 100px;
background: orange;
}
<div class="container">
<div class="left"></div>
<div class="right"></div>
<div class="center"></div>
</div>
复制代码
双飞翼布局容器
.container{
float: left;
width: 100%;
}
.center{
margin-left: 100px;
margin-right: 100px;
height: 100px;
background: orange;
}
.left{
float: left;
margin-left: -100%;
width: 100px;
height: 100px;
background: red;
}
.right{
float: left;
margin-left: -100px;
width: 100px;
height: 100px;
background: blue;
}
<div class="container">
<div class="center"></div>
</div>
<div class="left"></div>
<div class="right"></div>
复制代码
圣杯布局
和与双飞翼布局的区别:与双飞翼布局很像,有一些细节上的区别,相对于双飞翼布局来讲,HTML 结构相对简单,可是样式定义就稍微复杂,
优势:也是优先加载内容主体。
代码实现
.container{
margin-left: 100px;
margin-right: 100px;
}
.center{
float: left;
width: 100%;
height: 100px;
background: orange;
}
.left{
float: left;
margin-left: -100%;
position: relative;
left: -100px;
width: 100px;
height: 100px;
background: red;
}
.right{
float: left;
margin-left: -100px;
position: relative;
right: -100px;
width: 100px;
height: 100px;
background: blue;
}
<div class="container">
<div class="center"></div>
<div class="left"></div>
<div class="right"></div>
</div>
复制代码
Flex布局
.container{
display: flex;
}
.center{
flex-grow: 1;
height: 100px;
background: orange;
}
.left{
order: -1; /* order属性定义项目的排列顺序 数值越小 排列越靠前 默认为0 */
flex: 0 1 100px; /* flex-grow flex-shrink flex-basis */
height: 100px;
background: red;
}
.right{
flex: 0 1 100px;
height: 100px;
background: blue;
}
/*
flex-grow属性定义项目的放大比例 默认为0 即若是存在剩余空间 也不放大
flex-shrink属性定义了项目的缩小比例 默认为1 即若是空间不足 该项目将缩小
flex-basis属性定义了在分配多余空间以前 项目占据的主轴空间(main size)
浏览器根据这个属性 计算主轴是否有多余空间 它的默认值为auto 即项目的原本大小
*/
<div class="container">
<div class="center"></div>
<div class="left"></div>
<div class="right"></div>
</div>
复制代码
Table布局
.container{
display: table; /* 此元素会做为块级表格来显示(相似 <table>)表格先后带有换行符 */
width: 100%;
}
.left,
.center,
.right{
display: table-cell; /* 此元素会做为一个表格单元格显示(相似 <td> 和 <th>) */
}
.left{
width: 100px;
height: 100px;
background: red;
}
.center{
background: orange;
}
.right{
width: 100px;
height: 100px;
background: blue;
}
<div class="container">
<div class="left"></div>
<div class="center"></div>
<div class="right"></div>
</div>
复制代码
绝对定位布局
.container{
position: relative;
}
.center{
margin-left: 100px;
margin-right: 100px;
height: 100px;
background: orange;
}
.left{
position: absolute;
left: 0;
right: 0;
width: 100px;
height: 100px;
background: red;
}
.right{
position: absolute;
right: 0;
top: 0;
width: 100px;
height: 100px;
background: blue;
}
<div class="container">
<div class="left"></div>
<div class="center"></div>
<div class="right"></div>
</div>
复制代码
网格布局(Grid布局)
.container{
display: grid;
grid-template-columns: 100px auto 100px;
/*
用于设置网格容器的列属性 其实就至关于列的宽度 当咱们须要几列展现时
就设置几个值 这个属性能够接收具体数值好比100px 也能够接收百分比值
表示占据容器的宽度
须要注意的是: 当给容器设定了宽度时
grid-template-columns设定的百分比值是以容器的宽度值为基础计算的
若是未设置宽度时 会一直向上追溯到设置了宽度的父容器 直到body元素。
*/
grid-template-rows: 100px;
/*
用于设置网格容器的行属性 其实就至关于行的高度
其特性与grid-template-columns属性相似
*/
}
.left{
background: red;
}
.center{
background :orange;
}
.right{
background: blue;
}
<div class="container">
<div class="left"></div>
<div class="center"></div>
<div class="right"></div>
</div>
复制代码