说明
justify-content设置主轴上子元素的排列方式。
注意:使用这个属性之前,要先确定好主轴方向,主轴方向就是使用flex-direction确定的方向。
justify-content的属性值:
- flex-start:这是justify-content属性的默认值,项目在主轴上向弹性容器的起始侧对齐,彼此之间没有间隔。例如,主轴方向设置为row的话,那么flex-start表示从左到右排列。
- flex-end:项目在主轴上向弹性容器的结束侧对齐,彼此之间没有间隔。
- center:项目在主轴上向中心靠拢,彼此之间没有间隔。
- space-around:项目在主轴线上均匀分布。主轴上的项目间隔相同的空间,首尾的项目两侧空白间距是相邻项目间隔的一半。如果只有一个项目,它将居中。
- space-between:项目在主轴上均匀分布。每对相邻项目的间距相同。第一个项目与主轴起始边缘齐平,最后一个项目与主轴结束边缘齐平。
示例
示例:主轴方向设置为row时,演示justify-content: flex-start的效果
<!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>
div {
display: flex;
flex-direction: row;
justify-content: flex-start;
width: 800px;
height: 300px;
background-color: pink;
}
div span {
width: 150px;
height: 100px;
background-color: purple;
color: white;
}
</style>
</head>
<body>
<div>
<span>内容1</span>
<span>内容2</span>
<span>内容3</span>
<span>内容4</span>
</div>
</body>
</html>
展示效果:

示例:主轴方向设置为row时,演示justify-content: flex-end的效果
<!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>
div {
display: flex;
flex-direction: row;
justify-content: flex-end;
width: 800px;
height: 300px;
background-color: pink;
}
div span {
width: 150px;
height: 100px;
background-color: purple;
color: white;
}
</style>
</head>
<body>
<div>
<span>内容1</span>
<span>内容2</span>
<span>内容3</span>
<span>内容4</span>
</div>
</body>
</html>
展示效果:

示例:主轴方向设置为row时,演示justify-content: center的效果
<!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>
div {
display: flex;
flex-direction: row;
justify-content: center;
width: 800px;
height: 300px;
background-color: pink;
}
div span {
width: 150px;
height: 100px;
background-color: purple;
color: white;
}
</style>
</head>
<body>
<div>
<span>内容1</span>
<span>内容2</span>
<span>内容3</span>
<span>内容4</span>
</div>
</body>
</html>
展示效果:

示例:主轴方向设置为row时,演示justify-content: space-around的效果
<!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>
div {
display: flex;
flex-direction: row;
justify-content: space-around;
width: 800px;
height: 300px;
background-color: pink;
}
div span {
width: 150px;
height: 100px;
background-color: purple;
color: white;
}
</style>
</head>
<body>
<div>
<span>内容1</span>
<span>内容2</span>
<span>内容3</span>
<span>内容4</span>
</div>
</body>
</html>
展示效果:

示例:主轴方向设置为row时,演示justify-content: space-between的效果
<!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>
div {
display: flex;
flex-direction: row;
justify-content: space-between;
width: 800px;
height: 300px;
background-color: pink;
}
div span {
width: 150px;
height: 100px;
background-color: purple;
color: white;
}
</style>
</head>
<body>
<div>
<span>内容1</span>
<span>内容2</span>
<span>内容3</span>
<span>内容4</span>
</div>
</body>
</html>
展示效果:

示例:主轴方向设置为column时,演示justify-content: space-between的效果
<!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>
div {
display: flex;
flex-direction: column;
justify-content: space-between;
width: 800px;
height: 400px;
background-color: pink;
}
div span {
width: 150px;
height: 100px;
background-color: purple;
color: white;
}
</style>
</head>
<body>
<div>
<span>内容1</span>
<span>内容2</span>
<span>内容3</span>
</div>
</body>
</html>
展示效果:

示例:主轴方向设置为column时,演示justify-content: center的效果
<!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>
div {
display: flex;
flex-direction: column;
justify-content: center;
width: 800px;
height: 400px;
background-color: pink;
}
div span {
width: 150px;
height: 100px;
background-color: purple;
color: white;
}
</style>
</head>
<body>
<div>
<span>内容1</span>
<span>内容2</span>
<span>内容3</span>
</div>
</body>
</html>
展示效果:

5887




被折叠的 条评论
为什么被折叠?



