Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expand multiple flexbox items to fit container width

Tags:

css

flexbox

I have a layout where I want to make the breadcrumb trail use up all of the available space in the row. In the same row I also have a div (.tools) which needs to be of a dynamic width as it's contents may vary.

At the moment I have to set the breadcrumb trail to a % width to get it to work roughly correctly, but this isn't ideal... I'd like it to just expand to fill the available space.

Any ideas?

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  background-color: lightgreen;
  width: 100%;
  box-sizing: border-box;
}

.breadcrumb {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  white-space: nowrap;
  width: 55%;
  background-color: lightblue;
}

.breadcrumbItem {
  display: flex;
  flex: 0 10 auto;
  min-width: 45px;
  align-items: center;
  overflow: hidden;
  text-overflow: ellipsis;
}
.breadcrumbItem button {
  border: 0;
  background: none;
  padding: 0 5px;
  flex: 0 1 auto;
  white-space: nowrap;
  width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tools {
  display: flex;
  justify-content: flex-end;
  background-color: lightyellow;
}
.tools span, .tools button {
  white-space: nowrap;
}
<header>
  <div class="breadcrumb">
    <div class="breadcrumbItem">
      <button>Really long breadcrumb item here</button>
      <span>
        <span>></span>
      </span>
    </div>
    <div class="breadcrumbItem">
      <button>Another really long item that should ellipse when it gets too long, oidsf jsodifj dsoifj sdoifj sdoifj sdoifj sdoifj sdoifj sdoifj dsoifj sdoifj sd</button>
    </div>
  </div>
  <div class="tools">
    <span>0 photos selected</span>
    <button>Button</button>
  </div>
</header>

https://codepen.io/anon/pen/NYaQyV

like image 374
richwol Avatar asked Oct 21 '25 19:10

richwol


1 Answers

You can use flex-grow: 1; or the shorthand flex: 1; along with min-width or overflow.

.breadcrumb {
  ...
  flex: 1;
  min-width: 0;
}

Or

.breadcrumb {
  ...
  flex: 1;
  overflow: hidden;
}

CodePen

like image 56
Stickers Avatar answered Oct 23 '25 08:10

Stickers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!