使用 CSS3 的 box-sizing 屬性設定元素大小包含 border 與 padding

來源:互聯網
上載者:User

標籤:html   css3   bsp   add   width   text   tle   ext1   idt   

  • 預設情況下,內部元素(如:input)的寬度或高度,是不會包含元素的邊框和內邊距的,這是就需要使用 box-sizing 屬性設定該元素。

     

  • box-sizing 是 CSS3 的屬性,可以設定以上值:
  1. content-box: 元素 size 不包含 border 和 padding,預設值。
  2. border-box: 元素 size 包含 border 和 padding。
  3. inherit: 指定box-sizing屬性的值,應該從父元素繼承。

 

  1. 樣本1(不使用 box-sizing 屬性):
    1. HTML

      <!DOCTYPE html>
      <html
      lang="en">
      <head>
      <meta
      charset="UTF-8">
      <title>
      box-sizing</title>
      <style
      type="text/css">
      #div1
      {
      width: 300px;
      height: 100px;
      border: solid 1px blue;
      }
      #text1, #text2{
      width: 100%;
      }
      #text2{
      padding-left: 10px;
      }
      </style>
      </head>

      <body>
      <div
      id="div1">
      <input
      type="text" id="text1" value="abc"/>
      <input
      type="text" id="text2" value="abc"
      />
      </div>
      </body>
      </html>

    2. 效果

    3. 可以發現,內部元素的寬度已經超過父元素寬度。

       

  2. 樣本2(使用 box-sizing 屬性):
    1. 只需要加入如下樣式

      #text1, #text2{
      width: 100%;
      box-sizing: border-box;
      }

    2. 效果

總結:木有^_^,會用就行。。

使用 CSS3 的 box-sizing 屬性設定元素大小包含 border 與 padding

聯繫我們

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