pre: 滚动条(overflow)与自动换行(word wrap)

作者: lesca 分类: Tutorials,Web 发布时间: 2012-06-04 10:42

今天lesca介绍下如何实现HTML标签pre的自动换行与自动滚动条。这些特性都可以使用CSS实现。我们先介绍自动滚动条:

<pre style="overflow:auto">

预览一下:

This is "pre" tag test for browers... This is "pre" tag test for browers... This is "pre" tag test for browers...
This is "pre" tag test for browers... This is "pre" tag test for browers... This is "pre" tag test for browers...
This is "pre" tag test for browers... This is "pre" tag test for browers... This is "pre" tag test for browers...
This is "pre" tag test for browers... This is "pre" tag test for browers... This is "pre" tag test for browers...
This is "pre" tag test for browers... This is "pre" tag test for browers... This is "pre" tag test for browers...
This is "pre" tag test for browers... This is "pre" tag test for browers... This is "pre" tag test for browers...
This is "pre" tag test for browers... This is "pre" tag test for browers... This is "pre" tag test for browers...
This is "pre" tag test for browers... This is "pre" tag test for browers... This is "pre" tag test for browers...
This is "pre" tag test for browers... This is "pre" tag test for browers... This is "pre" tag test for browers...
This is "pre" tag test for browers... This is "pre" tag test for browers... This is "pre" tag test for browers...
This is "pre" tag test for browers... This is "pre" tag test for browers... This is "pre" tag test for browers...
This is "pre" tag test for browers... This is "pre" tag test for browers... This is "pre" tag test for browers...
This is "pre" tag test for browers... This is "pre" tag test for browers... This is "pre" tag test for browers...
This is "pre" tag test for browers... This is "pre" tag test for browers... This is "pre" tag test for browers...
This is "pre" tag test for browers... This is "pre" tag test for browers... This is "pre" tag test for browers...
This is "pre" tag test for browers... This is "pre" tag test for browers... This is "pre" tag test for browers...

这样,浏览器就能在需要时自动添加滚动条了。如果要实现自动换行,而不要滚动条,只需要这样设定:

<pre style="overflow:hidden;word-wrap:break-word;">

那么上面的内容就会变成这样:

This is "pre" tag test for browers... This is "pre" tag test for browers... This is "pre" tag test for browers...
This is "pre" tag test for browers... This is "pre" tag test for browers... This is "pre" tag test for browers...
This is "pre" tag test for browers... This is "pre" tag test for browers... This is "pre" tag test for browers...
This is "pre" tag test for browers... This is "pre" tag test for browers... This is "pre" tag test for browers...
This is "pre" tag test for browers... This is "pre" tag test for browers... This is "pre" tag test for browers...
This is "pre" tag test for browers... This is "pre" tag test for browers... This is "pre" tag test for browers...
This is "pre" tag test for browers... This is "pre" tag test for browers... This is "pre" tag test for browers...
This is "pre" tag test for browers... This is "pre" tag test for browers... This is "pre" tag test for browers...
This is "pre" tag test for browers... This is "pre" tag test for browers... This is "pre" tag test for browers...
This is "pre" tag test for browers... This is "pre" tag test for browers... This is "pre" tag test for browers...
This is "pre" tag test for browers... This is "pre" tag test for browers... This is "pre" tag test for browers...
This is "pre" tag test for browers... This is "pre" tag test for browers... This is "pre" tag test for browers...
This is "pre" tag test for browers... This is "pre" tag test for browers... This is "pre" tag test for browers...
This is "pre" tag test for browers... This is "pre" tag test for browers... This is "pre" tag test for browers...
This is "pre" tag test for browers... This is "pre" tag test for browers... This is "pre" tag test for browers...
This is "pre" tag test for browers... This is "pre" tag test for browers... This is "pre" tag test for browers...

版权声明

本文出自 Lesca 技术宅,转载时请注明出处及相应链接。

本文永久链接: https://lesca.me/archives/pre-overflow-and-word-wrap.html

如果觉得我的文章对您有用,请随意赞赏。您的支持将鼓励我继续创作!