CSS background全部匯總

所有背景屬性都不能繼承。
1. background-color
所有元素都能設(shè)置背景顏色。
background-color的默認(rèn)值是transparent;也就是說,如果一個(gè)元素沒有指定背景顏色,那么背景就是透明的,這樣其祖先元素的背景才能可見。
2. background-image
所有元素都能設(shè)置背景圖像;
背景圖像可以設(shè)置多個(gè),中間用逗號隔開;背景圖像會層疊,寫在前面的層次在上面。
3. background-repeat
repeat(默認(rèn))/no-repeat/repeat-x/ repeat-y
4. background-attachment
scroll(默認(rèn))/fixed
5. background-position
像素法:以圖像左上角為起點(diǎn),第一個(gè)值為水平移動的距離,第二個(gè)值為垂直移動的距離;若只規(guī)定一個(gè)值,第二個(gè)值默認(rèn)為50%。
百分比法:百分比同時(shí)應(yīng)用于圖像和元素,相應(yīng)的點(diǎn)重合進(jìn)行定位。如果只指定一個(gè)百分?jǐn)?shù),意味著垂直方向?yàn)?0%。
關(guān)鍵字法:top、right、bottom、left、center進(jìn)行組合定位;若只規(guī)定一個(gè)值,第二個(gè)值默認(rèn)為center。
注意:background-position可以為負(fù)值。
在默認(rèn)情況下,背景顏色延伸到邊框下面,背景圖像在padding區(qū)域的左上角。
6. background-size
設(shè)置背景圖像的尺寸;默認(rèn)值為auto。
像素法:第一個(gè)值設(shè)置寬度,第二個(gè)值設(shè)置高度;若只有一個(gè)值,第二個(gè)值為auto。
百分比法:以父元素的寬度和高度以基準(zhǔn)來計(jì)算。
關(guān)鍵字cover,不改變圖像寬高比例,在水平和垂直方向都鋪滿整個(gè)元素,有可能導(dǎo)致一部分圖像溢出。
關(guān)鍵字contain,不改變圖像寬高比例,盡可能拉伸,直到某一方向鋪滿整個(gè)元素,有可能導(dǎo)致另一方向沒有鋪滿。
7. background-origin
定義背景圖像的初始位置
border-box,邊框左上角。
padding-box,padding區(qū)域左上角;默認(rèn)值。
content-box,內(nèi)容區(qū)左上角。
8. background-clip
The CSS3 background-clip property specifies the painting area of the background.
The property takes three different values:
•border-box - (default) the background is painted to the outside edge of the border
•padding-box - the background is painted to the outside edge of the padding
•content-box - the background is painted within the content box
(英文的解釋得比較清楚)
(英文都是從W3Schools Online搬運(yùn))
關(guān)于background-origin和background-clip,它們是相互獨(dú)立的,互不干擾。
關(guān)于background的CSS寫法,個(gè)人認(rèn)為應(yīng)該邏輯明確、層次分明;具體而言:
background定義背景圖像,background-color定義背景顏色,background-clip定義背景顯示區(qū)域。
(個(gè)人見解,僅供參考)
Full Size Background Image
If we want to have a background image on a website that covers the entire browser window at all times.
The requirements are as follows:
• Fill the entire page with the image (no white space)
• Scale image as needed
• Center image on page
• Do not cause scrollbars
The following example shows how to do it; Use the html element (the html element is always at least the height of the browser window). Then set a fixed and centered background on it. Then adjust its size with the background-size property:
- html {
- background: url(img_flower.jpg) no-repeat center fixed;
- background-size: cover;
- }
小tips:
利用背景圖像的水平平鋪,實(shí)現(xiàn)波浪式的邊框效果。
(目前只有想法,還沒找到符合需求的圖像。)
以上這篇CSS background全部匯總就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
css background-attachment屬性進(jìn)階
前提是定義了background-image屬性,然后用background-attachment來指明背景圖的位置是固定于視口的,還是隨著包含塊移動的??珊唵卫斫鉃槎x背景圖片隨滾動軸的移動方式2017-03-08css 背景固定樣式background-attachment屬性基礎(chǔ)
這篇文章主要為大家介紹了在CSS中,使用背景附件屬性background-attachment可以設(shè)置背景圖像是隨對象滾動還是固定不動,需要的朋友可以參考下2017-03-08- 下面小編就為大家?guī)硪黄猚ss background 背景圖的設(shè)置方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-08-30
CSS的background屬性及CSS3的背景圖片設(shè)置總結(jié)
這篇文章主要介紹了CSS的background屬性及CSS3的背景圖片設(shè)置總結(jié),背景圖片的顯示區(qū)域和定位是非常值得注意的地方,需要的朋友可以參考下2016-06-13- 邊框在Web頁面的內(nèi)容塊中非常常用,這里為大家整理了CSS制作邊框效果的技巧總結(jié),尤其是第三種方案的background-origin利用十分討巧,需要的朋友可以參考下2016-05-27
- 固定背景圖片的通常方法就是把background-attachment設(shè)成fix,進(jìn)一步的話自然則是用background-position,下面來詳解使用CSS固定頁面背景圖片及位置的方法:2016-05-17
- 這篇文章主要介紹了使用CSS3來實(shí)現(xiàn)滾動視差效果的教程,主要使用到了background-attachment屬性,需要的朋友可以參考下2015-08-24
css 中background 設(shè)置文本框背景圖 的方法
這篇文章主要介紹了css 中background 設(shè)置文本框背景圖 的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-22