css 多個不定數量提交按鈕置中顯示,純css解決,css置中

來源:互聯網
上載者:User

css 多個不定數量提交按鈕置中顯示,純css解決,css置中

前幾天在公司修改一個css 多個按鈕置中問題,其實這樣的問題很多前端程式員都遇到過,舉個例子吧:

在一行中有三個按鈕或是兩個按鈕...個數不定,然後間距固定;然後就有很多人把所有按鈕放到一個div中,把div置為margin:10px auto(距上10像素,置中,然後又給了一個固定寬度,按鈕放在這個div中,這樣按鈕就不能具體置中了) ,也不通用如果按鈕減少到兩個 或一個怎麼辦,

也有很多人用javascript 動態算出寬度然後計算一大堆,並且很多時候比好用

錯誤碼:

 1 <!DOCTYPE html> 2 <html> 3 <head> 4     <meta charset="utf-8"> 5     <title></title> 6     <style type="text/css"> 7     .foot{width: 100%; height: 30px; border: 1px solid #d2d2d2;} 8     .foot .b{width:300px; margin: 3px auto;} 9     .foot .b .button{display: inline-block;line-height: 20px; background-color: #900; padding: 3px 5px; margin-left: 10px;}10     </style>11 </head>12 <body>13 <div class="foot">14     <div class="b">15         <a href="" class="button">提交</a>16         <a href="" class="button">提交</a>17         <a href="" class="button">提交</a>18     </div>19 </div>20 </body>21 </html>

 

後來修改如下

<!DOCTYPE html><html><head>    <meta charset="utf-8">    <title></title>    <style type="text/css">    .foot{width: 100%; height: 30px; border: 1px solid #d2d2d2; text-align: center;}    .foot ul{display: inline; margin-left: -10px;}    .foot ul li{display: inline-block; margin-left: 10px; line-height: 30px;}    .foot ul li a{background-color: #900; color: #fff;line-height: 20px;padding: 3px 5px;}    </style></head><body><div class="foot">    <ul>        <li><a href="" class="button">提交</a></li>        <li><a href="" class="button">提交</a></li>        <li><a href="" class="button">提交</a></li>        <li><a href="" class="button">提交</a></li>    </ul>    </div></div></body></html>

其實這些問題看上去很簡單,單還是有很多初學者不能實現,很多人也行用javascript實現,其實完全沒有必要

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.