如何使用 CSS 将长文本指示为较小的固定列?
如何将长文本放入只有一行文本空间的固定宽度列中?文本需要剪切为固定宽度(比如说100px),我想在末尾添加点“...”。例如这样的事情:
给定字符串:
Some really long string that I need to fit in there!
固定宽度列中所需的输出应该是:
Some really long string...
How do I fit long text into a fixed width column where I have room for only one line of text? The text needs to be cut to a fixed width (lets say to 100px) and I would like to add dots "..." at the end. Something like this for example:
Given string:
Some really long string that I need to fit in there!
Desired output in fixed-width-column should be:
Some really long string...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以单独使用 CSS 来完成此操作:
注意:您需要检查最新的浏览器支持。
You can do this with CSS alone:
Note: You will want to check on the latest browser support.
只是用一些CSS,比如戈登的回答。刚刚为
或
等内联元素添加了
display:block
属性:您可以在许多浏览器中使用它就像你可以在这个链接上看到的:
http://caniuse.com/#search=text-overflow
Just with some CSS like answer of Gordon. Just added the
display:block
property for an inline element like<a>
or<p>
:You could use it with many browser like you can see on this link :
http://caniuse.com/#search=text-overflow
我有一个类似的问题,我解决它的方法是将字符串精简到 60 个字符,并在其末尾附加一个“...”。
一个丑陋的解决方案?是的,但除非有 jQuery 解决方案,否则它可能是您最好的选择。
如果您使用 Smarty,这就是我解决问题的方法:
I had a similar problem, the way I solved it was to strip the string down to 60 characters and append an '...' to the end of it.
An ugly solution? Yes, but unless there is a jQuery solution it's probably your best bet.
If you're using Smarty, this is how I solved the problem:
这是我用来截断字符串的函数。与这里的大多数建议一样,它使用 substr 来截断字符串,但它会避免在单词中间分割字符串:
Here's a function I use to truncate strings. Like most of the suggestions here, it uses substr to truncate the string, but it will avoid splitting the string mid-word:
我认为在 N 个字符之后简单地剪切文本并不是您想要的。这不是一个解决方案,因为以下文本都有 15 个字符长度:iiiiiiiiiiiiiii, mmmmmmmmmmmmmm - 请注意,第二个“单词”大约是第一个“单词”的三倍长。
JavaScript 可能是一个解决方案:
首先准备您的标记:
其中
{TEXT}
是截断为 150 个字符的文本 +...
现在,当我们有了良好的 JavaScript 基础时,我们可以尝试制作您正在寻找的内容:
I think simple cutting text after N characters isn't what you're looking for. It's not a solution because the following text have both 15 character length: iiiiiiiiiiiiiii, mmmmmmmmmmmmmmm - notice that the second "word" is about three times longer than the first one.
JavaScript might be a solution:
First prepare your mark-up:
Where
{TEXT}
is your text truncated to 150 characters +...
Now when we've got a good base for JavaScript we can try to make what you're looking for: