Special Offer Diwali Special Bundle Offer: 3 courses for ₹8,999, save ₹9,001 (50% OFF)! Limited seats available. View Bundles →
0% Completed
Tutorials / CSS Tutorial / Chapter 4: Layouts, Positioning & Modern Flexbox
CSS ⏱ 12 min read

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

Key Item Properties: • `flex-grow`: Number defining how much the item can grow relative to siblings if extra space is available (e.g. `flex-grow: 1`). • `flex-shrink`: Number defining how the item can shrink if space is insufficient (e.g. `0` prevents shrinking). • `flex-basis`: Initial default size of the item before remaining space is distributed (e.g. `250px` or `auto`). • `flex: 1`: Shorthand for `flex: 1 1 0%` (grow, shrink, 0 basis). Makes all items share space equally. • `align-self`: Overrides container's `align-items` for this single item.

Syntax Structure

Syntax Blueprint
.item {
  flex: 1; /* Grow and shrink equally */
}

.sidebar {
  flex: 0 0 280px; /* Do not grow, do not shrink, stay 280px */
}

Code Example

CSS Code
/* 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

• The input automatically grows to occupy the full container width minus the fixed width of the button. • `flex-shrink: 0` ensures the button will never collapse on small mobile viewports.

Expected Output

A search bar where the input stretches dynamically to fill width, while the search button retains its exact dimensions.

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

• Not setting `flex-shrink: 0` on icons or avatars, resulting in them getting deformed when parent narrows. • Setting fixed pixel widths on flex items when `flex-basis` or `flex` shorthand is appropriate.

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.

Key Takeaways

• Use `flex-grow: 1` to expand an element into available whitespace. • Use `flex-shrink: 0` to protect buttons, avatars, and icons from collapsing. • The shorthand `flex: 1` is ideal for equal-width column layouts.