詳解CSS3 中的calc() 用法

來源:互聯網
上載者:User
本文主要介紹了詳解CSS 3 的 calc() 方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下,希望能協助到大家。

下面一段代碼給大家介紹css3中的calc()方法,具體內容如下所示:


<p style="width:100px; height:50px; background:red;">  <p style="width:100%; height:20px; margin:5px; background:green;"></p></p>

[Ctrl+A 全部選擇 提示:您可先修改部分代碼,再按運行]

如上代碼,預覽可以看出紅色框超出了,因為在標準的 CSS 中,width 是不包含 margin 的(老 IE 中 width 是包含 margin 的)。

為了達到上述效果,我們一般會在中間再套一層 p(有人說,不是把 width:100% 取了就可以了嗎?冷靜,我們是舉例,實際有些情況下,不能取消 width:100%。)

有沒有更方便的呢?用 calc() 吧。


<p style="width:100px; height:50px; background:red;">  <p style="width:calc(100% - 10px); height:20px; margin:5px; background:green;"></p></p>

[Ctrl+A 全部選擇 提示:您可先修改部分代碼,再按運行]

使用說明

  • 支援:+、-、*、/,支援混用

  • 支援:%、px、em、rem 等

  • +、- 前後必須要有空格,比如:calc(100%-10px) 是不正確的,應該改為:calc(100% - 10px)

  • *、/ 前後可以不要空格,但建議有。

支援性

主流的案頭瀏覽器都支援。據說手機瀏覽器幾乎不支援。

相關文章

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.