How to truncate string inside a direction column flexbox child

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-29 09:37:08

问题


I went over the article from csstricks, https://css-tricks.com/flexbox-truncated-text/

My question how to make it work when the .flex-parent has flex-direction: column; property.

Thanks for help!


回答1:


This really is just a matter of rearranging some properties, but naturally this answer depends on the projects needs.

Takeaways here are, if you want it to be a column, by nature every top-level element you include in the .flex-parent, when it's set to column direction, will display as such. Therefore, there will be nothing to the right of it to truncate with.

So essentially, if you want top-level elements to be a column AND truncate content inside of it, then one way of accomplishing it is by including both of them in the same 'row' or .flex-child. Then the row is where you could identify the justify and aligning properties.

.flex-parent {
  display: flex;
  flex-direction: column;
  padding: 10px;
  margin: 30px 0;
}
.flex-child {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.long-and-truncated-with-child-corrected {
  flex: 1;
  min-width: 0; /* or some value */
}
.long-and-truncated-with-child-corrected h2 {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.short-and-fixed > div {
    width: 30px;
    height: 30px;
    border-radius: 10px;
    background: lightgreen;
    display: inline-flex;
}

/*nonessentials*/
body {
  padding: 40px;
}
h2 {
  font-size: 1rem;
  font-weight: normal;
}
.flex-parent {
  background: rgba(0,0,0,0.1);
}
<div class="flex-parent">

  <div class="flex-child long-and-truncated-with-child-corrected">
    <h2>This is a <em>long string</em> that is OK to truncate please and thank you. Some more text to demonstrate, if the string is <b>REALLY</b> truncated</h2>
    <div class="flex-child short-and-fixed">
      <div></div>
      <div></div>
      <div></div>
    </div>
  </div>

</div>


来源:https://stackoverflow.com/questions/50514597/how-to-truncate-string-inside-a-direction-column-flexbox-child

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!