css絕對對齊

來源:互聯網
上載者:User

標籤: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,故在Google等瀏覽器中最後一行不會justify,所以我們使用after創作一個偽類,充當最後一行,設定height等於0,原先的最後一行就變成了倒數第二行,故而Google等瀏覽器中木有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絕對對齊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.