css 文本两端对齐

在做表单时我们经常遇到让上下两个字段对齐的情况,比如姓名, 手机号码, 出生地。这样我们就要用到 text-align, text-justify样式了。

text-align直接设为justify就行了,text-justify的情况就复杂了,可能有人对它还不熟悉。IE的取值如下:

auto :允许浏览器用户代理确定使用的两端对齐法则

inter-word :通过增加字之间的空格对齐文本。该行为是对齐所有文本行最快的方法。它的两端对齐行为对段落的最后一行无效

newspaper : 通过增加或减少字或字母之间的空格对齐文本。是用于拉丁文字母表两端对齐的最精确格式

distribute :处理空格很像newspaper,适用于东亚文档。尤其是泰国

distribute-all-lines :两端对齐行的方式与distribute相同,也同样不包含两段对齐段落的最后一行。适用于表意字文档

inter-ideograph : 为表意字文本提供完全两端对齐。他增加或减少表意字和词间的空格

但它最早是作为IE的私有实现,像text-overflow, overflow-x等,在FF很晚才实现,换言之有严格的兼容性问题。并且FF,chrome需要手动在汉字间插入空白或软换行标签才生效,在chrome遇到的阻力就更大了。

.test1 {
      text-align:justify;
      text-justify:distribute-all-lines;/*ie6-8*/
      text-align-last:justify;/* ie9*/
      -moz-text-align-last:justify;/*ff*/
      -webkit-text-align-last:justify;/*chrome 20+*/
  }
  @media screen and (-webkit-min-device-pixel-ratio:0){/* chrome*/
      .test1:after{
          content:".";
          display: inline-block;
          width:100%;
          overflow:hidden;
          height:0;
      }
  }

 

View Code
 1 <!DOCTYPE HTML>
 2 <html>
 3     <head>
 4         <title>文本两端对齐 </title>
 5         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 6 
 7    
 8         <style>
 9     
10             .box1{
11                 background:red;
12                 width:30%;
13             }
14             .test1 {
15                 text-align:justify;
16                 text-justify:distribute-all-lines;/*ie6-8*/
17                 text-align-last:justify;/* ie9*/
18                 -moz-text-align-last:justify;/*ff*/
19                 -webkit-text-align-last:justify;/*chrome 20+*/
20             }
21             @media screen and (-webkit-min-device-pixel-ratio:0){/* chrome*/
22                 .test1:after{
23                     content:".";
24                     display: inline-block;
25                     width:100%;
26                     overflow:hidden;
27                     height:0;
28                 }
29             }
30         </style>
31 
32     </head>
33     <body>
34         <div class="box1">
35             <div class="test1">姓 名</div>
36             <div class="test1">姓 名 姓 名</div>
37             <div class="test1">姓 名 名</div>
38             <div class="test1">所 在 地</div>
39             <div class="test1">工 作 单 位</div>
40         </div>
41 
42     </body>
43 </html>

 

效果图:

 

 

posted @ 2012-11-28 14:39  妍珊  阅读(5509)  评论(0编辑  收藏  举报