If the multi-line or single-line text exceeds the display point of two lines, how to ensure that the content is always centered vertically?

what I need now is this. I currently implement a div box that displays text beyond two lines. If a single line is to be vertically centered, if I use display:flex;align-items:center; for the container, the text will not be truncated when there is too much text.

clipboard.png

.info_des {
      .margin-all(10, 10, 12, 8);
      display: flex;
      align-items:center;
      div{
        .line-height(21);
        .height(42);
        .font-size(14);
        color: -sharp000;
        letter-spacing: 0.01px;
      }
    }
    
<div class="info_des">
        <div class="line_clamp2">is the leading online shopping platform in is the leading online shopping platform inis the leading online shopping platform in</div>
    </div>
Dec.20,2021

.info_des {
      .margin-all(10, 10, 12, 8);
      display: flex;
      align-items:center;
      .height(42);
      div{
        .line-height(21);
        .font-size(14);
        color: -sharp000;
        letter-spacing: 0.01px;
      }
    }

suddenly figured out the solution. The vertical center of the display:flex; is the center of the elements inside. Give the outer div a fixed height here is the height of two lines of text. Do not give the height of the text inside. When there is a line, the height of the div inside is the height of the line. When there is two lines of text, two lines of text will be displayed, and the effect beyond the display point will not be affected.


display:box + line-clamp seems to meet your requirements

Menu