1. 程式人生 > >word-wrap、white-space和word break的區別

word-wrap、white-space和word break的區別

src over word-wrap 也會 img lips 邊界 com 分享圖片

一、

先說white-space:

white-space規定了代碼中對空格的處理:
有幾個可選值:normal,nowrap,pre,pre-wrap,pre-line:

white-space:nowrap: nowrap和normal一樣,也合並空格,但是不會根據容器大小換行,表示不換行。

技術分享圖片

nowrap會導致文本不換行,經常和overflow,text-overflow一起使用

如overflow:hidden;text-overflow:ellipsis

white-space:pre保留空格不換行

作用是保持源碼中的空格,有幾個空格算幾個空格顯示,同時換行只認源碼中的換行和<br/>標簽。

技術分享圖片

white-space:pre-wrap 保留空格換行

技術分享圖片

white-space:pre-line合並空格換行 pre-line的作用是合並空格,換行和white-space:pre-wrap一樣,遇到源碼中的換行和<br/>會換行,碰到容器的邊界也會換行。

技術分享圖片

總結一下:wrap的意思是換行no-wrap就是不換行

pre的意思是不合並空格 保留原始樣式,所以

    no-wrap:不換行

    pre:保留空格(保留原始pre樣式)

pre-wrap: 保留空格並換行

    pre-line:保留原始空格並不換行(相當於 pre- no -wrap這個意思)

word-wrap、white-space和word break的區別