加入收藏 设为首页 联系站长
WEB2.0教程 | CSS教程 | HTML教程 | 设计素材 | CSS 模板 | CSS酷站欣赏 | CSS布局实例 | Javascript | 建站代码 | 专业软件 | 常见问题 | 内容搜索
>首页 -> 学习教程 -> CSS教程

TOP

如何避免容器被长单词撑开

[ 录入者:admin | 时间:2007-12-29 05:17:03 | 作者: | 来源: | 浏览:37次 ]

我们在一些页面中,需要输入英文文章,有时候会遇到烦恼:
  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>

上面的容器右侧没有出现空白,尽量多的写单词字母。最下面的容器也没有被容器撑开.

[上一篇]CSS selector 选择符有哪些 如何.. [下一篇]在CSS中写脚本实现交互效果

评论

称  呼:
内  容:

相关栏目

最新文章

热门文章

推荐文章

相关文章

推荐浏览

中国百度网
安徽皖一月嫂家政
犯罪现场