利用小數解析差異解決瀏覽器安全色性問題

來源:互聯網
上載者:User

標籤:style   blog   http   color   width   re   c   問題   

通常我們寫 css 的時候寫的數字都是整數,如 font-size:12px; margin:20px; 那麼看到標題可能有人會問,css 屬性值可以有小數點嗎?如果是小數那會顯示成什麼樣子?和整數有什麼區別?

首先我們先看個例子,通過例子來觀察下小數在各個瀏覽器的差異。

<!DOCTYPE HTML><html lang="en-US"><head>    <meta charset="UTF-8">    <title>decimal</title>    <style type="text/css">    body{font-family:SimSun;text-align:center;}    p{margin:20px;height:30px;}    .font11-9{font-size:11.9px;}    .font11-4{font-size:11.4px;}    </style></head><body>    <p class="font11-9">這段文字的大小是11.9像素。</p>    <p class="font11-4">這段文字的大小是11.4像素。</p></body></html>

我們可以看出在 chrome,firefox,ie8 下小數會通過四捨五入的方式轉成整數,而 ie6,ie7 會對小數進行下限取整轉成整數。通過這一特性我們在某些情況下,用小數來替代 css hack。譬如:

<!DOCTYPE HTML><html lang="en-US"><head>    <meta charset="UTF-8">    <title>decimal</title>    <style type="text/css">    body{font-family:SimSun;font-size:30px;}    .black{background:black;width:500px;height:500px;margin-left:auto;margin-right:auto;overflow:hidden;color:white;}    .white{background:white;width:100px;height:100px;margin:0.9px;}    </style></head><body>    <div class="black">        <div class="white"></div>        <p>在ie6,ie7下紅色塊離黑色塊沒有外邊距,而其他的瀏覽器則有 1px 外邊距。一般我們是寫css hack,如margin:1px;*margin:0;但是我們現在可以通過小數來解決啦。</p>    </div></body></html>

不僅僅縮短的代碼的長度,還去掉了 css hack。

總結:雖說這個小數解決一些相容性問題很神奇,但是它的缺點也很明顯,就是適用範圍,只能解決相差1像素的瀏覽器差異,只能解決 ie6,ie7 下值小1像素的瀏覽器差異。

瀏覽器:chrome,firefox,ie8,ie7,ie6

聯繫我們

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