In the style design of web applications, layout is a very important part. Layout is used to determine the size and position of different components and elements on the page. With the popularity of responsive user interfaces, web applications are generally required to adapt to different device sizes and browser resolutions. The most important aspect of responsive user interface design is layout. The layout needs to be adjusted according to the window size, thereby changing the size and position of the components to achieve the best display effect. This also makes the layout logic more complex.
This article introduces the new layout model introduced in the CSS3 specification: the flex box model. The flexbox model can meet many common complex layout needs in a simple way. Its advantage is that developers only declare the behavior that the layout should have, without giving specific implementation methods. The browser is responsible for doing the actual layout. This layout model is supported in major browsers.
##CSS3 Flexible Box Content##Flexible The box is composed of a Flex container and a Flex item.
A flexible container is defined as a flexible container by setting the value of the display property to flex or inline-flex.
The flexible container contains one or more flexible sub-elements.
Note: The outside of the flexible container and inside the flexible sub-element are rendered normally. The flex box only defines how the flex child elements are laid out within the flex container.
Flexible sub-elements are usually displayed in one line within the flexible box. By default there is only one row per container.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php.cn</title> <style> .flex-container { display: -webkit-flex; display: flex; width: 400px; height: 150px; background-color: yellow; } .flex-item { background-color: cornflowerblue; width: 100px; height: 100px; margin: 10px; } </style> </head> <body> <div class="flex-container"> <div class="flex-item"> 1</div> <div class="flex-item"> 2</div> <div class="flex-item"> 3</div> </div> </body> </html>
flex-direction property
Sets or retrieves the child elements of the flex box object The position in the parent container.Syntax
flex-direction: row | row-reverse | column | column-reverse
row-reverse: Reverse the horizontal arrangement (right-aligned, from back to front, with the last item at the front.
column: Vertical arrangement.
row-reverse: Reverse the vertical arrangement, from back to front, with the last item on top
Browser support
Firefox, Opera and Chrome support the flex-direction property
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php.cn</title> <style> .box{ display:-webkit-flex; display:flex; margin:0;padding:10px;list-style:none;background-color:#eee;} .box li{width:100px;height:100px;border:1px solid #aaa;text-align:center;} #box{ -webkit-flex-direction:row; flex-direction:row; } #box2{ -webkit-flex-direction:row-reverse; flex-direction:row-reverse; } #box3{ height:500px; -webkit-flex-direction:column; flex-direction:column; } #box4{ height:500px; -webkit-flex-direction:column-reverse; flex-direction:column-reverse; } </style> </head> <body> <h2>flex-direction:row</h2> <ul id="box" class="box"> <li>a</li> <li>b</li> <li>c</li> </ul> <h2>flex-direction:row-reverse</h2> <ul id="box2" class="box"> <li>a</li> <li>b</li> <li>c</li> </ul> <h2>flex-direction:column</h2> <ul id="box3" class="box"> <li>a</li> <li>b</li> <li>c</li> </ul> <h2>flex-direction:column-reverse</h2> <ul id="box4" class="box"> <li>a</li> <li>b</li> <li>c</li> </ul> </body> </html>
##justify-content. Attribute
Sets or retrieves the alignment of the flexbox element in the main axis (horizontal axis) direction
When all child elements on a row in the flexbox. This property also controls alignment when an element overflows a row.Syntax
flex-start: The flex box element will be aligned to the start of the row. The edge of the main start of the row's first child element will be aligned with the edge of the row's main start, while all subsequent flex items will be aligned with their previous item.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php.cn</title> <style> .box{ display:-webkit-flex; display:flex; width:400px;height:100px;margin:0;padding:0;border-radius:5px;list-style:none;background-color:#eee;} .box li{margin:5px;padding:10px;border-radius:5px;background:#aaa;text-align:center;} #box{ -webkit-justify-content:flex-start; justify-content:flex-start; } #box2{ -webkit-justify-content:flex-end; justify-content:flex-end; } #box3{ -webkit-justify-content:center; justify-content:center; } #box4{ -webkit-justify-content:space-between; justify-content:space-between; } #box5{ -webkit-justify-content:space-around; justify-content:space-around; } </style> </head> <body> <ul id="box" class="box"> <li>a</li> <li>b</li> <li>c</li> </ul> <h2>justify-content:flex-end</h2> <ul id="box2" class="box"> <li>a</li> <li>b</li> <li>c</li> </ul> <h2>justify-content:center</h2> <ul id="box3" class="box"> <li>a</li> <li>b</li> <li>c</li> </ul> <h2>justify-content:space-between</h2> <ul id="box4" class="box"> <li>a</li> <li>b</li> <li>c</li> </ul> <h2>justify-content:space-around</h2> <ul id="box5" class="box"> <li>a</li> <li>b</li> <li>c</li> </ul> </body> </html>
align-items property
Set or retrieve the flex box element on the cross axis Alignment in the (vertical axis) direction.
Syntax
stretch: If the attribute value specifying the cross-axis size is 'auto', its value will make the size of the item's margin box as close as possible to the size of the row, but at the same time it will comply with 'min/max-width/height' 'Attribute restrictions.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php.cn</title> <style> .box{ display:-webkit-flex; display:flex; width:200px;height:100px;margin:0;padding:0;border-radius:5px;list-style:none;background-color:#eee;} .box li{margin:5px;border-radius:5px;background:#aaa;text-align:center;} .box li:nth-child(1){padding:10px;} .box li:nth-child(2){padding:15px 10px;} .box li:nth-child(3){padding:20px 10px;} #box{ -webkit-align-items:flex-start; align-items:flex-start; } #box2{ -webkit-align-items:flex-end; align-items:flex-end; } #box3{ -webkit-align-items:center; align-items:center; } #box4{ -webkit-align-items:baseline; align-items:baseline; } #box5{ -webkit-align-items:strecth; align-items:strecth; } </style> </head> <body> <h2>align-items:flex-start</h2> <ul id="box" class="box"> <li>a</li> <li>b</li> <li>c</li> </ul> <h2>align-items:flex-end</h2> <ul id="box2" class="box"> <li>a</li> <li>b</li> <li>c</li> </ul> <h2>align-items:center</h2> <ul id="box3" class="box"> <li>a</li> <li>b</li> <li>c</li> </ul> <h2>align-items:baseline</h2> <ul id="box4" class="box"> <li>a</li> <li>b</li> <li>c</li> </ul> <h2>align-items:strecth</h2> <ul id="box5" class="box"> <li>a</li> <li>b</li> <li>c</li> </ul> </body> </html>
flex-wrap property
Sets or retrieves the child elements of the flex box object Whether to wrap when exceeding the parent container.
Syntax
flex-wrap: nowrap | wrap | wrap-reverse
nowrap: Do not wrap when the child element overflows the parent container.
wrap: Automatically wrap when the child element overflows the parent container.
wrap-reverse: Reverse the wrap arrangement.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php.cn</title> <style> .box{ display:-webkit-flex; display:flex; width:220px;margin:0;padding:10px;list-style:none;background-color:#eee;} .box li{width:100px;height:100px;border:1px solid #aaa;text-align:center;} #box{ -webkit-flex-wrap:nowrap; flex-wrap:nowrap; } #box2{ -webkit-flex-wrap:wrap; flex-wrap:wrap; } #box3{ -webkit-flex-wrap:wrap-reverse; flex-wrap:wrap-reverse; } </style> </head> <body> <ul id="box" class="box"> <li>a</li> <li>b</li> <li>c</li> </ul> <h2>flex-wrap:wrap</h2> <ul id="box2" class="box"> <li>a</li> <li>b</li> <li>c</li> </ul> <h2>flex-wrap:wrap-reverse</h2> <ul id="box3" class="box"> <li>a</li> <li>b</li> <li>c</li> </ul> </body> </html>
align-content property
Sets or retrieves the flex box stacking telescopic row Alignment.
Syntax
align-content: flex-start | flex-end | center | space- between | space-around | stretch
flex-start: Each row is stacked toward the starting position of the flex box container. The cross-axis starting edge of the first row in the flexbox container is adjacent to the cross-axis starting edge of the flexbox container, and each subsequent row is adjacent to the previous row.
flex-end: Each row is stacked toward the end of the flex box container. The cross-axis end boundary of the last row in the flexbox container is close to the cross-axis end boundary of the flexbox container, and each subsequent row is close to the previous row.
center: Each row is stacked toward the middle of the flexbox container. The rows are aligned two by two and are centered in the flex container, maintaining the distance between the starting content edge of the flex container and the first row and the distance between the ending content edge of the container and the last row. The distance is equal. (If the remaining space is a negative number, the rows will spill out an equal distance in both directions.)
space-between: Rows are evenly distributed within the flexbox container. If the remaining space is negative or there is only one row in the flex container, this value is equivalent to 'flex-start'. In other cases, the first row's cross-axis start edge is against the flex container's cross-start content edge, the last row's cross-axis end edge is against the flex container's cross-end content edge, and the remaining The rows are arranged in the flexbox window in such a way that the space between them is equal.
space-around: Each row is evenly distributed in the flexbox container, leaving half of the space between sub-elements at both ends. If the remaining space is negative or there is only one row in the flexbox container, this value is equivalent to 'center'. In other cases, the rows are arranged in the flexbox container in such a way that the space between them is equal, and the space before the first row and after the last row is half the space of the other rows.
stretch: Each row will stretch to take up the remaining space. If the remaining space is negative, this value is equivalent to 'flex-start'. In other cases, the remaining space is divided equally among all rows to expand their cross-axis size.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php.cn</title> <style> .box{ display:-webkit-flex; display:flex; -webkit-flex-wrap:wrap; flex-direction:wrap; width:200px;height:200px;margin:0;padding:0;border-radius:5px;list-style:none;background-color:#eee;} .box li{margin:5px;padding:10px;border-radius:5px;background:#aaa;text-align:center;} #box{ -webkit-align-content:flex-start; align-content:flex-start; } #box2{ -webkit-align-content:flex-end; align-content:flex-end; } #box3{ -webkit-align-content:center; align-content:center; } #box4{ -webkit-align-content:space-between; align-content:space-between; } #box5{ -webkit-align-content:space-around; align-content:space-around; } #box6{ -webkit-align-content:strecth; align-content:strecth; } </style> </head> <body> <h2>align-content:flex-start</h2> <ul id="box" class="box"> <li>a</li> <li>b</li> <li>c</li> <li>d</li> <li>e</li> <li>f</li> </ul> <h2>align-content:flex-end</h2> <ul id="box2" class="box"> <li>a</li> <li>b</li> <li>c</li> <li>d</li> <li>e</li> <li>f</li> </ul> <h2>align-content:center</h2> <ul id="box3" class="box"> <li>a</li> <li>b</li> <li>c</li> <li>d</li> <li>e</li> <li>f</li> </ul> <h2>align-content:space-between</h2> <ul id="box4" class="box"> <li>a</li> <li>b</li> <li>c</li> <li>d</li> <li>e</li> <li>f</li> </ul> <h2>align-content:space-around</h2> <ul id="box5" class="box"> <li>a</li> <li>b</li> <li>c</li> <li>d</li> <li>e</li> <li>f</li> </ul> <h2>align-content:strecth</h2> <ul id="box6" class="box"> <li>a</li> <li>b</li> <li>c</li> <li>d</li> <li>e</li> <li>f</li> </ul> </body> </html>
order property
Sets or retrieves the occurrence of child elements of the flexbox model object Order.
Syntax
order: <integer>
<integer> : Use integer values to define the sort order, with smaller values listed first. Can be negative.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php.cn</title> <style> .box{ display:-webkit-flex; display:flex; margin:0;padding:10px;list-style:none;background-color:#eee;} .box li{width:100px;height:100px;border:1px solid #aaa;text-align:center;} #box li:nth-child(3){ -webkit-order:-1; order:-1; } </style> </head> <body> <ul id="box" class="box"> <li>a</li> <li>b</li> <li>c</li> <li>d</li> <li>e</li> </ul> </body> </html>
align-self attribute
Sets or retrieves the flex box element itself on the side Alignment along the axis (vertical axis).
Syntax
align-self: auto | flex-start | flex-end | center | baseline | stretch
auto: If the value of 'align-self' is 'auto', its calculated value is the 'align-items' value of the element's parent element, if it has no parent element, The calculated value is 'stretch'.
flex-start: The boundary of the starting position of the cross axis (vertical axis) of the flex box element is close to the starting boundary of the cross axis of the row.
flex-end: The boundary of the starting position of the cross axis (vertical axis) of the flex box element is close to the end boundary of the cross axis of the row.
center: The flexbox element is centered on the cross axis (vertical axis) of the row. (If the size of the row is smaller than the size of the flexbox element, it will overflow the same length in both directions).
baseline: If the inline axis and side axis of the flex box element are the same, this value is equivalent to 'flex-start'. Otherwise, this value will participate in baseline alignment.
stretch: If the attribute value specifying the cross-axis size is 'auto', its value will make the size of the item's margin box as close as possible to the size of the row, but at the same time it will comply with 'min/max- width/height' attributes.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php.cn</title> <style> .box{ display:-webkit-flex; display:flex; -webkit-align-items: flex-end; height:100px;margin:0;padding:10px;border-radius:5px;list-style:none;background-color:#eee;} .box li{margin:5px;padding:10px;border-radius:5px;background:#aaa;text-align:center;} .box li:nth-child(1){ -webkit-align-self: flex-end; align-self: flex-end; } .box li:nth-child(2){ -webkit-align-self: center; align-self: center; } .box li:nth-child(3){ -webkit-align-self: flex-start; align-self: flex-start; } .box li:nth-child(4){ -webkit-align-self: baseline; align-self: baseline; padding:20px 10px; } .box li:nth-child(5){ -webkit-align-self: baseline; align-self: baseline; } .box li:nth-child(6){ -webkit-align-self: stretch; align-self: stretch; } .box li:nth-child(7){ -webkit-align-self: auto; align-self: auto; } </style> </head> <body> <ul id="box" class="box"> <li>a</li> <li>b</li> <li>c</li> <li>d</li> <li>e</li> <li>f</li> <li>g</li> <li>h</li> <li>i</li> </ul> </body> </html>
flex property
Composite property. Sets or retrieves how the child elements of the flex box object allocate space.
If the abbreviation flex:1, its calculated value is: 1 1 0
Syntax
flex: none | [ flex-grow ] || [ flex-shrink ] || [ flex-basis ]
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php.cn</title> <style> .box{ display:-webkit-flex; display:flex; max-width:400px;height:100px;margin:10px 0 0;padding:0;border-radius:5px;list-style:none;background-color:#eee;} .box li{background:#aaa;text-align:center;} .box li:nth-child(1){background:#999;} .box li:nth-child(2){background:#aaa;} .box li:nth-child(3){background:#ccc;} #box li:nth-child(1){-webkit-flex:1;flex:1;} #box li:nth-child(2){-webkit-flex:1;flex:1;} #box li:nth-child(3){-webkit-flex:1;flex:1;} #box2 li:nth-child(1){-webkit-flex:1 0 100px;flex:1 0 100px;} #box2 li:nth-child(2){-webkit-flex:2 0 100px;flex:2 0 100px;} #box2 li:nth-child(3){-webkit-flex:3 0 100px;flex:3 0 100px;} #box3{max-width: 800px;} #box3 li:nth-child(1){-webkit-flex:1 1 300px;flex:1 1 300px;background:#999;} #box3 li:nth-child(2){-webkit-flex:1 2 500px;flex:1 2 500px;background:#aaa;} #box3 li:nth-child(3){-webkit-flex:1 3 600px;flex:1 3 600px;background:#ccc;} </style> </head> <body> <ul id="box" class="box"> <li>flex:1;</li> <li>flex:1;</li> <li>flex:1;</li> </ul> <ul id="box2" class="box"> <li>flex:1 0 100px;</li> <li>flex:2 0 100px;</li> <li>flex:3 0 100px;</li> </ul> <ul id="box3" class="box"> <li>flex:1 1 400px;</li> <li>flex:1 2 400px;</li> <li>flex:1 2 400px;</li> </ul> </body> </html>