当前位置: 首页 > 知识库问答 >
问题:

对多行文字应用省略号[重复]

虞裕
2023-03-14

有没有办法在流体高度为20%的div中的最后一行添加省略号?

我在CSS中找到了-webkit line clamp函数,但在我的例子中,行号将取决于窗口大小。

p {
    width:100%;
    height:20%;
    background:red;
    position:absolute;
}
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla sed dui felis. Vivamus vitae pharetra nisl, eget fringilla elit. Ut nec est sapien. Aliquam dignissim velit sed nunc imperdiet cursus. Proin arcu diam, tempus ac vehicula a, dictum quis nibh. Maecenas vitae quam ac mi venenatis vulputate. Suspendisse fermentum suscipit eros, ac ultricies leo sagittis quis. Nunc sollicitudin lorem eget eros eleifend facilisis. Quisque bibendum sem at bibendum suscipit. Nam id tellus mi. Mauris vestibulum, eros ac ultrices lacinia, justo est faucibus ipsum, sed sollicitudin sapien odio sed est. In massa ipsum, bibendum quis lorem et, volutpat ultricies nisi. Maecenas scelerisque sodales ipsum a hendreritLorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla sed dui felis. Vivamus vitae pharetra nisl, eget fringilla elit. Ut nec est sapien. Aliquam dignissim velit sed nunc imperdiet cursus. Proin arcu diam, tempus ac vehicula a, dictum quis nibh. Maecenas vitae quam ac mi venenatis vulputate. Suspendisse fermentum suscipit eros, ac ultricies leo sagittis quis. Nunc sollicitudin lorem eget eros eleifend facilisis. Quisque bibendum sem at bibendum suscipit. Nam id tellus mi. Mauris vestibulum, eros ac ultrices lacinia, justo est faucibus ipsum, sed sollicitudin sapien odio sed est. In massa ipsum, bibendum quis lorem et, volutpat ultricies nisi. Maecenas scelerisque sodales ipsum a hendrerit.</p>

我用这个例子来说明这个问题。https://jsfiddle.net/96knodm6/

共有3个答案

山森
2023-03-14

请检查此css中是否有多行文字的省略号

body {
  margin: 0;
  padding: 50px;
}

/* mixin for multiline */
.block-with-text {
  overflow: hidden;
  position: relative;
  line-height: 1.2em;
  max-height: 6em;
  text-align: justify;
  margin-right: -1em;
  padding-right: 1em;
}
.block-with-text:before {
  content: '...';
  position: absolute;
  right: 0;
  bottom: 0;
}
.block-with-text:after {
  content: '';
  position: absolute;
  right: 0;
  width: 1em;
  height: 1em;
  margin-top: 0.2em;
  background: white;
}
<p class="block-with-text">The Hitch Hiker's Guide to the Galaxy has a few things to say on the subject of towels. A towel, it says, is about the most massivelyuseful thing an interstellar hitch hiker can have. Partly it has great practical value - you can wrap it around you for warmth as you bound across the cold moons of  Jaglan Beta; you can lie on it on the brilliant marble-sanded beaches of Santraginus V, inhaling the heady sea vapours; you can sleep under it beneath the stars which shine so redly on the desert world of Kakrafoon;  use it to sail a mini raft down the slow heavy river Moth; wet it for use in hand-to-hand-combat; wrap it round your head to ward off noxious fumes or to avoid the gaze of the Ravenous Bugblatter Beast of Traal (a mindboggingly stupid animal, it assumes that if you can't see it, it can't see you - daft as a bush, but very ravenous); you can wave your towel in emergencies as a distress signal, and of course dry yourself off with it if it still seems to be clean enough. More importantly, a towel has immense psychological value. For some reason, if a strag (strag: non-hitch hiker) discovers that a hitch hiker has his towel with him, he will automatically assume that he is also in possession of a toothbrush, face flannel, soap, tin of biscuits, flask, compass, map, ball of string, gnat spray, wet weather gear, space suit etc., etc. Furthermore, the strag will then happily lend the hitch hiker any of these or a dozen other items that the hitch hiker might accidentally have "lost". What the strag will think is that any man who can hitch the length and breadth of the galaxy, rough it, slum it, struggle against terrible odds, win through, and still knows where his towel is is clearly a man to be reckoned with.</p>
颜志学
2023-03-14

如果要应用省略号(…)对于一行文本,CSS使用text overflow属性使其变得简单。这仍然有点棘手(由于所有要求–请参见下文),但文本溢出使其成为可能且可靠。

然而,如果你想在多行文字上使用省略号——就像这里的情况一样——那就别指望有什么乐趣了。CSS没有标准的方法来实现这一点,而且解决方法是随意的。

使用文本溢出,省略号可以应用于单行文本。必须满足以下CSS要求:

  • 必须具有宽度最大宽度弹性基础

因此,这将起作用:

p {
  width: 200px;
  white-space: nowrap;
  overflow: hidden;
  display: inline-block;
  text-overflow: ellipsis;
  border: 1px solid #ddd;
  margin: 0;
}
html prettyprint-override"><p>
  This is a test of CSS <i>text-overflow: ellipsis</i>. 
  This is a test of CSS <i>text-overflow: ellipsis</i>. 
  This is a test of CSS <i>text-overflow: ellipsis</i>. 
  This is a test of CSS <i>text-overflow: ellipsis</i>.
  This is a test of CSS <i>text-overflow: ellipsis</i>.
  This is a test of CSS <i>text-overflow: ellipsis</i>.
</p>
屠华辉
2023-03-14

只需增加-webkit线夹:4;增加行数

p {
    display: -webkit-box;
    max-width: 200px;
    -webkit-line-clamp: 4;
    -webkit-box-orient: vertical;
    overflow: hidden;
}
<p>Lorem ipsum dolor sit amet, novum menandri adversarium ad vim, ad his persius nostrud conclusionemque. Ne qui atomorum pericula honestatis. Te usu quaeque detracto, idque nulla pro ne, ponderum invidunt eu duo. Vel velit tincidunt in, nulla bonorum id eam, vix ad fastidii consequat definitionem.</p>
 类似资料:
  • 问题内容: 是否有解决方案,以div的最后一行在流体高度(20%)处添加省略号? 我在CSS中找到了该函数,但在我的情况下,行号取决于窗口大小。 问题答案: 如果要将省略号(…)应用于一行文本,CSS可以通过该属性使此操作变得容易。仍然有些棘手(由于所有要求,请参阅下文),但使之成为可能且可靠。 但是,如果要在多行文本上使用省略号(如此处的情况),那么不要指望有什么乐趣。CSS没有执行此操作的标准

  • 问题内容: 我知道您可以使用CSS规则组合来在溢出时(不超出父级的范围)使文本以省略号(…)结尾。 是否有可能(随意说,不)达到相同的效果,但让文本换行超过一行? 如您所见,当文本的宽度大于div的宽度时,文本以省略号结尾。但是,仍然有足够的空间用于文本换行并继续。这会被中断,以使省略号起作用。 有任何想法吗? PS:没有JS解决方案,如果可能的话,使用纯CSS。 问题答案: 我不确定您是否看过

  • 我一直在使用空格:不换行,文本溢出:省略号和溢出:隐藏的CSS属性为多行文本创建省略号截断。但是,这在使用弹性框时不起作用。 使用flex时,text-overflow:省略号似乎总是将flex项的高度截断为一行。 对于多行文本,是否可以使用flex和css省略号截断的组合? 我可以让它在单行截断的情况下工作。手动设置高度与flex空格:nowrapp不起作用。

  • 问题内容: 这是我尝试过的: 本质上,当窗口变小时,我希望跨度以省略号缩小。我做错什么了? 问题答案: 您需要具有CSS ,(或)和。

  • 我无法让工作,我希望有人能发现我找不到的错误。这是我的代码: HTML: CSS: JSFIDLE(JSIDLE):http://jsfiddle.net/59m5e6ex/ 我找到了这个,但据我所知,我的满足了所有要求。

  • 我试图把省略号文本溢出: 家长: 儿童: 而且,是,但没有任何省略号。