css实现文字过长显示省略号的方法

 因为网页排版的需要,有些地方需要过长的问题加上省略号。比如:标题限制20个中文的宽度,超出的就用省略号代替。之前会使用程序截取的方法,不过使用css来截取更有利于SEO。

下面就介绍一下具体的使用方法:

<div class="title">当对象内文本溢出时显示省略标记</div>

这是一个例子,其实我们只需要显示如下长度:

css实现网页中文字过长截取...

title class应该这样写:

.title{ width:300px; white-space:nowrap; overflow:hidden; text-overflow:ellipsis;}

说明:

1、宽度一定要设置,可以根据实际需求调整。

2、white-space:nowrap是禁止文字折行。

3、text-overflow表示当文本溢出时是否显示省略标记,有两个值:

clip:不显示省略标记(...),而是简单的裁切。
ellipsis:当对象内文本溢出时显示省略标记(...)

4、overflow:hidden表示溢出内容为隐藏。

示例效果图:

#FormatImgID_0#

示例代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>text-overflow</title>
</head>
<body>
<style type="text/css">
  .test_demo_clip{text-overflow:clip; overflow:hidden; white-space:nowrap; width:200px; background:#ccc;}
  .test_demo_ellipsis{text-overflow:ellipsis; overflow:hidden; white-space:nowrap; width:200px; background:#ccc;}
</style>
<h2>text-overflow : clip </h2>
  <div class="test_demo_clip">
  不显示省略标记,而是简单的裁切条
</div>
<h2>text-overflow : ellipsis </h2>
<div class="test_demo_ellipsis">
  当对象内文本溢出时显示省略标记
</div>
</body>
</html>

时间: 2024-08-01 08:48:14

css实现文字过长显示省略号的方法的相关文章

CSS控制文字超出部分显示省略号方法

测试浏览器: IE6/7/8/9.opera12.02.firefox15.0.1.chrome  代码如下 复制代码 <p style="width:200px;overflow: hidden;white-space: nowrap;text-overflow: ellipsis;">省略我吧 省略我吧! 省略我吧!省略我! </p> 如果我们要给p标签定义text-overflow:ellipsis就可以这么写:  代码如下 复制代码 p {      

文字过长用省略号代替纯css代码

 文字过长用省略号代替纯css代码 代码如下: <table width="550" border="0" cellpadding="5" cellspacing="1" bgcolor="#007373" style="table-layout:fixed"> <tr> <th nowrap bgcolor="#159AA2" styl

Android设置TextView显示指定个数字符,超过部分显示...(省略号)的方法_Android

本文实例讲述了Android设置TextView显示指定个数字符,超过部分显示...(省略号)的方法.分享给大家供大家参考,具体如下: 一.问题: 今天在公司遇到一个需求:TextView设置最多显示8个字符,超过部分显示...(省略号) 二.解决方法: 网上找了很多资料,有人说分别设置TextView的android:signature="true",并且设置android:ellipsize="end";但是我试了,并没有成功,最后自己试出一种方式如下:供大家参

CSS控制文字溢出时的自动隐藏方法

 代码如下 复制代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv=&qu

行文本溢出显示省略号(...)的方法

个人使用的方法 .ellipsis1 { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; } .ellipsis2 { text-overflow: -o-ellipsis-lastline; overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: verti

css 溢出文本显示省略号各种方法总结

语法: text-overflow : clip | ellipsis 参数: clip : 不显示省略标记(...),而是简单的裁切 (clip这个参数是不常用的!) ellipsis : 当对象内文本溢出时显示省略标记(...) 说明: 设置或检索是否使用一个省略标记(...)标示对象内文本的溢出. 请您注意,text-overflow:ellipsis属性在FF中是没有效果的. 示例: div { text-overflow : clip; } text-overflow是一个比较特殊的样

处理jsp显示文字过长问题的解决方法_JSP编程

复制代码 代码如下: <tr>    <td height="60px;" width="20%" align="right" valign="top">      <font style="font-weight: bold;">标题:</font>     </td>    <td height="60px;" wid

css中文字图片背景图居中的方法汇总

一.让网页所有DIV标签内文字居中   -   实现所有DIV内字体居中实例完整网页源代码: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta charset="utf-8" /> <title>div字体居中实例</title> <style> div{text-align:center

用css来控制图片大小显示的实现方法与代码_经验交流

有时候图片太大,会破环网页整齐的布局.这时可以用css来强制按比例压缩图片的高度或宽度 css代码如下: img,a img{ border:0;  margin:0;  padding:0; max-width:590px; width:expression(this.width>590?"590px":this.width); max-height:590px; height:expression(this.height>590?"590px":th