CSSflex Property
Example
Let all the flexible items be the same length, regardless of its content:
flex: 1;
}
Tip: More "Try it Yourself" examples below.
Definition and Usage
Theflex property is a shorthand property for:
Theflex property sets the flexible length on flexible items.
Note: If the element is not a flexible item, theflex property has no effect.
| Default value: | 0 1 auto |
|---|---|
| Inherited: | no |
| Animatable: | yes,see individual properties.Read aboutanimatable |
| Version: | CSS3 |
| JavaScript syntax: | object.style.flex="1"Try it |
Browser Support
The numbers in the table specify the first browser version that fully supports the property.
| Property | |||||
|---|---|---|---|---|---|
| flex | 29 | 11 | 28 | 9 | 17 |
CSS Syntax
Property Values
| Value | Description | Demo |
|---|---|---|
| flex-grow | A number specifying how much the item will grow relative to the rest of the flexible items | Demo ❯ |
| flex-shrink | A number specifying how much the item will shrink relative to the rest of the flexible items | |
| flex-basis | The length of the item. Legal values: "auto", "inherit", or a number followed by "%", "px", "em" or any other length unit | Demo ❯ |
| auto | Same as 1 1 auto. | |
| initial | Same as 0 1 auto.Read aboutinitial | |
| none | Same as 0 0 auto. | |
| inherit | Inherits this property from its parent element.Read aboutinherit |
More Examples
Example
Usingflex together withmedia queries to create a different layout for different screen sizes/devices:
display: flex;
flex-wrap: wrap;
}
.flex-item-left {
flex: 50%;
}
.flex-item-right {
flex: 50%;
}
/* Responsive layout - makes a one column layout (100%) instead of a two-column layout (50%) */
@media (max-width: 800px) {
.flex-item-right, .flex-item-left {
flex: 100%;
}
}
Related Pages
CSS Tutorial:CSS Flexible Box
CSS Reference:flex-basis property
CSS Reference:flex-direction property
CSS Reference:flex-flow property
CSS Reference:flex-grow property
CSS Reference:flex-shrink property
CSS Reference:flex-wrap property
HTML DOM reference:flex property

