码迷,mamicode.com
首页 > Web开发 > 详细

css绝对对齐

时间:2017-03-29 22:25:20      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:for   enter   .net   demo   article   lock   height   style   ima   

方法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

css绝对对齐

标签:for   enter   .net   demo   article   lock   height   style   ima   

原文地址:http://www.cnblogs.com/RachelChen/p/6641926.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!