Flexbox Complete Guide: Item Properties
While container properties organize the overall layout, flex item properties let individual child elements grow, shrink, and prioritize their size.
What is it?
Flex item properties apply to direct children of a flex container, including `flex-grow`, `flex-shrink`, `flex-basis`, `flex` shorthand, and `align-self`.
Why is it used?
Controls how individual elements adapt when viewport space changes—e.g. letting a search input expand to fill all leftover space while the submit button keeps fixed width.
Detailed Explanation
Syntax Structure
.item {
flex: 1; /* Grow and shrink equally */
}
.sidebar {
flex: 0 0 280px; /* Do not grow, do not shrink, stay 280px */
}
Code Example
/* Search Bar with Expanding Input and Fixed Button */
.search-form {
display: flex;
gap: 12px;
max-width: 600px;
}
.search-input {
flex-grow: 1; /* Takes all available remaining horizontal space */
padding: 10px 16px;
border: 1px solid #eceae4;
border-radius: 6px;
}
.search-button {
flex-shrink: 0; /* Guarantees button text never gets squished */
padding: 10px 20px;
background: #1c1c1c;
color: #fff;
border: none;
border-radius: 6px;
white-space: nowrap;
}
Explanation of the Code
Expected Output
Real-World Application
Used in chat apps (message input box expands while send button stays fixed), sidebars, and split-screen dashboards.
Common Mistakes to Avoid
Quick Practice Challenge
Build a layout with a fixed 260px sidebar and an main content area that expands to take the rest of the window.