如果CSS或Asp.net中的文本太宽,则剪切文本 [英] Cut of text if too wide in CSS or Asp.net

查看:209
本文介绍了如果CSS或Asp.net中的文本太宽,则剪切文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个固定宽度的div文本:

 有些标题(2009-10-10小)
一些标题(2009-10-10小)
一些标题(2009-10-10大)

但是当标题太宽的时候,结果是:

 
一些宽标题(2009-10-10
large)
一些标题(2009-10-10大)

这不是好看:/我真正想要的是:

 一些标题(2009-10-10小)
一些宽...(2009-10-10大)
一些标题(2009-10-10大)

这是否可以使用Asp.net和CSS?我知道我可以测量字符串宽度在Asp.net,但它(我知道)是不是很精确。

解决方案

你需要的是截断文本,如果它超过一定的视觉长度。 CSS可以用来隐藏超出的字符,但截断必须在代码级别(服务器大小或javascript)完成。



这可能有点棘手if你试图按字符计数,除非你使用固定宽度的字体。或者,您可以尝试更保守的最大字符数。



我相信有更多的创造性的方法,我渴望从其余的听到。 / p>

I have this text inside a div with a fixed width:

Some headline (2009-10-10 small)
Some headline (2009-10-10 small)
Some headline (2009-10-10 large)

But when the headline is too wide the result is:

Some headline (2009-10-10 small)
Some wide headline (2009-10-10
large)
Some headline (2009-10-10 large)

Which is not good looking :/ What I really wants is:

Some headline (2009-10-10 small)
Some wide... (2009-10-10 large)
Some headline (2009-10-10 large)

Is this possible to make with Asp.net and CSS? I know I can measure a string width in Asp.net but it (as I know) is not very precise.

解决方案

What you need is to truncate the text if it exceeds a certain visual length. CSS can be used to hide the exceeding characters, but the truncation has to be done at code level (server-size or javascript).

This can be a little tricky if you are trying to turncate by character count unless you are using fixed-width fonts. Alternatively, you can try a more conservative max character count.

I'm sure there are more creative methods, I'm eager to 'hear' from the rest.

这篇关于如果CSS或Asp.net中的文本太宽,则剪切文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆