DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<style>
.box{
width: 100%;
height: 600px;
border: 1px solid black;
}
.box>div{
border: 1px solid black;
}
style>
head>
<body>
<div class="box">
<div>第一个子元素div>
<div>第二个子元素div>
<div>第三个子元素div>
div>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<style>
.box{
width: 100%;
height: 600px;
border: 1px solid black;
display: flex;
}
.box>div{
border: 1px solid black;
}
style>
head>
<body>
<div class="box">
<div>第一个子元素div>
<div>第二个子元素div>
<div>第三个子元素div>
div>
body>
html>
结论
就这一行就能让子元素并列排在一行,并且元素高度一样!!!
容器属性 | 说明 |
---|---|
flex-direction | 决定主轴的方向(即项目的排列方向) |
flex-wrap | 定义如果在一条轴线排不下,如何换行 |
flex-flow | 复合属性:是flex-direction和flex-wrap属性的简写形式 |
justify-content | 定义项目在主轴上的对齐方式 |
align-items | 定义项目在纵轴上的对齐方式 |
align-content | 定义多根轴线的对齐方式 |
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<style>
.box{
width: 100%;
height: 600px;
border: 1px solid black;
display: flex;
flex-direction:row;
}
.box>div{
border: 1px solid black;
}
style>
head>
<body>
<div class="box">
<div>第一个子元素div>
<div>第二个子元素div>
<div>第三个子元素div>
div>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<style>
.box{
width: 100%;
height: 600px;
border: 1px solid black;
display: flex;
flex-direction:row-reverse;
}
.box>div{
border: 1px solid black;
}
style>
head>
<body>
<div class="box">
<div>第一个子元素div>
<div>第二个子元素div>
<div>第三个子元素div>
div>
body>
html>
row-reverse:反转横向排列(右对齐),从后往前排,最后一项排在最前面
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<style>
.box{
width: 100%;
height: 600px;
border: 1px solid black;
display: flex;
flex-direction:column;
}
.box>div{
border: 1px solid black;
}
style>
head>
<body>
<div class="box">
<div>第一个子元素div>
<div>第二个子元素div>
<div>第三个子元素div>
div>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<style>
.box{
width: 100%;
height: 600px;
border: 1px solid black;
display: flex;
flex-direction:column-reverse;
}
.box>div{
border: 1px solid black;
}
style>
head>
<body>
<div class="box">
<div>第一个子元素div>
<div>第二个子元素div>
<div>第三个子元素div>
div>
body>
html>
使用这个之前要先把flex-direction:row调成默认
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<style>
.box{
width: 100%;
height: 600px;
border: 1px solid black;
display: flex;
flex-direction:row;
justify-content:center;
}
.box>div{
border: 1px solid black;
}
style>
head>
<body>
<div class="box">
<div>第一个子元素div>
<div>第二个子元素div>
<div>第三个子元素div>
div>
body>
html>
flex项目水平居中
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<style>
.box{
width: 100%;
height: 600px;
border: 1px solid black;
display: flex;
flex-direction:row;
justify-content:flex-end;
}
.box>div{
border: 1px solid black;
}
style>
head>
<body>
<div class="box">
<div>第一个子元素div>
<div>第二个子元素div>
<div>第三个子元素div>
div>
body>
html>
flex项目靠右对齐
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<style>
.box{
width: 100%;
height: 600px;
border: 1px solid black;
display: flex;
flex-direction:row;
justify-content:space-between;
}
.box>div{
border: 1px solid black;
}
style>
head>
<body>
<div class="box">
<div>第一个子元素div>
<div>第二个子元素div>
<div>第三个子元素div>
div>
body>
html>
flex项目两端对齐
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<style>
.box{
width: 100%;
height: 600px;
border: 1px solid black;
display: flex;
flex-direction:row;
justify-content: space-around;
}
.box>div{
border: 1px solid black;
}
style>
head>
<body>
<div class="box">
<div>第一个子元素div>
<div>第二个子元素div>
<div>第三个子元素div>
div>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<style>
.box{
width: 100%;
height: 600px;
border: 1px solid black;
display: flex;
flex-direction:row;
justify-content: space-evenly;
}
.box>div{
border: 1px solid black;
}
style>
head>
<body>
<div class="box">
<div>第一个子元素div>
<div>第二个子元素div>
<div>第三个子元素div>
div>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<style>
.box{
width: 100%;
height: 600px;
border: 1px solid black;
display: flex;
flex-direction:column;
align-items:center;
}
.box>div{
border: 1px solid black;
}
style>
head>
<body>
<div class="box">
<div>第一个子元素div>
<div>第二个子元素div>
<div>第三个子元素div>
div>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<style>
.box{
width: 100%;
height: 600px;
border: 1px solid black;
display: flex;
flex-direction:column;
align-items:flex-end;
}
.box>div{
border: 1px solid black;
}
style>
head>
<body>
<div class="box">
<div>第一个子元素div>
<div>第二个子元素div>
<div>第三个子元素div>
div>
body>
html>
使用这个之前要先把flex-direction:row调成默认
然后再div里加上高度
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<style>
.box{
width: 100%;
height: 600px;
border: 1px solid black;
display: flex;
flex-direction:row;
align-items:flex-end;
}
.box>div{
border: 1px solid black;
}
style>
head>
<body>
<div class="box">
<div style="height: 100px;">第一个子元素div>
<div style="height: 200px;">第二个子元素div>
<div style="height: 300px;">第三个子元素div>
div>
body>
html>
默认值,如果项目未设置高度过auto,子元素会被拉伸以适应容器
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<style>
.box{
width: 100%;
height: 600px;
border: 1px solid black;
display: flex;
flex-direction:row;
align-items:flex-start;
}
.box>div{
border: 1px solid black;
}
style>
head>
<body>
<div class="box">
<div style="height: 100px;">第一个子元素div>
<div style="height: 200px;">第二个子元素div>
<div style="height: 300px;">第三个子元素div>
div>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<style>
.box{
width: 100%;
height: 600px;
border: 1px solid black;
display: flex;
flex-direction:row;
align-items:center;
}
.box>div{
border: 1px solid black;
}
style>
head>
<body>
<div class="box">
<div style="height: 100px;">第一个子元素div>
<div style="height: 200px;">第二个子元素div>
<div style="height: 300px;">第三个子元素div>
div>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<style>
.box{
width: 100%;
height: 600px;
border: 1px solid black;
display: flex;
flex-direction:row;
align-items:baseline;
}
.box>div{
border: 1px solid black;
}
style>
head>
<body>
<div class="box">
<div style="height: 100px;">第一个子元素div>
<div style="height: 200px;">第二个子元素div>
<div style="height: 300px;">第三个子元素div>
div>
body>
html>
注意
在div加上宽度
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<style>
.box{
width: 100%;
height: 600px;
border: 1px solid black;
display: flex;
flex-direction:row;
align-items:stretch;
}
.box>div{
border: 1px solid black;
}
style>
head>
<body>
<div class="box">
<div style="height: 100px; width: 40%;">第一个子元素div>
<div style="height: 200px; width: 40%;">第二个子元素div>
<div style="height: 300px; width: 40%;">第三个子元素div>
div>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<style>
.box{
width: 100%;
height: 600px;
border: 1px solid black;
display: flex;
flex-direction:row;
align-items:stretch;
flex-wrap: nowrap;
}
.box>div{
border: 1px solid black;
}
style>
head>
<body>
<div class="box">
<div style="height: 100px; width: 40%;">第一个子元素div>
<div style="height: 200px; width: 40%;">第二个子元素div>
<div style="height: 300px; width: 40%;">第三个子元素div>
div>
body>
html>
默认值。意思是不换行、换列。
当flex项目的总宽度超过容器的宽度,flex项目将按照原有比例强行等分,且不换行。
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<style>
.box{
width: 100%;
height: 600px;
border: 1px solid black;
display: flex;
flex-direction:row;
align-items:stretch;
flex-wrap: wrap;
}
.box>div{
border: 1px solid black;
}
style>
head>
<body>
<div class="box">
<div style="height: 100px; width: 40%;">第一个子元素div>
<div style="height: 200px; width: 40%;">第二个子元素div>
<div style="height: 300px; width: 40%;">第三个子元素div>
div>
body>
html>