四,justify-content -main-轴-水平布局
使用这个之前要先把flex-direction:row调成默认
4.1,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> .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项目水平居中
4.2,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> .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项目靠右对齐
4.3,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> .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项目两端对齐
4.4,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> .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>
flex项目两端对齐,项目间的距离是两端和容器距离的2倍。
4.5,justify-content: space-evenly;
<!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> .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>
flex项目两端对齐,项目间距和容器两端的距离相等。