Flex 弹性布局
多行自适应,多列自适应,间隔也能自适应,任意对齐
创建弹性容器 flex container
display: block | inline | inline-block | none | flex
弹性元素 flex item
是不是flex container 弹性容器中所有的子元素都是 弹性元素呢?
不是的,只有弹性容器,在文档流中的直接子元素(不包含孙子元素 )才是弹性元素。
flex 布局特性
排列和方向
flex排列和方向的属性 | 描述 |
flex-direction
flex-direction取值 | 描述 |
row (flex-direction默认值)
row-reverse
column
column-reverse
举个栗子:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>flex direction</title> <style type="text/css"> .container{margin: 20px;line-height: 40px;font-size: 20px;color: #fff;background-color: #963297;} .item{margin: 10px;line-height: 40px;text-align: center;background-color: #c99702;} .container0 .item, .container1 .item{padding: 0 12px;} .container{display:flex;} .container1{flex-direction: row-reverse;} .container2{flex-direction: column;} .container3{flex-direction: column-reverse;} </style> </head> <body> <div class="container container0"> <div class="item">1</div> <div class="item">2</div> <div class="item">3</div> </div> <div class="container container1"> <div class="item">1</div> <div class="item">2</div> <div class="item">3</div> </div> <div class="container container2"> <div class="item">1</div> <div class="item">2</div> <div class="item">3</div> </div> <div class="container container3"> <div class="item">1</div> <div class="item">2</div> <div class="item">3</div> </div> </body> </html>
flex-wrap
flex-wrap取值 | 描述 |
wrap
举个栗子:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>flex-direction</title> <style type="text/css"> .container{width: 400px;margin: 20px;line-height: 40px;font-size: 20px;color: #fff;background-color: #963297;} .item{margin: 10px;width: 100px;line-height: 40px;text-align: center;background-color: #c99702;} .container{display: flex;} .container1{flex-wrap: wrap;} .container2{flex-wrap: wrap-reverse;} </style> </head> <body> <div class="container container0"> <div class="item">1</div> <div class="item">2</div> <div class="item">3</div> <div class="item">4</div> <div class="item">5</div> </div> <div class="container container1"> <div class="item">1</div> <div class="item">2</div> <div class="item">3</div> <div class="item">4</div> <div class="item">5</div> </div> <div class="container container2"> <div class="item">1</div> <div class="item">2</div> <div class="item">3</div> <div class="item">4</div> <div class="item">5</div> </div> </body> </html>
flex-flow(推荐使用)
flex-flow:<' flex-direction '> || <' flex-wrap '>
flex-flow取值 | 描述 |
推荐使用 flex-flow ,而不是单独用flex-direction 或 flew-wrap