css之background属性总结

background 一个集成属性,按照书写的顺序:包含以下子属性:

  • background-image: 指定元素使用的背景图像。可以是图片路径或使用渐变创建的“背景图像”
  • background-position: 指定背景图像在元素中出现的位置。
  • background-size: 指定背景图像尺寸。
  • background-repeat: 指定背景图像如何填充。
  • background-attachment: 定义滚动时背景图像相对于谁固定。
  • background-origin: 指定背景图像从元素的哪个区域作为显示的原点。
  • background-clip: 指定背景图像向外裁剪的区域。
  • background-color: 指定背景颜色。
background-image

属性值: 使用绝对或相对地址指或者创建渐变色来确定图像
说明: 在同一组背景定义中,如果背景颜色和背景图像都设置了,那么背景图像会覆盖在背景颜色之上。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
   html, body {
        height: 100%;
    }
    /* 在同一组背景定义中,如果背景颜色和背景图像都设置了,那么背景图像会覆盖在背景颜色之上 */
    .image {
        width:500px;
        height: 500px;
        background-image: url(./img/dog.jpg);
        background-repeat: no-repeat;
        background-color: red;
    }
    /* 使用绝对或相对地址指或者创建渐变色来确定图像 */
    .gradient {
        height: 100px;
        background-image: linear-gradient(to top, #45B5DA, #0382AD);
    }
    /* 组合使用例子 */
    .multi {
        height: 500px;
        background-image: url(./img/dog.jpg),
                      url(./img/dog2.jpg),
                      url(./img/dog3.jpg),
                      linear-gradient(to top, #45B5DA, #0382AD);
        background-repeat: no-repeat;
        background-position: 0 0,
                            500px 0,
                            1000px 0;    }
</style>
<body>
    <div class="image"></div>
    <hr>
    <div class="gradient"></div>
    <hr>
    <div class="multi"></div>
</body>
</html>

效果图:


background-image
background-position

属性值包含:

  • percentage : 用百分比指定背景图像在元素中出现的位置。可以为负值。参考容器尺寸减去背景图尺寸进行换算。
  • length : 用长度值指定背景图像在元素中出现的位置。可以为负值。
  • center: 背景图像水平垂直局中。
  • left: 背景图像从元素左边开始出现。
  • right: 背景图像从元素右边开始出现。
  • top :背景图像从元素顶部开始出现。
  • bottom: 背景图像从元素底部开始出现。

只接受1到4个参数值
例如:
1个param:
center;
如果只提供一个,该值将用于横坐标;纵坐标将默认为50%(即center)。
2个params:
left top;
如果提供两个,第一个用于横坐标,第二个用于纵坐标。
3个params:
left bottom 10px;
4个params:
right 20px bottom 20px
注意:
如果提供三或四个,每个 <percentage><length> 偏移量之前都必须跟着一个边界关键字(即left | right | top | bottom,不包括center),偏移量相对关键字位置进行偏移。
假设要定义背景图像在容器中右下方,并且距离右边和底部各有20px

background: url(test1.jpg) no-repeat right 20px bottom 20px;

background: url(test1.jpg) no-repeat left bottom 10px;

示例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        /* 如果只提供一个,该值将用于横坐标;纵坐标将默认为50%(即center)。 */
        .position1 {
            border: 1px solid #000;
            width: 1000px;
            height: 350px;
            background-image: url(../img/dog.jpg);
            background-repeat: no-repeat;
            /* background-position: center; */
            background-position: 10%;
        }   
        .position2 {
            border: 1px solid #000;
            width: 1000px;
            height: 350px;
            background-image: url(../img/dog.jpg);
            background-repeat: no-repeat;
            background-position: 40px 50px;
        }
        .position3 {
            border: 1px solid #000;
            width: 1000px;
            height: 350px;
            background-image: url(../img/dog.jpg);
            background-repeat: no-repeat;
            background-position: left bottom 20px;
        }
        .position4 {
            border: 1px solid #000;
            width: 1000px;
            height: 350px;
            background-image: url(../img/dog.jpg);
            background-repeat: no-repeat;
            background-position: top 20px left 100px;
        }   
       
        
    </style>
</head> 
<body>
    <div class="position1"></div>
    <hr>
    <div class="position2"></div>
    <hr>
    <div class="position3"></div>
    <hr>
    <div class="position4"></div>
</body>
</html>

效果图

background-position.png

background-size

属性值:

  • length: 用长度值指定背景图像大小。不允许负值。
  • percentage: 用百分比指定背景图像大小。不允许负值。
  • auto:背景图像的真实大小。
  • cover:将背景图像等比缩放到完全覆盖容器,背景图像有可能超出容器。我的理解就是width或height有任何一个属性如果没有覆盖到容器范围,就会放大,另一个也跟谁着等比例放大。可能会超过图片的范围。
  • contain:将背景图像等比缩放到宽度或高度与容器的宽度或高度相等,背景图像始终被包含在容器内。我的理解就是,width或height,有任何一个属性超出了容器的范围,就会等比缩小,直到图片完全包含在容器内。

该属性只能接受1-2个参数:

  • 1param:
    1: cover 或者 contain(只接受一个)
    2: 只提供一个具体的值(length/percentage),该值用于定义背景图像的宽度,高度将依据图像宽度定义进行等比缩放计算得到。
  • 2params : 50% 50%, 如果提供两个,第一个用于定义背景图像宽度,第二个定义高度;

示例代码如下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        p{
            border:1px dashed #666;
            width:400px;
            height:350px;
            background:url(../img/dog3.jpg) no-repeat;}
        .cover p {
            background-size:cover;
        }
        .contain p { 
            background-size:contain;
        }
        .length p {
            background-size:100px 140px;
        }
        .percentage p {
            background-size: 50% 30%;
        }
    </style>
</head>
<body>
    <ul class="test">
        <li class="cover">
            <h2>cover</h2>
            <p>将背景图像等比缩放到完全覆盖容器,背景图像有可能超出容器。</p>
        </li>
        <li class="contain">
            <h2>contain</h2>
            <p>将背景图像等比缩放到宽度或高度与容器的宽度或高度相等,背景图像始终被包含在容器内。</p>
        </li>
        <li class="length">
            <h2>length</h2>
            <p>自定义背景图像大小</p>
        </li>
        <li class="percentage">
            <h2>percentage</h2>
            <p>自定义背景图像大小</p>
        </li>
    </ul>
</body>
</html>

效果如下:

background-position.png

background-repeat

属性值:

  • repeat-x:背景图像在横向上平铺
  • repeat-y: 背景图像在纵向上平铺
  • repeat: 背景图像在横向和纵向平铺
  • no-repeat: 背景图像不平铺
  • round: 当背景图像不能以整数次平铺时,会根据情况缩放图像。(CSS3)
  • space: 当背景图像不能以整数次平铺时,会用空白间隙填充在图像周围。(CSS3)

该属性接受1-2个参数:

  • 如果提供两个参数值,第一个用于横向,第二个用于纵向;
  • 提供一个,则同时应用于横向与纵向,特殊值repeat-x和repeat-y除外,因为repeat-x相当于repeat no-repeat,repeat-y相当于no-repeat repeat,即其实repeat-x和repeat-y等价于提供了2个参数值

示例代码

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>Document</title>
   <style>
   .repeat-x {
       width: 2500px;
       height: 350px;
       background-image: url(../img/dog.jpg);
       background-repeat: repeat-x;
   }
   .repeat-y {
       width: 400px;
       height: 750px;
       background-image: url(../img/dog.jpg);
       background-repeat: repeat-y;
   }
   .repeat {
       width: 2000px;
       height: 400px;
       background-image: url(../img/dog.jpg);
       background-repeat: repeat;
   }
   .round {
       width: 2000px;
       height: 800px;
       background-image: url(../img/dog.jpg);
       background-repeat: round;
   }
   .space {
       width: 2000px;
       height: 700px;
       background-image: url(../img/dog.jpg);
       background-repeat: space;
       background-color: red;
   }
   </style>
</head>
<body>
   <div class="repeat-x">横向平铺背景图片</div>
   <hr>
   <div class="repeat-y">纵向平铺背景图片</div>
   <hr>
   <div class="repeat">平铺背景图片</div>
   <hr>
   <div class="round">背景图像不能以整数次平铺时,会根据情况缩放图像</div>
   <hr>
   <div class="space">当背景图像不能以整数次平铺时,会用空白间隙填充在图像周围</div>
</body>
</html>

效果图



background-attachment

属性值:

  • fixed 背景图像相对于视口(viewport)固定。
  • scroll 背景图像相对于元素固定,也就是说当元素内容滚动时背景图像不会跟着滚动,因为背景图像总是要跟着元素本身。但会随元素的祖先元素或窗体一起滚动。
  • local 背景图像相对于元素内容固定,也就是说当元素随元素滚动时背景图像也会跟着滚动,因为背景图像总是要跟着内容。
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            height: 350px;
            width: 1000px;
            background-image: url(../img/dog.jpg);
            background-repeat: no-repeat;
            background-position: 10% 10%;
            background-attachment: local;
            overflow: scroll;
        }
    </style>
</head>
<body>
    <div>
        <p>背景图像不随窗体内容滚动始终固定</p>
        <p>文字内容</p>
        <p>文字内容</p>
        <p>文字内容</p>
        <p>文字内容</p>
        <p>文字内容</p>
        <p>文字内容</p>
        <p>文字内容</p>
        <p>文字内容</p>
        <p>文字内容</p>
        <p>文字内容</p>
        <p>文字内容</p>
        <p>背景图像不随窗体内容滚动始终固定</p>
    </div>
    
</body>
</html>

效果图

image.png

background-origin

属性值

  • border-box:从border区域(含border)开始显示背景图像。
  • padding-box:从padding区域(含padding)开始显示背景图像
  • content-box:从content区域开始显示背景图像。
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        p{
            border:10px dashed #666;
            width:400px;
            height:400px;
            padding:20px;
            background:url(../img/dog.jpg)  no-repeat;
            background-size: contain;
            background-color: #aaa;
        }
        .border-box p {
            background-origin:border-box;
        }
        .padding-box p {
            background-origin:padding-box;
        }
        .content-box p {
            background-origin:content-box;
        }
    </style>
</head>
<body>
    <ul>
        <li class="border-box">
            <h2>border-box</h2>
            <p>从border区域(含border)开始显示背景图像</p>
        </li>
        <li class="padding-box">
            <h2>padding-box</h2>
            <p>从padding区域(含padding)开始显示背景图像</p>
        </li>
        <li class="content-box">
            <h2>content-box</h2>
            <p>从content区域开始显示背景图像</p>
        </li>
    </ul>
</body>
</html>

效果图:

background-clip
默认值: border-box
属性值:

  • border-box:从border区域(含border)开始向外裁剪背景。
  • padding-box:从padding区域(含padding)开始向外裁剪背景。
  • content-box:从content区域开始向外裁剪背景。
  • text:从前景内容的形状(比如文字)作为裁剪区域向外裁剪,如此即可实现使用背景作为填充色之类的遮罩效果。

代码演示

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        
        p {
            width:400px;
            height:200px;
            margin:0;
            padding:20px;
            border:20px dashed #666;
            background:url(../img/dog.jpg) no-repeat;
            background-origin:border-box;
            /* 背景颜色也会被剪裁 */
            background-color: #aaa ;
        }
        .border-box p {
            background-clip:border-box;
        }
        .padding-box p {
            background-clip:padding-box;
        }
        .content-box p {
            background-clip:content-box;
        }
        .text p { 
            width:100%;
            height:100%;
            background-repeat:repeat;
            -webkit-background-clip:text;
            -webkit-text-fill-color:transparent;
            font-weight:bold;
            font-size:120px;
        }
    </style>
</head>
<body>
    <ul>
        <li class="border-box">
            <h2>border-box</h2>
            <p>从border区域(不含border)开始向外裁剪背景</p>
        </li>
        <li class="padding-box">
            <h2>padding-box</h2>
            <p>从padding区域(不含padding)开始向外裁剪背景</p>
        </li>
        <li class="content-box">
            <h2>content-box</h2>
            <p>从content区域开始向外裁剪背景</p>
        </li>
        <li class="text">
            <h2>text</h2>
            <p>从前景内容的形状作为裁剪区域向外裁剪背景</p>
        </li>
    </ul>
</body>
</html>

效果图:

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 224,242评论 6 522
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 95,953评论 3 402
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 171,299评论 0 366
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 60,709评论 1 300
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 69,723评论 6 399
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 53,236评论 1 314
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 41,629评论 3 428
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 40,594评论 0 279
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 47,135评论 1 324
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 39,156评论 3 345
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 41,285评论 1 354
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,914评论 5 350
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 42,600评论 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 33,073评论 0 25
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 34,203评论 1 275
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 49,798评论 3 381
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 46,339评论 2 365