文字溢出隐藏,隐藏的问字用省略号表示

本文介绍了如何使用CSS实现单行及多行文本溢出隐藏的效果,并附带示例代码。单行文本可通过text-overflow:ellipsis实现,而多行文本则采用伪元素方法。

  之前在写页面的时候用的都是单行文字溢出隐藏,今天遇到了多行文字溢出隐藏,溢出部分用省略号。我通过查阅一些资料整理了一下,拿出来与大家分享一下。

  单行文本的溢出隐藏

  对于单行文本溢出 隐藏,text-overflow: ellipsis 就能完美的解决,不过在使用他时,一定要结合  overflow: hidden和   white-space: nowrap;,我有时就忘记写了,导致效果出不来,还让我找了半天。

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>单行文本溢出隐藏</title>
 6     <style>
 7         div{
 8             width: 200px;
 9             height: 300px;
10             border: 1px solid cyan;
11             overflow: hidden;/*溢出隐藏*/
12             white-space: nowrap;/*文字不换行*/
13             text-overflow: ellipsis;/*溢出用省略号代替*/
14         }
15     </style>
16 </head>
17 <body>
18 <div>
19     <div>凭借专业的精神、优质的团队、周到的服务,东方卓越现已成为众中国银行业协会、中国航空运输协会、中国工商银行、中国建设银行、凤凰网、联想(北京)、中国人保等知名企业的合作伙伴,服务领域涵盖金融证券、汽车产业、时尚传媒、互联网、地产家居、旅游娱乐等重点领域。</div>
20 
21 </div>
22 </body>
23 </html>

效果:

    

多行文本的溢出隐藏

   我是用伪类标签

 

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>多行文本溢出隐藏</title>
 6     <style>
 7         div{
 8             position: relative;
 9             overflow: hidden;
10             width: 200px;
11             height: 300px;
12             line-height: 30px;
13             font-size: 20px;
14             border: 1px solid cyan;
15         }
16         div:after{
17             position: absolute;
18             bottom: 0;
19             right: 0;
20             padding:0 5px 1px 45px;
21             content: '...';/*结尾内容换为 ... */
22             background:url("http://css88.b0.upaiyun.com/css88/2014/09/ellipsis_bg.png") repeat-y; /*背景是一个半白半透明的图片*/
23         }
24     </style>
25 </head>
26 <body>
27 <div>
28     凭借专业的精神、优质的团队、周到的服务,东方卓越现已成为众中国银行业协会、中国航空运输协会、中国工商银行、中国建设银行、凤凰网、联想(北京)、中国人保等知名企业的合作伙伴,服务领域涵盖金融证券、汽车产业、时尚传媒、互联网、地产家居、旅游娱乐等重点领域。凭借专业的精神、优质的团队、周到的服务,东方卓越现已成为众中国银行业协会、中国航空运输协会、中国工商银行、中国建设银行、凤凰网、联想(北京)、中国人保等知名企业的合作伙伴,服务领域涵盖金融证券、汽车产业、时尚传媒、互联网、地产家居、旅游娱乐等重点领域。
29 </div>
30 </body>
31 </html>

效果:

 

  小伙伴们你们有没有更好地方法呢,快拿出来分享分享吧!

 

转载于:https://www.cnblogs.com/yhyanjin/p/7071489.html

import java.io.*; public class TextFileCopy { public static void main(String[] args) { String sourceFile = "source.txt"; String destinationFile = "destination.txt"; try { // 方法1: 使用节流(InputStream/OutputStream) copyUsingByteStreams(sourceFile, destinationFile + ".method1"); // 方法2: 使用节流带缓冲(BufferedInputStream/BufferedOutputStream) copyUsingBufferedByteStreams(sourceFile, destinationFile + ".method2"); // 方法3: 使用符流(FileReader/FileWriter) copyUsingCharStreams(sourceFile, destinationFile + ".method3"); // 方法4: 使用符流带缓冲(BufferedReader/BufferedWriter) copyUsingBufferedCharStreams(sourceFile, destinationFile + ".method4"); // 方法5: 使用符流带缓冲和行处理(BufferedReader/BufferedWriter) copyUsingBufferedCharStreamsWithLineProcessing(sourceFile, destinationFile + ".method5"); System.out.println("所有文件复制操作已完成!"); } catch (IOException e) { System.err.println("文件操作出错: " + e.getMessage()); e.printStackTrace(); } } // 方法1: 使用基本节流 public static void copyUsingByteStreams(String source, String destination) throws IOException { try (InputStream in = new FileInputStream(source); OutputStream out = new FileOutputStream(destination)) { int byteRead; while ((byteRead = in.read()) != -1) { out.write(byteRead); } } System.out.println("方法1: 使用节流完成文件复制"); } // 方法2: 使用带缓冲的节流 public static void copyUsingBufferedByteStreams(String source, String destination) throws IOException { try (BufferedInputStream bin = new BufferedInputStream(new FileInputStream(source)); BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(destination))) { byte[] buffer = new byte[8192]; int bytesRead; while ((bytesRead = bin.read(buffer)) != -1) { bout.write(buffer, 0, bytesRead); } } System.out.println("方法2: 使用带缓冲的节流完成文件复制"); } // 方法3: 使用基本符流 public static void copyUsingCharStreams(String source, String destination) throws IOException { try (Reader reader = new FileReader(source); Writer writer = new FileWriter(destination)) { int charRead; while ((charRead = reader.read()) != -1) { writer.write(charRead); } } System.out.println("方法3: 使用符流完成文件复制"); } // 方法4: 使用带缓冲的符流 public static void copyUsingBufferedCharStreams(String source, String destination) throws IOException { try (BufferedReader br = new BufferedReader(new FileReader(source)); BufferedWriter bw = new BufferedWriter(new FileWriter(destination))) { char[] buffer = new char[8192]; int charsRead; while ((charsRead = br.read(buffer)) != -1) { bw.write(buffer, 0, charsRead); } } System.out.println("方法4: 使用带缓冲的符流完成文件复制"); } // 方法5: 使用带缓冲的符流逐行处理 public static void copyUsingBufferedCharStreamsWithLineProcessing(String source, String destination) throws IOException { try (BufferedReader br = new BufferedReader(new FileReader(source)); BufferedWriter bw = new BufferedWriter(new FileWriter(destination))) { String line; while ((line = br.readLine()) != null) { // 这里可以对每行内容进行处理 bw.write(line); bw.newLine(); // 添加换行符,因为readLine()会移除换行符 } } System.out.println("方法5: 使用带缓冲的符流逐行处理完成文件复制"); } }
05-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值