本身的地址
github.com/CodingMeUp/…css
.left {
float: left;
}
.right {
width: 100%
}複製代碼
.left {
float: left;
}
.right {
width: calc(100vw-200px);
}複製代碼
-【分析】.contain {
display: flex
}
.right {
flex: 1
}複製代碼
.contain{
background: pink;
float: left;
width: 100%;
}
.left{
height: 200px;
width: 200px;
float: left;
margin-left: -100%;
background: red;
}
.right {
background: blue;
height: 300px;
margin-left: 200px;
}複製代碼
<div class="contain">
<div class="right">
rrr
</div>
</div>
<div class="left">lll </div>複製代碼
【分析】html
<!DOCTYPE>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="test.css" type="text/css">
</head>
<style media="screen">
.one {
float: left;
height: 100px;
width: 300px;
background-color: blue;
}
.two {
overflow: auto;
height: 200px;
background-color: red;
}
</style>
<body>
<div class="one"></div>
<div class="two">第二種方法</div>
</body>
</html>複製代碼
position(絕對定位法) center的div須要放在最後面
絕對定位法原理是將左右兩邊使用absolute定位,由於絕對定位使其脫離文檔流,後面的center會天然流動到他們上面,而後使用margin屬性,留出左右元素的寬度,既可使中間元素自適應屏幕寬度。git
<div class='left'>left</div>
<div class='right'>right</div>
<div class='center'>center</div>
.left,.right{
position: absolute;
width: 200px;
height: 200px;
background-color: #df8793;
top:0;
}
.left{
left:0px;
}
.right{
right: 0px;
}
.center{
margin: 0 210px;
overflow: hidden;
background-color: yellow;
height: 200px;
}複製代碼
float:自身浮動法 center的div須要放在最後面
自身浮動法的原理就是使用對左右使用分別使用float:left和float:right,float使左右兩個元素脫離文檔流,中間元素正常在正常文檔流中,使用margin指定左右外邊距對其進行一個定位。github
<div class='left'>left</div>
<div class='right'>right</div>
<div class='center'>center</div>
.left,.right{
width: 200px;
height: 200px;
background-color: #df8793;
}
.left{
float: left;
}
.right{
float: right;
}
.center{
margin: 0 210px;
overflow: hidden;
background-color: yellow;
height: 200px;
}複製代碼
聖盃佈局
聖盃佈局的原理是margin負值法。使用聖盃佈局首先須要在center元素外部包含一個div,包含div須要設置float屬性使其造成一個BFC,並設置寬度,而且這個寬度要和left塊的margin負值進行配合瀏覽器
<div class='wrap'>
<div class='center'>center</div>
</div>
<div class='left'>left</div>
<div class='right'>right</div>
.wrap{
width: 100%; // .left margin-left 同步
float: left;
height: 200px;
background-color: #238978;
}
.center{
margin: 0 210px;
}
.left{
float: left;
margin-left: -100%; // .wrap width同步
width: 200px;
height: 200px;
background-color: #eee;
}
.right{
float: left;
margin-left: -200px;
width: 200px;
height: 200px;
background-color: #eee;
}複製代碼
<div class='wrap'>
<div class='left'>left</div>
<div class='center'>center</div>
<div class='right'>right</div>
</div>
.wrap{
display: flex;
}
.center{
flex:1;
margin: 0 10px;
background-color: pink;
}
.left{
width: 200px;
height: 200px;
background-color: #eee;
}
.right{
width: 200px;
height: 200px;
background-color: #eee;
}複製代碼
1) 固定高度bash
<div class="container">Hello World!</div>複製代碼
.container {
position: absolute;
left: 50%;
top: 50%;
margin-left: -150px;
margin-top: -150px;
width: 300px;
height: 300px;
border: 1px solid red;
}
// 支持calc
.container {
position: absolute;
left: calc(50% - 150px);
top: calc(50% - 150px);
width: 300px;
height: 300px;
border: 1px solid red;
}複製代碼
<div class="space"></div>
<div class="container">
<div class="inner">
hello world!
</div>
</div>複製代碼
.space {
float: left;
height: 50%;
margin-top: -150px;
}
.container {
clear: both;
height: 300px;
border: 1px solid red;
position: relative;
}複製代碼
2) 高度自適應函數
<div class="container">Hello World!</div>
.container {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%); // 自身寬度和高度的一半
border: 1px solid red;
}
優勢:無需定高度。高度隨內容自適應。
缺點:元素脫離文檔流。若是須要居中的元素已經在高度上超過了視口,那它的頂部會被視口裁切掉。複製代碼
擺脫maigin 百分比靠父元素寬度的問題 50%加上translate負值並不能實現垂直居中佈局。 改用
vh來作佈局
<div class="container">Hello World!</div>
.container {
width: 300px;
margin: 50vh auto 0;
transform: translateY(-50%);
border: 1px solid red;
}複製代碼
<div class="container">
<div class="inner">
<p>hello world!</p>
</div>
</div>複製代碼
.container {
display: flex;
height: 100vh;
}
.inner {
margin: auto;
}複製代碼
當咱們使父元素display: flex時,margin: auto不只能夠水平居中,也可以實現垂直居中。這是由於auto外邊距會平分水平或垂直方向上的額外空間。.container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}複製代碼
模擬表格 父display:table 子display: table-cell vertical-align: middlepost
<div class="container">
<div class="inner">
hello world!
</div>
</div>複製代碼
.container {
display: table; /* 讓div以表格的形式渲染 */
width: 100%;
border: 1px solid red;
}
.inner {
display: table-cell; /* 讓子元素以表格的單元格形式渲染 */
text-align: center;
vertical-align: middle;
}複製代碼