Flexible boxes, or flexbox, is a new layout mode in CSS3.
Use of flexbox ensures that elements behave predictably when the page layout must accommodate different screen sizes and different display devices.
For many applications, the flexible box model provides an improvement over the block model in that it does not use floats, nor do the flex container's margins collapse with the margins of its contents.
The numbers in the table specify the first browser version that fully supports the feature.
Numbers followed by -webkit- or -moz- specify the first version that worked with a prefix.
![]() |
![]() |
![]() |
![]() |
![]() | |
---|---|---|---|---|---|
Basic support (single-line flexbox) |
11.0 | 29.0 21.0 -webkit- |
22.0 18.0 -moz- |
6.1 -webkit- | 12.1 -webkit- |
Multi-line flexbox | 11.0 | 29.0 21.0 -webkit- |
28.0 | 6.1 -webkit- | 17.0 15.0 -webkit- 12.1 |
Flexbox consists of flex containers and flex items.
A flex container is declared by setting the display
property of
an element to either flex
(rendered as a block) or inline-
flex
(rendered as inline).
Inside a flex container there is one or more flex items.
Note: Everything outside a flex container and inside a flex item is rendered as usual. Flexbox defines how flex items are laid out inside a flex container.
Flex items are positioned inside a flex container along a flex line. By default there is only one flex line per flex container.
The following example shows three flex items. They are positioned by default: along the horizontal flex line, from left to right:
It is also possible to change the direction of the flex line.
If we set the direction
property to rtl
(right-to-
left), the text is drawn right to left, and also the flex line changes
direction, which will change the page layout:
The flex-direction
property specifies the direction of the
flexible items inside the flex container. The default value of
flex-direction
is row
(left-to-right, top-to-
bottom).
The other values are as follows:
row-reverse
- If the writing-mode (direction) is left to
right, the flex items will be laid out right to leftcolumn
- If the writing system is horizontal, the flex items
will be laid out verticallycolumn-reverse
- Same as column, but reversedThe following example shows the result of using the row-reverse
value:
The following example shows the result of using the column
value:
The following example shows the result of using the column-
reverse
value:
The justify-content
property horizontally aligns the flexible
container's items when the items do not use all available space on the main-
axis.
The possible values are as follows:
flex-start
- Default value. Items are positioned at the
beginning of the containerflex-end
- Items are positioned at the end of the
containercenter
- Items are positioned at the center of the
containerspace-between
- Items are positioned with space between the
linesspace-around
- Items are positioned with space before,
between, and after the linesThe following example shows the result of using the flex-end
value:
The following example shows the result of using the center
value:
The following example shows the result of using the space-
between
value:
The following example shows the result of using the space-around
value:
The align-items
property vertically aligns the flexible
container's items when the items do not use all available space on the cross-
axis.
The possible values are as follows:
stretch
- Default value. Items are stretched to fit the
containerflex-start
- Items are positioned at the top of the
containerflex-end
- Items are positioned at the bottom of the
containercenter
- Items are positioned at the center of the container
(vertically)baseline
- Items are positioned at the baseline of the
containerThe following example shows the result of using the stretch
value (this is the default value):
The following example shows the result of using the flex-start
value:
The following example shows the result of using the flex-end
value:
The following example shows the result of using the center
value:
The following example shows the result of using the baseline
value:
The flex-wrap
property specifies whether the flex items should
wrap or not, if there is not enough room for them on one flex line.
The possible values are as follows:
nowrap
- Default value. The flexible items will not wrapwrap
- The flexible items will wrap if necessarywrap-reverse
- The flexible items will wrap, if necessary,
in reverse orderThe following example shows the result of using the nowrap
value
(this is the default value):
The following example shows the result of using the wrap
value:
The following example shows the result of using the wrap-reverse
value:
The align-content
property modifies the behavior of the
flex-wrap
property. It is similar to align-items
, but
instead of aligning flex items, it aligns flex lines.
The possible values are as follows:
stretch
- Default value. lines stretch to take up the
remaining spaceflex-start
- lines are packed toward the start of the flex
containerflex-end
- lines are packed toward the end of the flex
containercenter
- lines are packed toward the center of the flex
containerspace-between
- lines are evenly distributed in the flex
containerspace-around
- lines are evenly distributed in the flex
container, with half-size spaces on either endThe following example shows the result of using the center
value:
The order
property specifies the order of a flexible item
relative to the rest of the flexible items inside the same container:
Setting margin: auto;
will absorb extra space. It can be used to
push flex items into different positions.
In the following example we set margin-right: auto;
on the first
flex item. This will cause all the extra space to be absorbed to the right of
that element:
In the following example we will solve an almost daily problem: perfect centering.
It is very easy with flexbox. Setting margin: auto;
will make
the item perfectly centered in both axis:
The align-self
property of flex items overrides the flex
container's align-items property for that item. It has the same possible values
as the align-items
property.
The following example sets different align-self values to each flex item:
The flex
property specifies the length of the flex item,
relative to the rest of the flex items inside the same container.
In the following example, the first flex item will consume 2/4 of the free space, and the other two flex items will consume 1/4 of the free space each:
The following table lists the CSS properties used with flexbox:
Property | Description |
---|---|
display | Specifies the type of box used for an HTML element |
flex-direction | Specifies the direction of the flexible items inside a flex container |
justify-content | Horizontally aligns the flex items when the items do not use all available space on the main-axis |
align-items | Vertically aligns the flex items when the items do not use all available space on the cross-axis |
flex-wrap | Specifies whether the flex items should wrap or not, if there is not enough room for them on one flex line |
align-content | Modifies the behavior of the flex-wrap property. It is similar to align- items, but instead of aligning flex items, it aligns flex lines |
flex-flow | A shorthand propert for flex-direction and flex-wrap |
order | Specifies the order of a flexible item relative to the rest of the flex items inside the same container |
align-self | Used on flex items. Overrides the container's align-items property |
flex | Specifies the length of a flex item, relative to the rest of the flex items inside the same container |
The @media
rule, introduced in CSS2, made it possible to define
different style rules for different media types.
Examples: You could have one set of style rules for computer screens, one for printers, one for handheld devices, one for television-type devices, and so on.
Unfortunately these media types never got a lot of support by devices, other than the print media type.
Media queries in CSS3 extend the CSS2 media types idea: Instead of looking for a type of device, they look at the capability of the device.
Media queries can be used to check many things, such as:
Using media queries are a popular technique for delivering a tailored style sheet to tablets, iPhone, and Androids.
The numbers in the table specifies the first browser version that fully supports the @media rule.
Rule |
![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
@media | 21 | 9 | 3.5 | 4.0 | 9 |
A media query consists of a media type and can contain one or more expressions, which resolve to either true or false.
The result of the query is true if the specified media type matches the type of device the document is being displayed on and all expressions in the media query are true. When a media query is true, the corresponding style sheet or style rules are applied, following the normal cascading rules.
Unless you use the not or only operators, the media type is optional and the
all
type will be implied.
You can also have different stylesheets for different media:
Value | Description |
---|---|
all | Used for all media type devices |
Used for printers | |
screen | Used for computer screens, tablets, smart-phones etc. |
speech | Used for screenreaders that "reads" the page out loud |
One way to use media queries is to have an alternate CSS section right inside your style sheet.
The following example changes the background-color to lightgreen if the viewport is 480 pixels wide or wider (if the viewport is less than 480 pixels, the background-color will be pink):
The following example shows a menu that will float to the left of the page if the viewport is 480 pixels wide or wider (if the viewport is less than 480 pixels, the menu will be on top of the content):
For a full overview of all the media types and features/expressions, please look at the @media rule in CSS reference.
Let us look at some more examples of using media queries.
We will start with a list of names which function as email links. The HTML is:
Notice the data-email
attribute. In HTML5, we can use attributes
prefixed with data-
to store information. We will use the
data-
attribute later.
When the browser's width is between 520 and 699px, we will apply an email icon to each email link:
When the browser's width is between from 700 to 1000px, we will preface each email link with the text "Email:";
When the browser's width is above 1001px, we will append the email address to the links.
We will use the value of the data-
attribute to add the email
address after the person's name:
For browser widths above 1151px, we will again add the icon as we used before.
Here, we do not have to write an additional media query block, we can just append an additional media query to our already existing one using a comma (this will behave like an OR operator):