1

I'm having a hard time explaining my problem, have a look

body { width: 200px }

.a {
  border: solid 1px black;
  display: flex;
}

.b {
  flex: 1;
}
<div class=a>
  <div class=b>
    <span>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</span>
  </div>
  <button>
    click
  </button>
</div>

What I need is

body { width: 200px }

.a {
  border: solid 1px black;
  display: flex;
}

.b {
  flex: 1;
}
<div class=a>
  <div class=b>
    <span>aaaaaaaaaaaaaaaaaaaaa</span>
  </div>
  <button>
    click
  </button>
</div>

Basically cut off the text no matter how long it is and keep it on 1 line. I just can't wrap my head around what I'm messing up... I tried overflow, text-overflow, word-wrap, word-break, white-space - no luck.

Asons
  • 84,923
  • 12
  • 110
  • 165
php_nub_qq
  • 15,199
  • 21
  • 74
  • 144

3 Answers3

2

The reason this occur is based on the fact that flex row item's min-width defaults to auto.

This mean that it can be smaller than its content, hence the content in a will overflow its parent, and with that also push the button along with it.

The solution is to either set min-width to 0, or use overflow with a value other than visible.

In this case the overflow will work better, as seen in below sample, as when using min-width the overflowed text will be visible.

Stack snippet

body { width: 200px }

.a {
  border: solid 1px black;
  display: flex;
}

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

.b2 {
  flex: 1;
  overflow: hidden;
}
<div class=a>
  <div class=b>
    <span>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</span>
  </div>
  <button>
    click
  </button>
</div>

<div class=a>
  <div class=b2>
    <span>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</span>
  </div>
  <button>
    click
  </button>
</div>
Asons
  • 84,923
  • 12
  • 110
  • 165
0

You can achieve this with a max-width and an overflow-hidden in the B element.

body { width: 200px }

.a {
  border: solid 1px black;
  display: flex;
}

.b {
  flex: 1;
  max-width: 200px;
  overflow: hidden;
}

You can add another element and a max-height to item b so that horizontal scrolling is hidden and all text can be selected.

https://jsfiddle.net/pk7etoka/

Rinho
  • 564
  • 3
  • 8
0

You can try this as well:

body { width: 200px }

.a {
  border: solid 1px black;
  display: flex;
}

.b {
  flex: 1;
  overflow-x:auto;
  max-width:200px;
}
<div class=a>
  <div class=b>
    <span>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</span>
  </div>
  <button>
    click
  </button>
</div>

You can also use the other overflow methods like overflow:hidden/scroll as per your requirement.

AKNair
  • 1,369
  • 1
  • 12
  • 24