CSS - text-overflow: ellipsis with nested display: flex elements

3 points
Created by:

In this article, we're going to have a look at how to in a simple way in pure CSS cut overflowing text for multiple nested elements with flex-box.

Quick solution:

// ONLINE-RUNNER:browser;


    .parent-fix {           /*  with display: flex; styled element   */
    	min-width: 0;
    .text-shortcut {        /*  with display: block; styled element  */
      	text-overflow: ellipsis;
        white-space: nowrap;
        overflow: hidden;


<div style="width: 100px; display: flex">
  <div class="parent-fix" style="display: flex">
    <div class="text-shortcut">
      Very long text here... Very long text here...

More complicated practical example

Verry common problem when we try to make CSS shortcut for text is: when we try to combine flex-box layout (display: flex;) with text-overflow: ellipsis;.

Solution for this problem is to use min-width: 0; for parent element that we want to shortcut text.

Note: when div.shortcut element will be in flex-box mode (display: flex;), text-overflow: ellipsis; will be not working.

Conclusion: use ellipsis always with block element, wrapping text with aditional element when it is necessary.

// ONLINE-RUNNER:browser;

<!doctype html>

    div { padding: 5px; border: 1px solid red; }
    div.h-container { display: flex; }
    div.v-container { display: flex; flex-direction: column; }
    div.item { flex: 1; }
    div.fix {
    	min-width: 0; /* <--------------- required */
    div.shortcut  {
      	text-overflow: ellipsis; /* <---- required */
        white-space: nowrap; /* <-------- required */
        overflow: hidden; /* <----------- required */

  <div class="h-container">
    <!-                          |fix here (for flex element)                   ->
    <!-                          v                                              ->
    <div class="item v-container fix">
      <div class="item shortcut">  <!- ---------- shortcut here (block element) ->
        Very long text here, Very long text here, 
        Very long text here, Very long text here
      <div class="item">
        Nothing here
    <div class="item">
      Nothing here


Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.