constrain inner div with text-overflow: ellipsis (or similar) without max-width

huangapple go评论54阅读模式
英文:

constrain inner div with text-overflow: ellipsis (or similar) without max-width

问题

我有一个使用flexbox的行,其中包含多个单元格,最后一个单元格(比如说D4)的宽度会变化(flex-grow: 1)。这个div里面包含另一个包含实际文本的div。现在,我的问题是文本超出了父div(D4)的宽度。我尝试添加text-overflow: ellipsis;overflow: hidden,但没有固定的width,它自然不起作用。请问有什么最好的方法来控制这个内部div

这是我尝试做的一个小示意图 - 在这个片段中,我将父div .menu 限制在500px,但这只是为了演示,我的实际div是页面的宽度。当文本超过父宽度时,它会超出页面并在页面上添加滚动条,这是我想避免的情况。

请问我该如何实现这一点?

/* 从scss转换而来 */
.menu {
  display: flex;
  border: 1px solid red;
  width: 500px;
}

.menu__item {
  border: 1px solid black;
  padding: 5px;
}

.menu__item--description {
  flex-grow: 1;
}

.menu__item--description div {
  display: flex;
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
  max-width: 100%; /* 不起作用 */
}
<div class="menu">
  <div class="menu__item">One</div>
  <div class="menu__item">Two</div>
  <div class="menu__item">Three</div>
  <div class="menu__item menu__item--description">
    <div>
      但我必须向您解释,所有这些关于谴责快乐并赞美痛苦的错误想法是如何产生的,我将为您提供一个完整的系统说明。
    </div>
  </div>
</div>
英文:

I have a flexbox row with multiple cells and the last cell (D4, lets say) will be of variying width (flex-grow: 1). This div will contain inside it, another div that has the actual text. Now, my problem is that the text is going far beyond the width of the parent div (D4). I tried adding text-overflow: ellipsis; and overflow: hidden, but without a fixed width, it naturally isn't working. What's the best way to tame this inner div please?

Here's a little mockup of what I'm trying to do - and in this snippet, I've constrained the parent div .menu to 500px, but, that's just for demonstration and my actual div is the width of the page. When the text is longer than the parent width, it extends beyond the page and adds scrollbars to the page, which is what I'm trying to avoid.

How can I achieve this please?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

/* converted from scss */
.menu {
  display: flex;
  border: 1px solid red;
  width: 500px;
}

.menu__item {
  border: 1px solid black;
  padding: 5px;
}

.menu__item--description {
  flex-grow: 1;
}

.menu__item--description div {
  display: flex;
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
  max-width: 100%; /* doesn&#39;t work */
}

<!-- language: lang-html -->

&lt;div class=&quot;menu&quot;&gt;
  &lt;div class=&quot;menu__item&quot;&gt;One&lt;/div&gt;
  &lt;div class=&quot;menu__item&quot;&gt;Two&lt;/div&gt;
  &lt;div class=&quot;menu__item&quot;&gt;Three&lt;/div&gt;
  &lt;div class=&quot;menu__item menu__item--description&quot;&gt;
    &lt;div&gt;
      But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 1

你可以将 .menu__item--description 添加 overflow: hidden; 并移除 .menu__item--description div 上的 display: flex;

/* converted from scss */
.menu {
  display: flex;
  border: 1px solid red;
  width: 500px;
}

.menu__item {
  border: 1px solid black;
  padding: 5px;
}

.menu__item--description {
  flex-grow: 1;
  overflow: hidden;
}

.menu__item--description div {
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
  max-width: 100%;
}
<div class="menu">
  <div class="menu__item">One</div>
  <div class="menu__item">Two</div>
  <div class="menu__item">Three</div>
  <div class="menu__item menu__item--description">
    <div id="tekstdiv">
      但我必须向你解释,这个错误的快乐批评和赞扬痛苦的观念是如何产生的,我将为你提供系统的完整说明
    </div>
  </div>
</div>
英文:

You can add overflow: hidden; to .menu__item--description and remove display: flex; on the .menu__item--description div.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

/* converted from scss */
.menu {
  display: flex;
  border: 1px solid red;
  width: 500px;
}

.menu__item {
  border: 1px solid black;
  padding: 5px;
}

.menu__item--description {
  flex-grow: 1;
  overflow: hidden;
}

.menu__item--description div {
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
  max-width: 100%;
}

<!-- language: lang-html -->

&lt;div class=&quot;menu&quot;&gt;
  &lt;div class=&quot;menu__item&quot;&gt;One&lt;/div&gt;
  &lt;div class=&quot;menu__item&quot;&gt;Two&lt;/div&gt;
  &lt;div class=&quot;menu__item&quot;&gt;Three&lt;/div&gt;
  &lt;div class=&quot;menu__item menu__item--description&quot;&gt;
    &lt;div id=&quot;tekstdiv&quot;&gt;
      But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 1

我只进行了少量更改。添加了overflow: hidden;并删除了display: flex;

<!-- 开始代码段: js 隐藏: false 控制台: true Babel: false -->

<!-- 语言: lang-css -->
/* 从 SCSS 转换而来 */
.menu {
  display: flex;
  border: 1px solid red;
}

.menu__item {
  border: 1px solid black;
  padding: 5px;
}

.menu__item--description {
  flex-grow: 1;
  overflow: hidden;
}

.menu__item--description div {
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
}

<!-- 语言: lang-html -->
<div class="menu">
  <div class="menu__item">One</div>
  <div class="menu__item">Two</div>
  <div class="menu__item">Three</div>
  <div class="menu__item menu__item--description">
    <div>
      但我必须向您解释,所有这些错误的观念都是如何产生的,谴责快乐,赞美痛苦,我将为您提供系统的完整说明
    </div>
  </div>
</div>

<!-- 结束代码段 -->
英文:

I have only changed a little. Added an overflow: hidden; and removed a display: flex;:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

/* converted from scss */
.menu {
  display: flex;
  border: 1px solid red;
}

.menu__item {
  border: 1px solid black;
  padding: 5px;
}

.menu__item--description {
  flex-grow: 1;
  overflow: hidden;
}

.menu__item--description div {
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
}

<!-- language: lang-html -->

&lt;div class=&quot;menu&quot;&gt;
  &lt;div class=&quot;menu__item&quot;&gt;One&lt;/div&gt;
  &lt;div class=&quot;menu__item&quot;&gt;Two&lt;/div&gt;
  &lt;div class=&quot;menu__item&quot;&gt;Three&lt;/div&gt;
  &lt;div class=&quot;menu__item menu__item--description&quot;&gt;
    &lt;div&gt;
      But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月17日 17:10:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76702972.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定