Ova

How do you make one item full width in flexbox?

Published in Flexbox Layout 7 mins read

To make a single item span the full width of its container within a Flexbox layout, you primarily use Flexbox properties like flex-grow, flex-basis, or the flex shorthand. These properties allow you to precisely control an item's size and how it distributes space, ensuring it stretches across the entire available width while influencing the layout of other items.


Mastering Full-Width Flex Items

Achieving a full-width item in Flexbox often depends on how you want other items in the container to behave. Here are the most effective methods:

1. Using flex-grow for Space Distribution

The flex-grow property specifies how much a flex item will grow relative to the rest of the flex items in the container. Setting flex-grow: 1 on a specific item will make it consume all available free space.

  • How it works: If your flex container has multiple items, and you apply flex-grow: 1 to one item, that item will expand to fill any remaining space in the row. If there's only one item with flex-grow: 1, it will take up all available space.
  • Impact on other items: Other items without flex-grow (or with flex-grow: 0, the default) will maintain their content-based size or flex-basis size. The item with flex-grow: 1 will stretch to fill the remaining space.

Example: flex-grow

<div class="flex-container">
  <div class="flex-item">Item 1</div>
  <div class="flex-item full-width-item">Full-Width Item</div>
  <div class="flex-item">Item 3</div>
</div>
.flex-container {
  display: flex;
  width: 100%; /* Ensure container has a defined width */
  background-color: #eee;
  padding: 10px;
  gap: 10px; /* For spacing between items */
}

.flex-item {
  padding: 20px;
  background-color: lightblue;
  border: 1px solid steelblue;
  flex-shrink: 0; /* Prevent items from shrinking below their content size */
}

.full-width-item {
  flex-grow: 1; /* This item will take all available space */
  /* You might also want to set flex-basis to 0 or auto for predictable behavior */
  flex-basis: 0; /* Allows it to grow from 0, consuming all space */
  /* Or flex-basis: auto; to grow from its content size */
}

In this setup, "Full-Width Item" will expand to fill the space left by "Item 1" and "Item 3", effectively spanning the majority of the container's width. For more details on this property, refer to the MDN Web Docs on flex-grow.

2. Utilizing flex-basis for Initial Size Control

The flex-basis property defines the initial size of a flex item before any available space is distributed. Setting flex-basis to 100% makes the item initially occupy the full width of its flex container.

  • How it works: When flex-basis: 100% is applied, the item attempts to take up 100% of the container's width. If other items are present and flex-wrap is not set to wrap, these items will be pushed out of the row or shrink significantly. If flex-wrap: wrap is applied, the 100% width item will move to its own line.
  • Impact on other items: If flex-wrap: wrap is enabled on the container, the item with flex-basis: 100% will force other items onto new lines, effectively occupying an entire row by itself. If flex-wrap is not wrap, other items might be forced to shrink (flex-shrink: 1 default) or overflow.

Example: flex-basis with flex-wrap

<div class="flex-container-wrap">
  <div class="flex-item">Item A</div>
  <div class="flex-item full-width-item-basis">Full-Width Item (Basis)</div>
  <div class="flex-item">Item C</div>
</div>
.flex-container-wrap {
  display: flex;
  flex-wrap: wrap; /* Crucial for basis: 100% to create a new row */
  width: 100%;
  background-color: #f0f8ff;
  padding: 10px;
  gap: 10px;
}

.flex-item {
  padding: 20px;
  background-color: lightcoral;
  border: 1px solid darkred;
}

.full-width-item-basis {
  flex-basis: 100%; /* This item will try to take 100% of the container width */
  /* flex-grow: 0; flex-shrink: 0; are often good companions with flex-basis: 100% */
  flex-grow: 0;
  flex-shrink: 0;
}

In this scenario, "Full-Width Item (Basis)" will occupy its own row due to flex-basis: 100% and the flex-wrap: wrap property on the container. Explore more about flex-basis on MDN Web Docs.

3. Using the flex Shorthand

The flex shorthand property combines flex-grow, flex-shrink, and flex-basis into a single declaration. This is often the most concise way to control a flex item's behavior.

  • Syntax: flex: <flex-grow> <flex-shrink> <flex-basis>;
  • To make an item full width and take its own row: A common pattern is flex: 0 0 100% for an item intended to always take full width on its own line (similar to flex-basis: 100% with flex-wrap).
  • To make an item grow and fill remaining space: flex: 1 0 0% (or flex: 1 0 auto) is equivalent to flex-grow: 1, flex-shrink: 0, and flex-basis: 0% (or auto). This allows the item to grow from a minimal or content-defined size to fill available space.

Example: flex Shorthand

<div class="flex-container">
  <div class="flex-item">Section A</div>
  <div class="flex-item full-width-grow">Full-Width Grow</div>
  <div class="flex-item">Section C</div>
</div>

<div class="flex-container-wrap">
  <div class="flex-item">Section D</div>
  <div class="flex-item full-width-basis-shorthand">Full-Width Basis (Shorthand)</div>
  <div class="flex-item">Section F</div>
</div>
.flex-container, .flex-container-wrap {
  display: flex;
  width: 100%;
  padding: 10px;
  gap: 10px;
  margin-bottom: 20px;
  background-color: #e6ffe6;
}

.flex-container-wrap {
  flex-wrap: wrap;
}

.flex-item {
  padding: 20px;
  background-color: lightgreen;
  border: 1px solid darkgreen;
}

.full-width-grow {
  flex: 1 0 0; /* Grow, don't shrink, initial basis 0 (grow from nothing) */
}

.full-width-basis-shorthand {
  flex: 0 0 100%; /* Don't grow, don't shrink, initial basis 100% */
}

The flex shorthand is a powerful tool for controlling item dimensions. Learn more about it on the MDN Web Docs for flex.

Key Flexbox Properties for Full Width

Property Description Effect on Item Best Use Case
flex-grow: 1 Specifies how much a flex item will grow relative to the rest of the flex items to fill available space. The item will expand to consume all available free space within the flex container along the main axis. Other items without flex-grow will retain their size. When you want one item to stretch and fill remaining horizontal space, alongside other items that maintain their size.
flex-basis: 100% Defines the initial size of a flex item before any available space is distributed or shrunk. The item attempts to take up 100% of the container's width. If flex-wrap: wrap is applied to the container, this item will move to its own line, effectively becoming full-width on that row. If flex-wrap is not wrap, other items might be pushed out or severely compressed. When you want an item to occupy an entire row by itself, pushing other items to new lines (requires flex-wrap: wrap on the container).
flex: 1 0 0 Shorthand for flex-grow: 1, flex-shrink: 0, flex-basis: 0. Similar to flex-grow: 1, the item will grow to fill available space. The flex-basis: 0 ensures it grows from a minimal point, allowing it to take maximum advantage of the available space. A concise way to make an item stretch and fill remaining space.
flex: 0 0 100% Shorthand for flex-grow: 0, flex-shrink: 0, flex-basis: 100%. The item will not grow or shrink, and its initial size is set to 100% of the container. This makes it effectively full-width, especially when combined with flex-wrap: wrap on the container, which will place it on its own line. A concise way to force an item to its own full-width row (requires flex-wrap: wrap).

Practical Insights

  • Container Width: Ensure your flex container (.flex-container) itself has a defined width (e.g., width: 100%) so there is a clear boundary for the items to expand within.
  • flex-wrap is Key for flex-basis: 100%: If you want an item to truly take up its own full-width row using flex-basis: 100%, you must apply flex-wrap: wrap to the flex container. Otherwise, all items will try to stay on a single line, and the 100% item might just push others out of view or severely compress them.
  • flex-shrink: By default, flex-shrink is 1. This means items can shrink if there isn't enough space. If you want an item to strictly adhere to its flex-basis or width, you might set flex-shrink: 0.
  • Responsive Design: These Flexbox techniques are inherently responsive. As the container's width changes, the full-width item will adjust accordingly.

By strategically using flex-grow or flex-basis (often via the flex shorthand), you can ensure a specific flex item occupies the desired full width of its container, either by filling available space or by taking up an entire row.