|
我们在一些页面中,需要输入英文文章,有时候会遇到烦恼: 1、容器在较小的时候很轻易的被长单词给撑开了,超出了原来设置的宽度,影响了页面的整体布局,破坏美观与页面外观的结构! 2、在输入英文句子的时候,单词不能自动断开,行尾部空着很多,但宽度容不下一个单词,它就自动跑到下一行去了。导致页面文本区域右侧参差不齐。
遇到这样的问题,我们该如何解决呢?CSS为我们提供了两个属性word-break、word-wrap。我们可以利用它们来对我们的文本进行控制。使长英文单词或句子,以我们预想的形式显示出来! 关于word-break请参考这里;关于word-wrap请参考这里。
我们看下面的例子,看设置后的效果与不作任何设置的变化!
不作任何设置的情形:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>www.xxcss.com</title> <style type="text/css"> <!-- .woaicss { width:100px; background:#ccc; margin:20px auto 0 auto; padding:0; font-size:12px; color:#c00; } --> </style> </head> <body> <div class="woaicss">background width padding font</div> <div class="woaicss"></div> <div class="woaicss">backgroundbackgroundbackground</div> </body> </html>
上面容器的单词出现了右部很多的空白,中间的容器是参考宽度的空容器,下面的容器被长单词给撑开了。
我们加上word-wrap: break-word;看下面的效果: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>www.xxcss.com</title> <style type="text/css"> <!-- .woaicss { width:100px; background:#ccc; margin:20px auto 0 auto; padding:0; font-size:12px; color:#c00; word-wrap: break-word; } --> </style> </head> <body> <div class="woaicss">background width padding font</div> <div class="woaicss"></div> <div class="woaicss">backgroundbackgroundbackground</div> </body> </html>
最下面的容器没有被撑开,而是长单词自动的进行了换行。这样就不会影响布局了。但上面容器的单词右部很多的空白依然存在。
我们加上word-break : break-all;看下面的效果:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>www.xxcss.com</title> <style type="text/css"> <!-- .woaicss { width:100px; background:#ccc; margin:20px auto 0 auto; padding:0; font-size:12px; color:#c00; word-break: break-all; } --> </style> </head> <body> <div class="woaicss">background width padding font</div> <div class="woaicss"></div> <div class="woaicss">backgroundbackgroundbackground</div> </body> </html>
上面的容器右侧没有出现空白,尽量多的写单词字母。最下面的容器也没有被容器撑开.
|