Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

CSS3 Flexbox

CSS3 Flexible Box (Flex Box)

The flexible box is a new layout mode in CSS3.

CSS3 flexible box (Flexible Box or flexbox) is a layout approach that ensures elements behave appropriately when a page needs to adapt to different screen sizes and device types.

The flexible box layout model was introduced to provide a more efficient way to arrange, align, and distribute space among child elements in a container.


Browser support

The numbers in the table indicate the version number of the first browser that supports this property.

The -webkit- or -moz- following a number is the prefix for the specified browser.

property
Basic support
(single-line flexbox) 29.0
21.0 -webkit- 11.0 22.0
18.0 -moz- 6.1 -webkit- 12.1 -webkit-
Multi-line flexbox 29.0
21.0 -webkit- 11.0 28.0 6.1 -webkit- 17.0
 15.0 -webkit-       

 12.1 |


CSS3 Flexbox Content

A flexbox consists of a flex container and flex items.

A flex container is defined by setting the display property to flex or inline-flex.

A flex container contains one or more flex items.

Note: Outside the flex container and inside the flex items, rendering is normal. Flexbox only defines how flex items are laid out inside the flex container.

Flex items are usually displayed in a single row within the flexbox. By default each container has only one row.

The following elements show flex items displayed in a single row, from left to right:

Example

<!DOCTYPE html>
<html>
<head>
<style.flex-container{>

display:

webkit-flex

;

display:

flex

;

width:

400

px

;

height:

250

px

;

background-color:

lightgrey

;

}

.flex-item

{

background-color:

cornflowerblue

;

width:

100

px

;

height:

100

px

;

margin:

10

px

;

}

style

head

<body>
<div class="flex-container">
<div class="flex-item">

flex item 1

div

<div class="flex-item">

flex item 2

div

<div class="flex-item">

flex item 3

div

div

body

html

Try it »

Of course we can change the arrangement.

If we set direction attribute to rtl (right-to-left), the arrangement of the flex items will change, and the page layout will change accordingly:

Example

body

{

direction:

rtl

;

}

.flex-container

{

display:

webkit-flex

;

display:

flex

;

width:

400

px

;

height:

250

px

;

background-color:

lightgrey

;

}

.flex-item

{

background-color:

cornflowerblue

;

width:

100

px

;

height:

100

px

;

margin:

10

px

;

}

Try it »


flex-direction

flex-direction The property specifies the position of flex items in the parent container.

Syntax



flex-direction: row | row-reverse | column | column-reverse

flex-directionThe values are:

The following example demonstrates the row-reverse usage:

Example

.flex-container

{

display:

webkit-flex

;

display:

flex

; -

webkit-flex-direction:

row-reverse

;

flex-direction:

row-reverse

;

width:

400

px

;

height:

250

px

;

background-color:

lightgrey

;

}

Try it »

The following example demonstrates the column usage:

Example

.flex-container

{

display:

webkit-flex

;

display:

flex

; -

webkit-flex-direction:

column

;

flex-direction:

column

;

width:

400

px

;

height:

250

px

;

background-color:

lightgrey

;

}

Try it »

The following example demonstrates the column-reverse usage:

Example

.flex-container

{

display:

webkit-flex

;

display:

flex

; -

webkit-flex-direction:

column-reverse

;

flex-direction:

column-reverse

;

width:

400

px

;

height:

250

px

;

background-color:

lightgrey

;

}

Try it »


justify-content Property

The justify-content property is applied to the flex container to align flex items along the main axis of the container.

The justify-content syntax is as follows:

justify-content: flex-start | flex-end | center | space-between | space-around

Explanation of each value:

The effect is shown below:

The following example demonstrates the flex-end usage:

Example

.flex-container

{

display:

webkit-flex

;

display:

flex

; -

webkit-justify-content:

flex-end

;

justify-content:

flex-end

;

width:

400

px

;

height:

250

px

;

background-color:

lightgrey

;

}

Try it »

The following example demonstrates the center usage:

Example

.flex-container

{

display:

webkit-flex

;

display:

flex

; -

webkit-justify-content:

center

;

justify-content:

center

;

width:

400

px

;

height:

250

px

;

background-color:

lightgrey

;

}

Try it »

The following example demonstrates the space-between usage:

Example

.flex-container

{

display:

webkit-flex

;

display:

flex

; -

webkit-justify-content:

space-between

;

justify-content:

space-between

;

width:

400

px

;

height:

250

px

;

background-color:

lightgrey

;

}

Try it »

The following example demonstrates the space-around usage:

Example

.flex-container

{

display:

webkit-flex

;

display:

flex

; -

webkit-justify-content:

space-around

;

justify-content:

space-around

;

width:

400

px

;

height:

250

px

;

background-color:

lightgrey

;

}

Try it »


align-items Property

align-items Sets or retrieves the alignment of flexbox elements along the cross axis (vertical axis).

Syntax



align-items: flex-start | flex-end | center | baseline | stretch

Explanation of each value:

The following example demonstrates the stretch(默认值) usage:

Example

.flex-container

{

display:

webkit-flex

;

display:

flex

; -

webkit-align-items:

stretch

;

align-items:

stretch

;

width:

400

px

;

height:

250

px

;

background-color:

lightgrey

;

}

Try it »

The following example demonstrates the flex-start usage:

Example

.flex-container

{

display:

webkit-flex

;

display:

flex

; -

webkit-align-items:

flex-start

;

align-items:

flex-start

;

width:

400

px

;

height:

250

px

;

background-color:

lightgrey

;

}

Try it »

The following example demonstrates the flex-end usage:

Example

.flex-container

{

display:

webkit-flex

;

display:

flex

; -

webkit-align-items:

flex-end

;

align-items:

flex-end

;

width:

400

px

;

height:

250

px

;

background-color:

lightgrey

;

}

Try it »

The following example demonstrates the center usage:

Example

.flex-container

{

display:

webkit-flex

;

display:

flex

; -

webkit-align-items:

center

;

align-items:

center

;

width:

400

px

;

height:

250

px

;

background-color:

lightgrey

;

}

Try it »

The following example demonstrates the baseline usage:

Example

.flex-container

{

display:

webkit-flex

;

display:

flex

; -

webkit-align-items:

baseline

;

align-items:

baseline

;

width:

400

px

;

height:

250

px

;

background-color:

lightgrey

;

}

Try it »


flex-wrap Property

flex-wrap The property specifies how the child elements of a flexbox wrap.

Syntax



flex-wrap: nowrap|wrap|wrap-reverse|initial|inherit;

Explanation of each value:

The following example demonstrates the nowrap usage:

Example

.flex-container

{

display:

webkit-flex

;

display:

flex

; -

webkit-flex-wrap:

nowrap

;

flex-wrap:

nowrap

;

width:

300

px

;

height:

250

px

;

background-color:

lightgrey

;

}

Try it »

The following example demonstrates the wrap usage:

Example

.flex-container

{

display:

webkit-flex

;

display:

flex

; -

webkit-flex-wrap:

wrap

;

flex-wrap:

wrap

;

width:

300

px

;

height:

250

px

;

background-color:

lightgrey

;

}

Try it »

The following example demonstrates the wrap-reverse usage:

Example

.flex-container

{

display:

webkit-flex

;

display:

flex

; -

webkit-flex-wrap:

wrap-reverse

;

flex-wrap:

wrap-reverse

;

width:

300

px

;

height:

250

px

;

background-color:

lightgrey

;

}

Try it »


align-content Property

align-content The property is used to modify the flex-wrap behavior of the property. Similar to align-items, but instead of setting the alignment of flex items, it sets the alignment of the lines.

Syntax



align-content: flex-start | flex-end | center | space-between | space-around | stretch

Explanation of each value:

The following example demonstrates the center usage:

Example

.flex-container

{

display:

webkit-flex

;

display:

flex

; -

webkit-flex-wrap:

wrap

;

flex-wrap:

wrap

; -

webkit-align-content:

center

;

align-content:

center

;

width:

300

px

;

height:

300

px

;

background-color:

lightgrey

;

}

Try it »


Flex Item Properties

Sorting

Syntax



order: 

Explanation of each value:

order The property sets properties of flex items within the flex container:

Example

.flex-item

{

background-color:

cornflowerblue

;

width:

100

px

;

height:

100

px

;

margin:

10

px

;

}

.first

{

webkit-order:

1

;

order:

1

;

}

Try it »

Alignment

Setting the "margin" value to "auto" automatically takes up the remaining space in the flex container. So setting the vertical margin to "auto" can center a flex item completely along both axes of the flex container.

The following example sets on the first flex item margin-right: auto; . It places the remaining space to the right of the element:

Example

.flex-item

{

background-color:

cornflowerblue

;

width:

75

px

;

height:

75

px

;

margin:

10

px

;

}

.flex-item

:first-child

{

margin-right:

auto

;

}

Try it »

Perfect Centering

The following example will perfectly solve the centering problems we usually encounter.

With flexbox, centering becomes very simple; you only need to set margin: auto; which can center a flex item completely along both axes:

Example

.flex-item

{

background-color:

cornflowerblue

;

width:

75

px

;

height:

75

px

;

margin:

auto

;

}

Try it »

align-self

align-self The property sets the alignment of the flex element itself along the cross axis (vertical axis).

Syntax



align-self: auto | flex-start | flex-end | center | baseline | stretch

Explanation of each value:

The following example demonstrates the effect of different align-self values on a flex item:

Example

.flex-item

{

background-color:

cornflowerblue

;

width:

60

px

;

min-height:

100

px

;

margin:

10

px

;

}

.item1

{

webkit-align-self:

flex-start

;

align-self:

flex-start

;

}

.item2

{

webkit-align-self:

flex-end

;

align-self:

flex-end

;

}

.item3

{

webkit-align-self:

center

;

align-self:

center

;

}

.item4

{

webkit-align-self:

baseline

;

align-self:

baseline

;

}

.item5

{

webkit-align-self:

stretch

;

align-self:

stretch

;

}

Try it »

flex

flex The property specifies how a flex item distributes space.

Syntax



flex: auto | initial | none | inherit |  [ flex-grow ] || [ flex-shrink ] || [ flex-basis ]

Explanation of each value:

In the following example, the first flex item takes 2/4 of the space, and the other two each take 1/4:

Example

.flex-item

{

background-color:

cornflowerblue

;

margin:

10

px

;

}

.item1

{

webkit-flex:

2

;

flex:

2

;

}

.item2

{

webkit-flex:

1

;

flex:

1

;

}

.item3

{

webkit-flex:

1

;

flex:

1

;

}

Try it »


More examples

Creating a Responsive Page with Flexbox


CSS3 Flexbox Properties

The table below lists the properties commonly used in flexbox:

property Description
display Specifies the box type of an HTML element.
flex-direction Specifies the arrangement of child elements in a flex container
justify-content Sets the alignment of flexbox elements along the main axis (horizontal axis).
align-items Sets the alignment of flexbox elements along the cross axis (vertical axis).
flex-wrap Sets whether the child elements of a flexbox wrap when they overflow the parent container.
align-content Modifies the behavior of the flex-wrap property, similar to align-items, but instead of setting the alignment of child elements, it sets the alignment of lines
flex-flow Shorthand for flex-direction and flex-wrap
order Sets the order of the child elements of a flexbox.
align-self Used on a flex item. Overrides the container's align-items property.
flex Sets how the child elements of a flexbox distribute space.