2

I'm trying to implement white-space: nowrap; and text-overflow: ellipsis; on a flex:1; div without setting the max-width. I want it to adjust to the width flex:1 gives.

Here is a fiddle that explains what I want and what actually happens: https://jsfiddle.net/dani3l/6y04bvu1/13/

I'd like to keep the solution css only if possible.

Thanks in advance!

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
sUP
  • 614
  • 3
  • 16
  • 1
    Related - http://stackoverflow.com/questions/31329835/css-fit-content-div-to-remaining-space/33228704#33228704 – Paulie_D Feb 09 '16 at 10:19

2 Answers2

0

to apply text-overflow: ellipsis, you have to give width or max-width otherwise it don't have any idea where it needs to stop.

Jay Patel
  • 5,783
  • 1
  • 17
  • 20
0

You can try this in your flex:1 class:

.flex-1, .flex-1 > * {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

Here is a snippet to show this.

.flex-container {
  display: flex;
  align-items: center;
  height: 50px;
  width: 120px;
  padding: 0 10px;
  background-color: black;
}

.flex-1 {
  flex: 1;
  color: white;
}

.flex-1, .flex-1 > * {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
<div class="flex-container">
  <div class="flex-1">
    This is very long text.
  </div>
</div>


<br>
<br>

<div class="flex-container">
  <div class="flex-1">
    <div>
      Child divs work too
    </div>
    <br />
    <span>
      Child spans work too
    </span>
  </div>
</div>

I found Larry Gordon's Codepen that helped me with this.

Stephen C
  • 843
  • 2
  • 14
  • 28