我们使用 flex-basis 属性在分配空间之前定义flex项的默认大小.
以下示例演示了flex-basis属性的用法.在这里,我们创建3个彩色框并将其大小固定为150像素.
<!doctype html> <html lang = "en"> <style> .box{ font-size:15px; padding:15px; } .box1{background:green; flex-basis:150px; } .box2{background:blue; flex-basis:150px;} .box3{background:red; flex-basis:150px;} .container{ display:flex; height:100vh; align-items: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> </body> </html>
我们使用flex-grow属性来设置flex-grow因子。 如果容器中有多余的空间,它指定特定的柔性项目应该增长多少。
<!doctype html> <html lang = "en"> <style> .box{ font-size:15px; padding:15px; } .box1{background:green; flex-grow:10; flex-basis:100px; } .box2{background:blue; flex-grow:1; flex-basis:100px; } .box3{background:red; flex-grow:1; flex-basis:100px; } .container{ display:flex; height:100vh; align-items: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> </body> </html>
我们使用flex-shrink属性来设置flex收缩因子。 如果容器中没有足够的空间,则指定Flex项目应缩小的程度。
<!doctype html> <html lang = "en"> <style> .box{ font-size:15px; padding:15px; } .box1{background:green; flex-basis:200px; flex-shrink:10} .box2{background:blue; flex-basis:200px; flex-shrink:1} .box3{background:red; flex-basis:200px; flex-shrink:1} .container{ display:flex; height:100vh; align-items: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> </body> </html>
有一个简写到一次为所有这三个属性设置值;它被称为 flex .使用此属性,可以一次将值设置为flex-grow,flex-shrink和flex-basis值.以下是此属性的语法.
.item { flex: none | [ <'flex-grow'> <'flex-shrink'>? || <'flex-basis'> ] }