flex佈局中,一些屬性定義會失效,如float、clear等css
關於flex佈局的屬性:html
在flex佈局中,能夠想象爲在二維座標系中,主軸爲x軸,交叉軸爲y軸,而且交叉軸和主軸呈90度,flex-direction能夠設置主軸的方向瀏覽器
在flex佈局中有兩種角色,容器能夠理解爲父元素,項目能夠理解爲子元素bash
flex-direction:column佈局
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test</title>
<style type="text/css">
.container{
display: flex;
flex-direction: row;
}
.container>div{
background-color: yellow;
width: 40px;
height: 40px;
margin: 10px;
}
</style>
</head>
<body>
<div class="container">
<div></div>
<div></div>
<div></div>
<div></div>
</div>
</body>
</html>
複製代碼
flex-direction:row(默認)flex
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test</title>
<style type="text/css">
.container{
display: flex;
flex-direction: column;
}
.container>div{
background-color: yellow;
width: 40px;
height: 40px;
margin: 10px;
}
</style>
</head>
<body>
<div class="container">
<div></div>
<div></div>
<div></div>
<div></div>
</div>
</body>
</html>
複製代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test</title>
<style type="text/css">
.container{
display: flex;
flex-direction: row;
width: 250px;
/*默認爲flex-wrap:nowrap*/
flex-wrap: wrap;
}
.container>div{
background-color: yellow;
width: 40px;
height: 40px;
margin: 10px;
}
</style>
</head>
<body>
<div class="container">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
</body>
</html>
複製代碼
align-items:flex-endui
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test</title>
<style type="text/css">
.container{
display: flex;
flex-direction: row;
width: 600px;
height: 300px;
/*默認爲flex-wrap:nowrap*/
flex-wrap: wrap;
align-items: flex-end;
}
.container>div{
background-color: yellow;
width: 40px;
height: 40px;
margin: 10px;
}
</style>
</head>
<body>
<div class="container">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
</body>
</html>
複製代碼
align-items:centerurl
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test</title>
<style type="text/css">
.container{
display: flex;
flex-direction: row;
width: 600px;
height: 300px;
/*默認爲flex-wrap:nowrap*/
flex-wrap: wrap;
align-items: center;
}
.container>div{
background-color: yellow;
width: 40px;
height: 40px;
margin: 10px;
}
</style>
</head>
<body>
<div class="container">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
</body>
</html>
複製代碼
justify-content:flex-endspa
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test</title>
<style type="text/css">
.container{
display: flex;
flex-direction: row;
width: 600px;
height: 300px;
/*默認爲flex-wrap:nowrap*/
flex-wrap: wrap;
align-items: center;
outline: 1px solid black;
justify-content: flex-end;
}
.container>div{
background-color: yellow;
width: 40px;
height: 40px;
margin: 10px;
}
</style>
</head>
<body>
<div 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> <div></div>
</div>
</body>
</html>
複製代碼
justify-content: center3d
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test</title>
<style type="text/css">
.container{
display: flex;
flex-direction: row;
width: 600px;
height: 300px;
/*默認爲flex-wrap:nowrap*/
flex-wrap: wrap;
align-items: center;
outline: 1px solid black;
justify-content: center;
}
.container>div{
background-color: yellow;
width: 40px;
height: 40px;
margin: 10px;
}
</style>
</head>
<body>
<div 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> <div></div>
</div>
</body>
</html>
複製代碼
align-content: flex-end
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test</title>
<style type="text/css">
.container{
display: flex;
flex-direction: row;
width: 600px;
height: 300px;
/*默認爲flex-wrap:nowrap*/
flex-wrap: wrap;
align-items: center;
outline: 1px solid black;
justify-content: center;
align-content: flex-end;
}
.container>div{
background-color: yellow;
width: 40px;
height: 40px;
margin: 10px;
}
</style>
</head>
<body>
<div 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>
</body>
</html>
複製代碼
先初始化一個頁面以下:
咱們將偶數的div排到前面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test</title>
<style type="text/css">
.container{
display: flex;
flex-direction: row;
width: 600px;
height: 300px;
/*默認爲flex-wrap:nowrap*/
flex-wrap: wrap;
align-items: center;
outline: 1px solid black;
justify-content: center;
/*align-content: flex-end;*/
}
.container>div{
background-color: yellow;
width: 40px;
height: 40px;
margin: 10px;
}
.container>div:nth-child(2n+1){
order: 3;
}
</style>
</head>
<body>
<div class="container">
<div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> <div>6</div> <div>7</div> <div>8</div> <div>9</div> <div>10</div> <div>11</div> <div>12</div>
</div>
</body>
</html>
複製代碼
運行效果:
未佔滿一個主軸時:
設置flex-frow:1時:<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test</title>
<style type="text/css">
.container{
display: flex;
flex-direction: row;
width: 600px;
height: 300px;
/*默認爲flex-wrap:nowrap*/
flex-wrap: nowrap;
align-items: center;
outline: 1px solid black;
justify-content: center;
/*align-content: flex-end;*/
}
.container>div{
background-color: yellow;
flex-basis: 40px;
margin: 10px;
flex-grow: 1;
/*flex-shrink: 0;*/
}
</style>
</head>
<body>
<div class="container">
<div>6</div> <div>7</div> <div>8</div> <div>9</div> <div>10</div> <div>11</div> <div>12</div>
</div>
</body>
</html>
複製代碼
未佔滿一個主軸時:
沾滿並溢出主軸時:<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test</title>
<style type="text/css">
.container{
display: flex;
flex-direction: row;
width: 600px;
height: 300px;
/*默認爲flex-wrap:nowrap*/
flex-wrap: nowrap;
align-items: center;
outline: 1px solid black;
justify-content: center;
/*align-content: flex-end;*/
}
.container>div{
background-color: yellow;
flex-basis: 40px;
margin: 10px;
/*flex-shrink: 0;*/
}
</style>
</head>
<body>
<div class="container">
<div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> <div>6</div> <div>7</div> <div>8</div> <div>9</div> <div>10</div> <div>11</div> <div>12</div>
</div>
</body>
</html>
複製代碼
設置flex-shrink:0時:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test</title>
<style type="text/css">
.container{
display: flex;
flex-direction: row;
width: 600px;
height: 300px;
/*默認爲flex-wrap:nowrap*/
flex-wrap: nowrap;
align-items: center;
outline: 1px solid black;
justify-content: center;
/*align-content: flex-end;*/
}
.container>div{
background-color: yellow;
flex-basis: 40px;
margin: 10px;
flex-shrink: 0;
}
</style>
</head>
<body>
<div class="container">
<div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> <div>6</div> <div>7</div> <div>8</div> <div>9</div> <div>10</div> <div>11</div> <div>12</div>
</div>
</body>
</html>
複製代碼
雪碧圖:background-position 響應式界面下:background-size:定位雪碧圖的大小 給定一張圖片,咱們想象是不少圖片的組合
截取2號圖片做爲背景圖<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test</title>
<style type="text/css">
.container{
width: 50px;
height: 50px;
background-image: url("./test.png");
background-position: -60px 0px;
outline: 1px solid black;
}
</style>
</head>
<body>
<div class="container"></div>
</body>
</html>
複製代碼
運行效果:
關於雪碧圖的兼容: 一、兼容部分老的IE瀏覽器:能夠使用background-position-x & background-position-y進行兼容 二、兼容部分老版瀏覽器:使用css hock技術