word-break、word-wrap、white-space区别
<div id="box">
Hi ,
This is a incomprehensibilities long word.
</br>
你好 ,
这 是一个不可思议的长单词
</div>
现在只给了它一个宽度和边框,没有其它任何样式,下面是它目前的展现情况:
nbsp;
和</br>
可以正常发挥作用,而连续的空格会被缩减成一个(比如This和is之间的三个空格变成了一个),换行符也全都无效。句子超过一行后会自动换行,而长度超过一行的单个单词会超出边界。接下来我们看下, 给它上面三个css属性赋值后会出现什么变化。
white-space
正如它的名字,这个属性是用来控制空白字符的显示的,同时还能控制是否自动换行。它有五个值:normal | nowrap | pre | pre-wrap | pre-line
。因为默认是normal
,所以我们主要研究下其它四种值时的展现情况。
white-space:normal 初始默认样式

先看下white-space:nowrap
时的情况:
不仅空格被合并,换行符无效,连原本的自动换行都没了!只有</br>
才能导致换行!所以这个值的表现还是挺简单的,我们可以理解为永不换行
。
white-space:pre
空格和换行符全都被保留了下来!不过自动换行还是没了。保留,所以pre
其实是preserve
的缩写,这样就好记了。
white-space:pre-wrap 显然pre-wrap
就是preserve
+wrap
,保留空格和换行符,且可以自动换行。
white-space:pre-line 空格被合并了,但是换行符可以发挥作用,line
应该是new line
的意思,自动换行还在,所以pre-line
其实是preserve
new line
+wrap
。
word-break
从这个名字可以知道,这个属性是控制单词如何被拆分换行的。它有三个值:normal | break-all | keep-all
。
word-break:keep-all
:
所有“单词”一律不拆分换行,注意,我这里的“单词”包括连续的中文字符(还有日文、韩文等),或者可以理解为只有空格可以触发自动换行
这样的效果好像并不太好呀,能不能就把incomprehensibilities拆一下,其它的单词不拆呢?那就需要下面这个属性了:
word-wrap(overflow-wrap)
word-wrap
又叫做overflow-wrap
:
word-wrap 属性原本属于微软的一个私有属性,在 CSS3 现在的文本规范草案中已经被重名为 overflow-wrap 。 word-wrap 现在被当作 overflow-wrap 的 “别名”。 稳定的谷歌 Chrome 和 Opera 浏览器版本支持这种新语法。
这个属性也是控制单词如何被拆分换行的,实际上是作为word-break
的互补,它只有两个值:normal | break-word
,那我们看下break-word
:
终于达到了上文我们希望的效果,只有当一个单词一整行都显示不下时,才会拆分换行该单词。
所以我觉得overflow-wrap
更好理解好记一些,overflow,只有长到溢出的单词才会被强制拆分换行!
(其实前面的word-break
属性除了列出的那三个值外,也有个break-word
值,效果跟这里的word-wrap:break-word
一样,然而只有Chrome、Safari等部分浏览器支持)
word-break、word-wrap、white-space区别的更多相关文章
- LeetCode之“动态规划”:Word Break && Word Break II
1. Word Break 题目链接 题目要求: Given a string s and a dictionary of words dict, determine if s can be seg ...
- 17. Word Break && Word Break II
Word Break Given a string s and a dictionary of words dict, determine if s can be segmented into a s ...
- LeetCode ||& Word Break && Word Break II(转)——动态规划
一. Given a string s and a dictionary of words dict, determine if s can be segmented into a space-sep ...
- leetcode@ [139/140] Word Break & Word Break II
https://leetcode.com/problems/word-break/ Given a string s and a dictionary of words dict, determine ...
- word break和word wrap
默认情况下,如果同一行中某个单词太长了,它就会被默认移动到下一行去: word break(normal | break-all | keep-all):表示断词的方式 word wrap(norma ...
- [LeetCode] Word Break II 拆分词句之二
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- LeetCode Word Break II
原题链接在这里:https://leetcode.com/problems/word-break-ii/ 题目: Given a string s and a dictionary of words ...
- 140. Word Break II
题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...
- 139. Word Break
题目: Given a string s and a dictionary of words dict, determine if s can be segmented into a space-se ...
- LeetCode: Word Break II 解题报告
Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...
随机推荐
- WPF引用WinForm控件
前言:在WPF开发当中由于DataGrid控件实现业务未达到非常理想的效果,我决定使用WinForm中的DataGridView来实现业务. 在XAML中加入以下命名空间: xmlns:wf=&quo ...
- Python文件的操作函数的使用
字符编码 二进制和字符之间的转换过程 --> 字符编码 ascii,gbk,shit,fuck 每个国家都有自己的编码方式 美国电脑内存中的编码方式为ascii ; 中国电脑内存中的编码方式为g ...
- 带你了解Android Jetpack
1.Jetpack主要特性有以下三点: 1.加速开发组件可单独使用,也可以协同工作,当使用kotlin语言特性时,可以提高效率. 2.消除样板代码Android Jetpack可管理繁琐的Activi ...
- 报错解决 unable to unroll loop, loop does not appear to terminate in a timely manner (994 iterations) or unrolled loop is too large, use the [unroll(n)] attribute to force an exact higher number
在 Unity 写 Shader 的时候,在一个循环里面使用了 tex2D 函数,类似与下面这样: fixed2 center = fixed2(0.5,0.5); fixed2 uv = i.uv ...
- ConcurrentHashMap(1.8)分析
在ConcurrentHashMap(1.8)中与HashMap非常相似,只不过它是线程安全的,在这里主要分析一下putVal()方法,看看与HashMap的区别. final V putVal(K ...
- python通过http下载文件的方法
1.通过requests.get方法 r = requests.get("http://200.20.3.20:8080/job/Compile/job/aaa/496/artifact/b ...
- Android Studio出现Failed to open zip file问题的解决方法
直接在网上找到gradle-3.3-all.zip下载下来,不要解压缩,放在类似下面的目录中 C:\Users\Administrator\.gradle\wrapper\dists\gradle-3 ...
- 中国古风唯美水墨工作计划汇报PPT模板推荐
模版来源:http://ppt.dede58.com/
- Spring(4)AOP
Spring(4)AOP 1.AOP概述 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种 ...
- 12c Data guard Switchover Best Practices using SQLPLUS (Doc ID 1578787.1)
12c Data guard Switchover Best Practices using SQLPLUS (Doc ID 1578787.1) APPLIES TO: Oracle Databas ...