使pre的内容自动换行
思韵闪耀
2018-01-28
0

<pre> 元素可定义预格式化的文本。被包围在 pre 元素中的文本通常会保留空格和换行符。而文本也会呈现为等宽字体。

<pre> 标签的一个常见应用就是用来表示计算机的源代码。

而我们经常碰到的一个问题是如果一个代码上碰到有图片或者网页地址就会使代码很长,结果会造成页面撑开或者代码超出边界。非常难受,如果用overflow:hidden那么会将原来的代码隐藏掉,用overflow:auto则会出现滚动条,代码也不方便阅读。

演示页面代码:

<!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="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
pre {
background-color:#CCC
}
</style>
</head>

<body>
<pre style="width:500px; overflow:hidden">此处显示新此处显示新此处显示新此处显示新此处显示新此处显示新此处显示新此处显示新此处显示新此处显示新此处显示新此处显示新此处显示新此处显示此处显示新此处显示新此处显示新此处显示新此处显示新此处显示新此处显示新此处显示新此处显示新此处显示新此处显示新此处显示新此处显示新此处显示
新 Div preprepr
epreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepre</pre>
</body>
</html>

今天折腾了一个晚上终于搞定<pre>的内容自动换行的问题:

1.先尝试使用:word-wrap: break-word;将内容自动换行,IE,OP,Chrome,Safari都可以,FF就悲剧了。

演示页面代码:

<!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="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
pre {
word-wrap: break-word;
}
</style>
</head>

<body>
<pre style="width:500px;">此处显示新此处显示新此处显示新此处显示新此处显示新此处显示新此处显示新此处显示新此处显示新此处显示新此处显示新此处显示新此处显示新此处显示此处显示新此处显示新此处显示新此处显示新此处显示新此处显示新此处显示新此处显示新此处显示新此处显示新此处显示新此处显示新此处显示新此处显示
新 Div preprepr
epreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepreprepre</pre>
</body>
</html>

2.查看了pre的浏览器默认样式:

xmp, pre, plaintext {
  display: block;
  font-family: -moz-fixed;
  white-space: pre;
  margin: 1em 0;
}
都有这个white-space: pre,看看white-space的值:
描述
normal 默认。空白会被浏览器忽略。
pre 空白会被浏览器保留。其行为方式类似 HTML 中的 <pre> 标签。
nowrap 文本不会换行,文本会在在同一行上继续,直到遇到 <br> 标签为止。
pre-wrap 保留空白符序列,但是正常地进行换行。
pre-line 合并空白符序列,但是保留换行符。
inherit 规定应该从父元素继承 white-space 属性的值。

有个pre-wrap,保留空白符序列,但是正常地进行换行。

这样就OK了搞定,我们只要加上样式:

	pre {
	white-space: pre-wrap;
	word-wrap: break-word;
	}

就能使<pre>的内容自动换行了。

【版权声明】
本站部分内容来源于互联网,本站不拥有所有权,不承担相关法律责任。如果发现本站有侵权的内容,欢迎发送邮件至masing@13sy.com 举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。

相关内容

热门资讯

Invalid left-ha... 写一个提交函数的时候,发现函数不生效,F12之后看到Invalid left-hand side i...
响应图片(Responsive... 一、开门见山 响应图片技术可以说是响应布局衍生出来的一个小分支。说白了,就是不同显示器宽度下调用不同...
轻松删除MetInfo CMS... 第一步:首页底部版权,模板文件foot直接删除; 第二部:首页头部generator版权,\publ...
ckplayer6.3如何替换... 1.刚打开播放器,视频在缓冲时播放器中间的LOGO怎么替换?(如下图) 2.视频播放器右上角的CK ...
inDensity,inTar... 如果接触Android中的Bitmap较深的话,肯定会知道标题上的这三个属性值,那么这三个属性值的关...
JS 验证 利用Javascript中每个对象(Object)的prototype属性我们可以为Javascri...
在文本框中输入时保证总是在最... 那就要在keypress时保证文本框的光标在尾部,而文本框value赋值后其光标默认在最后,故可采用...
微信UA Mozilla/5.0 (Linux; U; Android 2.3.6; zh-cn; GT-S5...
检测大写锁定键(Caps Lo... h1检测大写锁定键/h1form action="#" method="post" p label ...
iframe高度自适应,全屏,... !注意此方法用于同一个域名下,可能需要部署在服务器上才能看出效果 !DOCTYPE html PUB...