2014-05-22 58 views
3

我試圖讓HTML/CSS一個更好的把握,很多時候我看到這樣 background-size: 100pxCSS背景大小的一個參數

的東西是什麼意思時,有東西給只有一個參數要求長度和寬度?它是否默認僅根據圖像定義長度和縮放寬度?還是默認爲寬度?

+1

單值的語法:該值定義了圖像的寬度,高度被隱式地設置爲「自動」 [MDN(https://developer.mozilla.org/ en-US/docs/Web/CSS/background-size) – George

回答

3

如果只定義了一種尺寸,則將其作爲寬度處理,高度自動設置爲auto

the Mozilla Developer Network

/* Keywords syntax */ 
background-size: cover 
background-size: contain 

/* One-value syntax: the value defines the width of the image, the height is implicitly set to 'auto' */ 
background-size: 50% 
background-size: 3em 
background-size: 12px 
background-size: auto 

/* Two-value syntax: the first value defines the width of the image, the second its height */ 
background-size: 50% auto 
background-size: 3em 25% 
background-size: auto 6px 
background-size: auto auto 

/* Values for the multiple backgrounds, defined by background-image, may be listed separated by commas */ 
background-size: auto, auto  /* Do not confuse this with background-size: auto auto */ 
background-size: 50%, 25%, 25% 
background-size: 6px, auto, contain 

background-size: inherit 
+0

所以我說背景大小:50%,目標寬度,auto目標是什麼?它是否像原始圖像的高度一樣留下高度?還是會將其縮小50% –

+0

它保存背景圖像的寬高比。請注意,'background-size:50%;'不會將背景圖像調整50%,而是將其設置爲容器寬度的50%。然後根據需要計算高度以保持縱橫比。 – Nit

+0

因此100x100的圖像不會被調整爲具有背景大小屬性的50x50?它會裁剪到50x50? –