background-size CSS屬性[剛剛看到,分享給大家]

來源:互聯網
上載者:User

標籤:class   code   c   使用   a   rgb   

 

如果問是什麼讓所有男人都生活中恐懼中,回答是不確定。沒有人表露,但好奇會害死貓。十個女人中會有九個同意這點。廣告商想盡辦法讓我們覺得不足夠,但會告訴我們還可以改進。沒錯,先生們….size很重要。

我說的是background-size。CSS裡的background-size屬效能夠讓程式員決定如何在指定的元素裡展示。MooTools程式員Christoph Pojer在他的網站上運用了background-size技術,使得在瀏覽器上他的頭像始終“全面覆蓋”在頁頭上——甚至是在手機上。下面讓我們來看看神奇的background-size屬性是如何使用的。

background-size CSS屬性

這個background-size屬性有幾個預定義的值,但也可以像其它size屬性一樣使用數字:

  • contain: 包含,整個背景圖都要被包含在元素內,沒有超出的部分。
  • cover: 覆蓋,背景要覆蓋元素的所有地區,不能有空白出現。
  • 長度/百分比: 數字值

不管你相信與否,這最有趣的部分是當它的值為長度和百分比的時候。下面用幾個例子示範一下 background-size的幾種不同用法:

/* base header classes */#header {/* header dimension! */height: 350px;/* additional background properties */background-repeat: no-repeat;background-position: center center;/* some box shadow for good fun */-webkit-box-shadow: rgba(0,0,0,0.20) 0 10px 10px;-moz-box-shadow: rgba(0,0,0,0.20) 0 10px 10px;box-shadow: rgba(0,0,0,0.20) 0 10px 10px;}/* 覆蓋 cover */#header.flex {/* size matters */-webkit-background-size: cover;-moz-background-size: cover;background-size: cover;}/* 包含 contain */#header.flex {/* size matters */-webkit-background-size: contain;-moz-background-size: contain;background-size: contain;}/* flex, fun */#header.flex {/* size matters */-webkit-background-size: 100% auto;-moz-background-size: 100% auto;background-size: 100% auto;}

Christoph在其網站上使用了最後一個例子。

background-size神奇之處就在於它的自我調整能力。我以前常常擔心當瀏覽器視窗發生變化,當有resize事件發生時,頁面會出現不可預測的變化。但background-size卻能根據用戶端瀏覽器的大小自我的調整適應。background-size在Firefox瀏覽器, Safari瀏覽器, Google瀏覽器, Opera, 和 IE9+ 中都受支援。

你覺得這種技術有價值嗎?會使用在你的頁面上嗎?

聯繫我們

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