zoukankan      html  css  js  c++  java
  • css绝对对齐

    方法1:使用text-align:justify

    能够兼容所有的浏览器,但是一定要在模块和模块或者字之间存在空格,换行符或者制表符,这样才能起作用

    *{margin:0;padding:0;}
    /* 
     说明:
     1.IE中要实现块内单行两端对齐需要使用其私有属性text-align-last:justify配合,text-align-last 要生效,必须先定义text-align 为justify
     2.line-height:0 解决标准浏览器容器底部多余的空白
    */
    .demo{
         text-align:justify;
         text-align-last:justify;
         line-height:0;
         height:44px;
    }
    /*
     说明:
     模块使用[换行符]或[空格符]后,webkit浏览器中会引起最后一个模块有多余空白,使用font-size:0可清除该空格
    */
    @media all and (-webkit-min-device-pixel-ratio:0){
    .demo{
         font-size:0;
    }
    }
     /* 
     说明:
     1.text-align-last:justify 目前只有IE支持,标准浏览器需要使用 .demo:after 伪类模拟类似效果 
     2.opera浏览器需要添加 vertical-align:top 才能完全解决底部多余的空白
     */
    .demo:after{
         display:inline-block;
         overflow:hidden;
         width:100%;
         height:0;
         content:'';
         vertical-align:top;
    }
    .demo a{
         width:20%;
         display:inline-block;
         height:44px;
         line-height:44px;
         text-align:center;
         border:1px solid #428cc8;
         color:#666;
         font-size:16px;
         margin-bottom:5px;
         border-radius:3px;
         background-color:#fefefe;
         background-image:-webkit-gradient(linear,left top,left bottom,color-stop(0,#fefefe),color-stop(1,#eee));
         color:#666;
         text-decoration:none;
    }

    因为text-align-last:justify暂时只是支持IE,故在谷歌等浏览器中最后一行不会justify,所以我们使用after创作一个伪类,充当最后一行,设置height等于0,原先的最后一行就变成了倒数第二行,故而谷歌等浏览器中木有text-align-last:justify亦可。

    对于文本动态获取的元素,可以使用函数

    function justify_Let(obj){
         var obj=$(obj);
        var lengths=[];
        obj.each(function(){
            lengths.push($(this).text().length);
    
        });
        //var smax=Math.max(lengths);
        if(lengths.length==0){return;}
        for(var i=0,smax=lengths[0],len=lengths.length;i<len;i++){
            if(lengths[i]>smax){
                smax=lengths[i];
            }
        }
    
        for(var i=0,len=lengths.length;i<len;i++){
            var currlen=lengths[i];
            if(currlen==smax){continue;}
            obj.eq(i).css({"letter-spacing": ((smax-currlen)/(currlen-1))+"em"});
        }
    <p class="t1">姓名</p>
    <p class="t1">性别</p>
    <p class="t1">兴趣爱好</p>
    <p class="t1">座右铭</p>
    <script type="text/javascript">// <![CDATA[
    justify_Let(".t1")
    // ]]></script>

    方法2:使用justify-content:space-between;

    box-pack是css3的新书习惯,依赖于display:box,搜到box-orient影响, box-pack决定了子标签水平对其的方式,可选择 start| end | center| justify

    <div class="demo">
        <a class="link" href="#none">10元</a>
        <a class="link" href="#none">20元</a>
        <a class="link" href="#none">30元</a>
        <a class="link" href="#none">50元</a>
    </div>
    *{margin:0;padding:0;}
    /*
     说明:
     display:box定义布局为盒模型后,可使用盒模型下的box-pack:justify属性
    */
    .demo{
        display:-webkit-box;
        display:-webkit-flex;
        display:-ms-flexbox;
        display:flex;
        -webkit-box-pack:justify;
        -webkit-justify-content:space-between;
        -ms-flex-pack:justify;
        justify-content:space-between;
    }
    
    .demo a{
         width:20%;
         display:block;
         height:44px;
         line-height:44px;
         text-align:center;
         border:1px solid #428cc8;
         color:#666;
         font-size:16px;
         margin-bottom:5px;
         border-radius:3px;
         background-color:#fefefe;
         background-image:-webkit-gradient(linear,left top,left bottom,color-stop(0,#fefefe),color-stop(1,#eee));
         color:#666;
         text-decoration:none;
    }

    方法3:使用column:

    <div class="demo">
        <a class="link" href="#none">10元</a>
        <a class="link" href="#none">20元</a>
        <a class="link" href="#none">30元</a>
        <a class="link" href="#none">50元</a>
    </div>
    *{margin:0;padding:0;}
    /* 
     说明:
     1.column-count定义了对象的列数,例子中有4个模块,那么定义为4列
     2.column-gap定义了对象中列与列的间距,间距不能设置为百分比,显得不够灵活
    */
    .demo{
         -webkit-column-count:4;-moz-column-count:4;column-count:4;
         -webkit-column-gap:20px;-moz-column-gap:20px;column-gap:20px; 
    }
    
    .demo a{
         display:block;
         height:44px;
         line-height:44px;
         text-align:center;
         border:1px solid #428cc8;
         color:#666;
         font-size:16px;
         margin-bottom:5px;
         border-radius:3px;
         background-color:#fefefe;
         background-image:-webkit-gradient(linear,left top,left bottom,color-stop(0,#fefefe),color-stop(1,#eee));
         color:#666;
         text-decoration:none;
    }

    参考:http://www.cnblogs.com/PeunZhang/p/3289493.html

    参考:http://blog.csdn.net/nieshanfeng1/article/details/19193323

  • 相关阅读:
    部署asp.net出现了问题
    给中国学生的第三封信[转载:李开复]
    给中国学生的第四封信:[转载:李开复]
    GMAIL DRIVE 提供邮箱一样大小的硬盘
    天气凉了,好舒服
    Asp.net与SQL一起打包部署安装
    无刷新联动下拉菜单(DotTextWeb版)
    我的主界面
    DataList绑定时控制字符串长度
    给中国学生的第一封信 [转载:李开复]
  • 原文地址:https://www.cnblogs.com/RachelChen/p/6641926.html
Copyright © 2011-2022 走看看