Flexbox 对齐内容基础教程文档
收录于 2023-04-20 00:10:05 · بالعربية · English · Español · हिंदीName · 日本語 · Русский язык · 中文繁體
通常您可以观察到如下所示排列弹性项目后容器中剩余的额外空间。
使用属性
justify-content,您可以通过按预期分配额外空间来沿主轴对齐内容。您还可以调整 flexitems 的对齐方式,以防它们溢出行。
使用-
justify-content: flex-start | flex-end | center | space-between | space-around| space-evenly;
现在,我们将通过示例了解如何使用 justify-content 属性。
flex-start
将此值传递给属性
justify-content 时,弹性项目将放置在容器的开头。
以下示例演示了将值
flex-start 传递给
justify-content 属性的结果。
<!doctype html> <html lang = "en"> <style> .box1{background:green;} .box2{background:blue;} .box3{background:red;} .box4{background:magenta;} .box5{background:yellow;} .box6{background:pink;} .box{ font-size:35px; padding:15px; } .container{ display:flex; border:3px solid black; justify-content:flex-start; } </style> <body> <div class = "container"> <div class = "box box1">One</div> <div class = "box box2">two</div> <div class = "box box3">three</div> <div class = "box box4">four</div> <div class = "box box5">five</div> <div class = "box box6">six</div> </div> </body> </html>
flex-end
将此值传递给属性
justify-content 时,弹性项目被放置在容器的末尾。
以下示例演示了将值
flex-end 传递给
justify-content 属性的结果。
<!doctype html> <html lang = "en"> <style> .box1{background:green;} .box2{background:blue;} .box3{background:red;} .box4{background:magenta;} .box5{background:yellow;} .box6{background:pink;} .box{ font-size:35px; padding:15px; } .container{ display:flex; border:3px solid black; justify-content:flex-end; } </style> <body> <div class = "container"> <div class = "box box1">One</div> <div class = "box box2">two</div> <div class = "box box3">three</div> <div class = "box box4">four</div> <div class = "box box5">five</div> <div class = "box box6">six</div> </div> </body> </html>
中心
将此值传递给属性
justify-content 时,弹性项目被放置在容器的中心,其中额外的空间在开始和结束时平均分配弹性项目。
以下示例演示了将值
center 传递给
justify-content 属性的结果。
<!doctype html> <html lang = "en"> <style> .box1{background:green;} .box2{background:blue;} .box3{background:red;} .box4{background:magenta;} .box5{background:yellow;} .box6{background:pink;} .box{ font-size:35px; padding:15px; } .container{ display:flex; border:3px solid black; justify-content:center; } </style> <body> <div class = "container"> <div class = "box box1">One</div> <div class = "box box2">two</div> <div class = "box box3">three</div> <div class = "box box4">four</div> <div class = "box box5">five</div> <div class = "box box6">six</div> </div> </body> </html>
间距
将此值传递给属性
justify-content 时,额外的空间在弹性项目之间平均分配,这样任何两个弹性项目之间的空间都是相同的,并且开始和结束弹性项目接触容器的边缘。
以下示例演示了将值
space-between 传递给
justify-content 属性的结果。
<!doctype html> <html lang = "en"> <style> .box1{background:green;} .box2{background:blue;} .box3{background:red;} .box4{background:magenta;} .box5{background:yellow;} .box6{background:pink;} .box{ font-size:35px; padding:15px; } .container{ display:flex; border:3px solid black; justify-content:space-between; } </style> <body> <div class = "container"> <div class = "box box1">One</div> <div class = "box box2">two</div> <div class = "box box3">three</div> <div class = "box box4">four</div> <div class = "box box5">five</div> <div class = "box box6">six</div> </div> </body> </html>
空间
将此值传递给属性
justify-content 时,额外的空间在弹性项目之间平均分配,这样任何两个弹性项目之间的空间都是相同的。然而,容器边缘与其内容(弹性项目的开始和结束)之间的空间是弹性项目之间空间的一半。
以下示例演示了将值
space-around 传递给
justify-content 属性的结果。
<!doctype html> <html lang = "en"> <style> .box1{background:green;} .box2{background:blue;} .box3{background:red;} .box4{background:magenta;} .box5{background:yellow;} .box6{background:pink;} .box{ font-size:35px; padding:15px; } .container{ display:flex; border:3px solid black; justify-content:space-around; } </style> <body> <div class = "container"> <div class = "box box1">One</div> <div class = "box box2">two</div> <div class = "box box3">three</div> <div class = "box box4">four</div> <div class = "box box5">five</div> <div class = "box box6">six</div> </div> </body> </html>
空间均匀
将此值传递给属性
justify-content 时,额外的空间在弹性项目之间平均分配,这样任何两个弹性项目之间的空间都是相同的(包括到边缘)。
以下示例演示了将值
space-evenly 传递给
justify-content 属性的结果。
<!doctype html> <html lang = "en"> <style> .box1{background:green;} .box2{background:blue;} .box3{background:red;} .box4{background:magenta;} .box5{background:yellow;} .box6{background:pink;} .box{ font-size:35px; padding:15px; } .container{ display:flex; border:3px solid black; justify-content:space-evenly; } </style> <body> <div class = "container"> <div class = "box box1">One</div> <div class = "box box2">two</div> <div class = "box box3">three</div> <div class = "box box4">four</div> <div class = "box box5">five</div> <div class = "box box6">six</div> </div> </body> </html>