Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to word-break text in a flex display when the text is long?

Tags:

html

css

flexbox

I need to make a responsive web, so that the width of parent is dynamic. There are two flex items, one is long (dynamic) and another one is short (static).

I hope the result can look like the second line, that the long text is broken (or hidden when overlap), and the short text is always displayed correctly.

I tried to use flex-shrink: 0 but seems there is always an overflow.

How can I get rid of the overflow in this case?

I do need flex layout, and the js should not be involved.

.parent {
  display: flex;
  flex-direction: row;
  width: 15rem;
  background: yellowgreen;
  padding: 10px;
  overflow: hidden;
}
.flex-item {
  width: 10em;
  padding: 10px;
  background: yellow;
  flex: 1 1 50%;
}
.block1 {
  background: red;
}
.block2 {
  background: orange;
}
.nos {
  flex-shrink: 0 !important;
}
<div class="parent">
  <div class="flex-item">
    <div class="block1">
      longlonglonglonglonglonglonglonglonglonglonglonglonglong
    </div>
  </div>
  <div class="flex-item nos">
    <div class="block2">
      Display
    </div>
  </div>
</div>
<br/>
<div class="parent">
  <div class="flex-item">
    <div class="block1">
      longlonglonglonglong...
    </div>
  </div>
  <div class="flex-item">
    <div class="block2">
      Display
    </div>
  </div>
</div>

https://jsfiddle.net/templefox/Lw3hhz8j/

like image 655
templefox Avatar asked Jan 16 '17 07:01

templefox


People also ask

How do you break a long text in CSS?

The <wbr> element If you know where you want a long string to break, then it is also possible to insert the HTML <wbr> element. This can be useful in cases such as displaying a long URL on a page.

How do you break a span in word?

You can use the CSS property word-wrap:break-word; , which will break words if they are too long for your span width.

How do you wrap text in Flex?

As you only want the text itself to wrap you need to use flex-wrap: nowrap; to keep . right on the same line. The text will automatically wrap when there is not enough space.

Is word-break deprecated?

Note: While word-break: break-word is deprecated, it has the same effect, when specified, as word-break: normal and overflow-wrap: anywhere — regardless of the actual value of the overflow-wrap property.


2 Answers

just put word-break:break-all property to its parent div. You can see it live by clicking this JSFiddle link

like image 167
Shubham Baranwal Avatar answered Nov 14 '22 04:11

Shubham Baranwal


.parent {
   display: flex;
   width: 15rem;
   padding: 10px;
   background: yellowgreen;
   /* overflow: hidden       <-- not necessary at this point */
   /* flex-direction: row    <-- default value; can be omitted */
}
.flex-item {
   /* width: 10em            <-- not necessary at this point */
   /* flex: 1 1 50%          <-- not necessary at this point */
   padding: 10px;
   background: yellow;
   display: flex;            /* new */
   min-width: 0;             /* see note #1 */

}
.block1 {
   width: 10em;
   overflow: hidden;         /* see note #2 */
   text-overflow: ellipsis;  /* see note #2 */
   white-space: nowrap;      /* see note #2 */
   background: red;
}
.block2 {
  background: orange;
}

/* .nos { flex-shrink: 0 !important; } */
<div class="parent">
  <div class="flex-item">
    <div class="block1">longlonglonglonglonglonglonglonglonglonglonglonglonglong</div>
  </div>
  <div class="flex-item">
    <div class="block2">Display</div>
  </div>
</div>
<br/>
<div class="parent">
  <div class="flex-item">
    <div class="block1">longlonglonglonglong...</div>
  </div>
  <div class="flex-item">
    <div class="block2">Display</div>
  </div>
</div>

revised fiddle

Notes:

  1. Why doesn't flex item shrink past content size?
  2. Applying an ellipsis to multiline text
like image 20
Michael Benjamin Avatar answered Nov 14 '22 03:11

Michael Benjamin