首页 > web前端 > css教程 > 正文

CSS中关于background属性的具体分析

黄舟
发布: 2017-07-21 09:08:02
原创
2292人浏览过

一、background属性可以设置一个元素的背景样式,当然前提是这个元素有具体的宽高值。

先来一个简单的背景设置:

#show-box {
            width: 800px;
            height: 500px;
            background: #000;
            background-image: url(image url);
        }
    </style>
登录后复制

这里只是简单的设置了颜色和背景贴图。

下面让我们来看一下官方的background的属性:

  语法格式:

  background: color position size repeat origin clip attachment image;

立即学习前端免费学习笔记(深入)”;

  注意:如果同时设置了“position”和“size”两个属性,应该用左斜杠“/”,而不是用空格把两个参数值隔开:“position/size”。

1 background: url("img.jpg") center center/100% 100% no-repeat;
登录后复制

  属性表(图片可能会显示得太小,请右键“在新标签中打开”以查看原图):

  background

  1.color:背景颜色值。这个属性会把整个元素添加颜色,而且处于最底层(在有背景图片的情况下就可以看出)。

  可选值:默认是透明,其他值可以通过查看“CSS颜色值表”来设置。

<style>
        #show-box {
            width: 180px;
            height: 180px;
            border: 20px dashed #000;
            background-color: #000000;
            background-color: blue;
            background-color: rgb(255, 255, 255);
            background-color: rgba(255, 255, 255, 0.8);
        }
    </style>
登录后复制

  2.position:背景图片的定位。如果没有图片设置,那么这个属性不起效果。

  可选值:两个参数,水平位置和垂直位置。如果只有一个值,第二个值为“center”。

  默认值是元素的左上顶角。可以使用位置关键字(top,right,bottom,left,center)。百分比(以元素大小为基值)。像素值。

<style>
        #show-box {
            width: 180px;
            height: 180px;
            border: 20px dashed #000;
            background-position: center;
            background-position: center top;
            background-position: 0 100px;
            background-position: 10% 20%;
        }
    </style>
登录后复制

 

  3.size:图片尺寸。应用于图片。

  可选值:两个数值,如果只有一个值,第二个值为auto。

  默认是图片自身大小。可以使用像素值,百分百(以元素大小为基值)。

  cover:等比例缩放图片,覆盖这个元素。类似与windows中桌面背景的“填充”。

  contain:等比例缩放图片,适应元素的宽或者高。类似于windows中桌面背景的“适应”。

  

   4.repeat:平铺方式。

    repeat:完全平铺,复制图片把整个元素填满。(默认)

    repeat-x:水平平铺,在水平方向复制并平铺。

    repeat-y:垂直平铺,在垂直方向复制并平铺。

    no-repeat:不平铺,只使用一张图片。

   5.origin:背景的参考区域。

   可选值:border-box,padding-box,content-box。

   6.clip:背景的可视区域。

   可选值:border-box,padding-box,content-box。

   对比一下不同值的效果图:

    1.origin:border-box;clip:border-box;

<style>
        #show-box {
            width: 180px;
            height: 180px;
            margin: 20px;
            padding: 20px;
            border: 20px dashed #000;
            background: url("img.jpg") no-repeat border-box border-box;
        }
    </style>
登录后复制

    

 

    2.origin:padding-box;clip:border-box;

<style>
        #show-box {
            width: 180px;
            height: 180px;
            margin: 20px;
            padding: 20px;
            border: 20px dashed #000;
            background: url("img.jpg") no-repeat padding-box border-box;
        }
    </style>
登录后复制

    

    3.origin:content-box;clip:border-box;

<style>
        #show-box {
            width: 180px;
            height: 180px;
            margin: 20px;
            padding: 20px;
            border: 20px dashed #000;
            background: url("img.jpg") no-repeat content-box border-box;
        }
    </style>
登录后复制

    

 

    4.origin:border-box;clip:content-box;

<style>
        #show-box {
            width: 180px;
            height: 180px;
            margin: 20px;
            padding: 20px;
            border: 20px dashed #000;
            background: url("img.jpg") no-repeat border-box content-box;
        }
    </style>
登录后复制

    

 

  可以看出,origin设置的是位置,clip则会根据区域裁剪出背景图片。

   

  7.attachment:设置背景图像是否固定或者随着页面的其余部分滚动。

  默认值是scroll:背景图片随页面的其余部分滚动。fixed:背景图像是固定的。

 

  8.多背景设置。

  导入图片:background-image: url(image url);

 

二、多背景设置。

  多背景的写法:使用逗号“,”隔开,继续写背景属性。

  background: color position size repeat origin clip attachment image, color position size repeat origin clip attachment image;

  也可以具体属性单独设置:background-image:url(image url 1),url(image url 2);

<style>
        #show-box {
            width: 180px;
            height: 180px;
            border: 20px dashed #000;
            background: url("img.jpg1") left top/100% 100% no-repeat,
            url("img.jpg2") left center/100% 100% no-repeat,
            url("img.jpg3") left bottom/100% 100% no-repeat,
            url("img.jpg4") center top/100% 100% no-repeat;
        }
    </style>
登录后复制
<style>
        #show-box {
            width: 180px;
            height: 180px;
            border: 20px dashed #000;
            background-image: url("img.jpg1"), url("img.jpg2"), url("img.jpg3"), url("img.jpg4");
            background-position: left top, left center, left bottom, center top;
            background-size: 100%;
            background-repeat: no-repeat;
        }
    </style>
登录后复制

以上就是CSS中关于background属性的具体分析的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
相关标签:
css
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号