CSS flex Property
Shorthand for flex-grow, flex-shrink, and flex-basis combined
Syntax
flex: none | [ <flex-grow> <flex-shrink>? || <flex-basis> ];Values
| Value | Description |
|---|---|
| initial | Equivalent to 0 1 auto |
| auto | Equivalent to 1 1 auto |
| none | Equivalent to 0 0 auto |
| 1 | Grow equally, equivalent to 1 1 0 |
| <number> | Sets flex-grow with flex-shrink: 1 and flex-basis: 0 |
Example
.item {
flex: 1;
}
.sidebar {
flex: 0 0 300px;
}Understanding CSS flex
The CSS flex property shorthand for flex-grow, flex-shrink, and flex-basis combined. As part of the Flexbox module in CSS, it is one of the most commonly used properties for controlling the visual presentation of web pages.
You can set flex to values such as initial, auto, none, 1, among others. Each value changes how the browser renders the affected element, giving you fine-grained control over your page layout and design. Choosing the right value depends on the specific design requirements of your project.
Common use cases for the flex property include responsive web design, component-based layouts, and creating visually consistent interfaces across devices. It works closely with related properties like flex-grow, flex-shrink, flex-basis to achieve complex styling effects. Understanding how these properties interact helps you write cleaner, more maintainable stylesheets.
Browser support for flex is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. For older browsers, consider using fallback values or progressive enhancement strategies. The property can also be set dynamically via JavaScript using element.style.flex or the CSS custom properties (variables) approach for theming.
Related Properties
flex-growSets how much a flex item will grow relative to the rest of the items
flex-shrinkSets the flex shrink factor of a flex item to prevent overflow
flex-basisSets the initial main size of a flex item before free space is distributed
flex-directionDefines the direction flex items are placed in the flex container
Explore More CSS Properties
Browse our complete reference of 251 CSS properties with syntax, examples, and tips.