第二行 CSS 省略号
CSS text-overflow: ellipsis
在第二行,这可能吗?我在网上找不到它。
示例:
我想要的是这样的:
I hope someone could help me. I need
an ellipsis on the second line of...
但发生的事情是这样的:
I hope someone could help me. I ...
CSS text-overflow: ellipsis
on second line, is this possible? I can't find it on the net.
example:
what I want is like this:
I hope someone could help me. I need
an ellipsis on the second line of...
but what's happening is this:
I hope someone could help me. I ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(22)
Sass helper mixin:
对于那些使用像 Sass 这样的预处理器的人来说,你可以有一个 mixin,它带有一个名为
lines
的可选参数,默认为 1,这使得在元素上应用文本截断变得非常方便,并在需要时更改行数:用法:
Sass helper mixin:
For those who are using a preprocessor like Sass, you could have a mixin with an optional parameter called
lines
which defaults to 1, this makes it very convenient to apply text truncation on an element, and change the number of lines when you need:Usage:
我之前使用过 jQuery-condense-plugin ,看起来它可以做你想做的事情。如果没有,不同的插件可能会满足您的需求。
编辑:为您制作了一个演示 - 请注意,我链接了jquery.condense.js 在演示中它发挥了魔力,所以完全归功于该插件的作者:)
I have used the jQuery-condense-plugin before, which looks like it can do what you want. If not, there are different plugins that might suit your needs.
Edit: Made you a demo - note that I linked the jquery.condense.js on the demo which does the magic, so full credit to the author of that plugin :)
纯CSS方式用省略号修剪多行文本
调整文本容器的高度,
通过 -webkit-line-clamp 中断的控制线:2;
Pure css way of trim multiline text with ellipsis
Adjust text container's hight,
control line to break by -webkit-line-clamp: 2;
这里所有的答案都是错误的,他们缺少重要的部分,高度
All the answers here are wrong, they missing important piece, the height
它是一个非标准的 CSS,当前版本的 CSS 中没有涵盖它(Firefox 不支持它)。尝试使用 JavaScript 代替。
It is a non-standard CSS, which is not covered in current version of CSS (Firefox does not support it). Try to use JavaScript instead.
我以前遇到过这个问题,并且没有纯 CSS 解决方案
,这就是为什么我开发了一个小型库来处理这个问题(以及其他问题)。该库提供了用于建模和执行字母级文本渲染的对象。例如,您可以模拟文本溢出:具有任意限制的省略号(不一定是一行)
阅读更多信息 http://www.samuelrossille.com/home/jstext.html 查看屏幕截图、教程和下载链接。
I've met this issue before, and there is no pure css solution
That's why i have developped a small library to deal with this issue (among others). The library provides objects to modelize and perform letter-level text rendering. You can for example emulate a text-overflow: ellipsis with an arbitrary limit (not necessary one line)
Read more at http://www.samuelrossille.com/home/jstext.html for screenshot, tutorial, and dowload link.
基于 -webkit-line-clamp 的纯 css 方法,适用于 webkit:
a pure css method base on -webkit-line-clamp, which works on webkit:
没有真正简单的方法可以做到这一点。使用 Clamp.js 库。
No real easy way to do this. Use the Clamp.js library.
试试这个
点击网址-------->
[1]: https://i.sstatic.net/QMFQFPnZ.png
Try this
click on url ------->
[1]: https://i.sstatic.net/QMFQFPnZ.png
延迟回复,但 html 换行符也可以,因此您可以执行仅 html + css 的解决方案。因此,通常使用 br 元素是不好的做法,但如果您用 br 来中断注释,文本溢出省略号将从 html 文本的下一行开始。
Late Reply but a html line-break works as well, so you can do a html + css only solution. So it's bad practice to usually use the br element, but if you break your comment with br the text-overflow ellipsis will start on the next line of the html text.
我找到了一个解决方案,但是您需要知道适合您的文本区域的粗略字符长度,然后将 ... 加入到前面的空格中。
我这样做的方法是:
函数与文本一起
长度
代码:
这是一个小提琴 - http://jsfiddle.net/GYsW6/1/
I found a solution however you need to know a rough character length that will fit in to your text area, then join a ... to the preceeding space.
The way I did this is to:
function along with your text
length
code :
here is a fiddle - http://jsfiddle.net/GYsW6/1/
这应该可以在 webkit 浏览器中工作:
浏览器支持
This should work in webkit browsers:
browser support
text-overflow: ellipsis;
工作的要求是white-space
的单行版本 (pre
,nowrap< /代码>等)。这意味着文本永远不会到达第二行。
因此。在纯 CSS 中不可能。
我刚才在寻找完全相同的东西时的来源: http://www.quirksmode.org /css/textoverflow.html(Quirksmode ftw!)
编辑如果CSS大神会实现http://www.w3.org/TR/css-overflow-3/#max-lines 我们可以使用
fragments
(新)和max-lines
(新)。还有一些关于 http://css-tricks.com/line-clampin/< strong>编辑2 WebKit/Blink有
line-clamp
:-webkit-line-clamp: 2
会将省略号放在第二行。A requirement for
text-overflow: ellipsis;
to work is a one-line version ofwhite-space
(pre
,nowrap
etc). Which means the text will never reach the second line.Ergo. Not possible in pure CSS.
My source when I was looking for the exact same thing just now: http://www.quirksmode.org/css/textoverflow.html (Quirksmode ftw!)
EDIT If the good CSS gods will implement http://www.w3.org/TR/css-overflow-3/#max-lines we can haz this in pure CSS using
fragments
(new) andmax-lines
(new). Also some more info on http://css-tricks.com/line-clampin/EDIT 2 WebKit/Blink has
line-clamp
:-webkit-line-clamp: 2
will put ellipsis on 2nd line.只需对支持它的浏览器(大多数现代浏览器)使用 line-clamp 并在较旧的浏览器中回退到 1 行。
浏览器支持
Just use line-clamp for the browsers that support it(most modern browsers) and fall back to 1 line for older.
browser support
我发现 Skeep 的答案还不够,还需要一个
overflow
风格:I found that Skeep's answer was not enough and needed an
overflow
style too:正如其他人已经回答的那样,纯 CSS 解决方案不存在。
有一个 jQuery 插件非常好用,它的名字叫 dotdotdot。
它使用容器的宽度和高度来计算是否需要截断并添加省略号。
这是一个 jsFiddle。
As others have already answered, a pure CSS solution does not exists.
There is a jQuery plugin that is very easy to use, it is called dotdotdot.
It uses the container's width and height to calculate if it needs to truncate and add ellipsis.
Here is a jsFiddle.
我将这些 css 属性用于行省略号 -
这里
-webkit-line-clamp: 3;
是 ... 之后显示的行数(3 行)。I used these css properties for line ellipsis-
here
-webkit-line-clamp: 3;
is the number of line(3 lines) after which ... shows.如果有人试图让 line-clamp 在 Flexbox 中工作,那就有点棘手了 - 特别是当你用很长的单词进行折磨测试时。此代码片段中唯一真正的区别是包含截断文本的 Flex 项目中的
min-width: 0;
和word-wrap: break-word;
。向 https://css-tricks.com/flexbox-truncated-text/< 致敬/a> 以获得洞察力。免责声明:在撰写此答案时,Chrome 之外的浏览器支持很差。然而,从那时起,情况可能有所改善。http://codepen.io/AlgeoMA/pen/JNvJdx(codepen版本)
If anyone is trying to get line-clamp to work in flexbox, it's slightly trickier - especially once you are torture testing with really long words. The only real differences in this code snippet are
min-width: 0;
in the flex item containing truncated text, andword-wrap: break-word;
. A hat-tip to https://css-tricks.com/flexbox-truncated-text/ for the insight. Disclaimer: at the time this answer was written, browser support was poor outside of Chrome. However, things may have improved since then.http://codepen.io/AlgeoMA/pen/JNvJdx (codepen version)
如果有人正在使用 SASS/SCSS 并偶然发现这个问题 - 也许这个 mixin 会有所帮助:
这只会在 webkit 浏览器中添加省略号。休息只会切断它。
if someone is using SASS/SCSS and stumbles upon this question - maybe this mixin could be of help:
this only adds the ellipsis in webkit browsers. rest just cuts it off.
我不是 JS 专业人士,但我想出了几种方法可以做到这一点。
HTML:
然后使用 jQuery 将其截断为特定的字符数,但保留最后一个单词,如下所示:
结果如下所示:
或者,您可以简单地将其截断为特定字符数,如下所示:
结果如下所示:
希望能有所帮助。
这是 jsFiddle。
I'm not a JS pro, but I figured out a couple ways you could do this.
The HTML:
Then with jQuery you truncate it down to a specific character count but leave the last word like this:
The result looks like this:
Or, you can simply truncate it down to a specific character count like this:
The result looks like this:
Hope that helps a bit.
Here is the jsFiddle.
多么遗憾你不能让它在两条线上工作!如果元素是显示块并且高度设置为 2em 或其他值,并且当文本溢出时会显示省略号,那就太棒了!
对于单行,您可以使用:
对于多行,您可以使用 Polyfill,例如 github 上的 jQuery autoellipsis http:// pvdspek.github.com/jquery.autoellipsis/
What a shame that you can't get it to work over two lines! Would be awesome if the element was display block and had a height set to 2em or something, and when the text overflowed it would show an ellipsis!
For a single liner you can use:
For multiple lines maybe you could use a Polyfill such as jQuery autoellipsis which is on github http://pvdspek.github.com/jquery.autoellipsis/
这是一个很好的纯示例 CSS。
here is good example in pure css.