美文网首页
05-CSS基础-背景和精灵图

05-CSS基础-背景和精灵图

作者: xiaohan_zhang | 来源:发表于2019-08-06 09:57 被阅读0次
背景颜色

在CSS中可以通过background-color:属性设置标签的背景颜色。

取值:
  单词
  rgb
  rgba
  十六进制

    <style>
        div{
            width: 300px;
            height: 150px;
        }
        .box1{
            background-color: green;
        }
        .box2{
            background-color: #FF0000;
        }
        .box3{
            background-color: rgb(200, 200, 100);
        }
        .box4{
            background-color: rgba(100, 200, 200, 0.8);
        }
    </style>

快捷键:
bc background-color: #fff;

背景图片

在CSS可以通过background-image: url();设置背景图片。

<style>
    .box1{
        background-image: url(images/0075Z4Wggy1fpoz1ftpj4j30qo15xn6b.jpg);
        /*background-image: url(http://img4.imgtn.bdimg.com/it/u=2278032206,4196312526&fm=21&gp=0.jpg);*/
    }
</style>

<div class="box1"></div>

注意点:

  1. 图片的地址必须放在url()中, 图片的地址可以是本地的地址, 也可以是网络的地址;
  2. 如果图片的大小没有标签的大小大, 那么会自动在水平和垂直方向平铺来填充;
    快捷键:
    bi background-image: url();
背景平铺

在CSS中可以通过background-repeat属性控制背景图片的平铺方式的。

取值:
  repeat   默认, 在水平和垂直都需要平铺
  no-repeat 在水平和垂直都不需要平铺
  repeat-x  只在水平方向平铺
  repeat-y  只在垂直方向平铺
background-repeat: repeat;

应用场景:
可以通过背景图片的平铺来降低图片的大小, 提升网页的访问速度;(有规律的小图片拼接在一起,看不出是多张图片)
可以将多张图片拼接成一张图片;


注意点:
背景颜色和背景图片可以共存, 图片会覆盖颜色

快捷键
bgr background-repeat:

背景定位

在CSS中有一个叫做background-position:属性, 就是专门用于控制背景图片的位置。

格式:
  background-position: 水平方向 垂直方向;

取值:
  具体的方位名词
    水平方向: left center right
    垂直方向: top center bottom
  具体的像素
    background-position: 100px 200px;(具体的像素是可以接收负数)

<style>
    .box4{
        background-color: rgba(100, 200, 200, 0.8);
        background-image: url("images/A8016EB2-6C39-4D7C-BA19-96F8AB8AAD74-54346-00000D006FF6F167.JPG");
        background-repeat: no-repeat;
        /*background-position: left top;*/
        /*background-position: right top;*/
        /*background-position: right bottom;*/
        /*background-position: left bottom;*/
        /*background-position: center center;*/
        /*background-position: left center;*/
        /*background-position: center top;*/

        /*background-position: 50px 0;*/
        background-position: 50px 100px;
        /*background-position: -10px -30px;*/
</style>

应用场景:
当图片比较大的时候, 可以通过定位属性保证图片永远居中显示。

快捷键:
bp background-position: 0 0;

背景属性连写

背景属性缩写的格式:
background: 背景颜色 背景图片 平铺方式 关联方式 定位方式;

注意点:
background属性中, 任何一个属性都可以被省略。

快捷键:
bg+ background: #fff url() 0 0 no-repeat;

background: #666 url("images/A8016EB2-6C39-4D7C-BA19-96F8AB8AAD74-54346-00000D006FF6F167.JPG") 0 0 no-repeat;
背景关联

默认情况下背景图片会随着滚动条的滚动而滚动, 如果不想让背景图片随着滚动条的滚动而滚动, 那么我们就可以修改背景图片和滚动条的关联方式。
在CSS中有一个叫做background-attachment的属性, 这个属性就是专门用于修改关联方式的。

格式:
  background-attachment:scroll;

取值:
  scroll   默认值, 会随着滚动条的滚动而滚动
  fixed   不会随着滚动条的滚动而滚动

快捷键:
ba background-attachment:;

插入图片和背景图片的区别
  1. 背景图片仅仅是一个装饰, 不会占用位置
    插入图片(img)会占用位置;
  2. 背景图片有定位属性, 可以很方便的控制图片的位置
    插入图片没有定位属性, 控制图片的位置不太方便;
  3. 插入图片的语义比背景图片的语义要强, 所以在企业开发中如果你的图片想被搜索引擎收录, 那么推荐使用插入图片;
css精灵

CSS精灵图是一种图像合成技术, 全称CSS Sprite。
CSS精灵图作用:
可以减少请求的次数, 以及可以降低服务器处理压力。
CSS的精灵图需要配合背景图片和背景定位来使用。

    <style>
        .box{
            width: 86px;
            height: 28px;
            background-image: url(images/weibo.png);
            background-position: -425px -200px;
        }
    </style>
    <div class="box"></div>
完整图片
显示的图片
  • 背景尺寸属性
    background-size
    CSS3中新增的属性,专门用于设置背景图片大小。
<style>
    *{
        margin: 0;
        padding: 0;
    }
    ul{
        width: 1200px;
        height: 500px;
        margin: 0 auto;
    }
    ul li{
        list-style: none;
        float: left;
        width: 200px;
        height: 200px;
        margin: 30px 30px;
        border: 1px solid #000;
        text-align: center;
        line-height: 200px;
    }
    ul li:nth-child(1){
        background: url("images/A8016EB2-6C39-4D7C-BA19-96F8AB8AAD74-54346-00000D006FF6F167.JPG") no-repeat;
    }
    ul li:nth-child(2){
        background: url("images/A8016EB2-6C39-4D7C-BA19-96F8AB8AAD74-54346-00000D006FF6F167.JPG") no-repeat;
        background-size: 100px 100px;
    }
    ul li:nth-child(3){
        background: url("images/A8016EB2-6C39-4D7C-BA19-96F8AB8AAD74-54346-00000D006FF6F167.JPG") no-repeat;
        background-size: 100% 50%;
    }
    ul li:nth-child(4){
        background: url("images/A8016EB2-6C39-4D7C-BA19-96F8AB8AAD74-54346-00000D006FF6F167.JPG") no-repeat;
        background-size: auto 100px;
    }
    ul li:nth-child(5){
        background: url("images/A8016EB2-6C39-4D7C-BA19-96F8AB8AAD74-54346-00000D006FF6F167.JPG") no-repeat;
        background-size: 100px auto;
    }
    ul li:nth-child(6){
        background: url("images/A8016EB2-6C39-4D7C-BA19-96F8AB8AAD74-54346-00000D006FF6F167.JPG") no-repeat;
        /*等比拉伸 拉伸到高度和宽度都填满整个元素*/
        background-size: cover;
    }
    ul li:nth-child(7){
        background: url("images/A8016EB2-6C39-4D7C-BA19-96F8AB8AAD74-54346-00000D006FF6F167.JPG") no-repeat;
        /*等比拉伸 拉伸到高度或者宽度填满整个元素*/
        background-size: contain;
    }

</style>

<ul>
    <li>默认</li>
    <li>具体像素</li>
    <li>百分比</li>
    <li>宽度等比拉伸</li>
    <li>高度等比拉伸</li>
    <li>cover</li>
    <li>contain</li>
</ul>
  • 背景图片定位区域属性
    background-origin
    告诉系统背景图片从什么区域开始显示,默认从padding区域开始显示。
<style>
    *{
        margin: 0;
        padding: 0;
    }
    ul li{
        list-style: none;
        width: 100px;
        height: 100px;
        float: left;
        text-align: center;
        line-height: 100px;
        border: 20px dashed #000;
        padding: 50px;
        margin-left: 20px;
        background: url("images/A8016EB2-6C39-4D7C-BA19-96F8AB8AAD74-54346-00000D006FF6F167.JPG") no-repeat;
    }
    ul li:nth-child(2){
        background-origin: padding-box;
    }
    ul li:nth-child(3){
        background-origin: border-box;
    }
    ul li:nth-child(4){
        background-origin: content-box;
    }
</style> 

<ul>
    <li>默认</li>
    <li>padding</li>
    <li>border</li>
    <li>content</li>
</ul>
  • 背景绘制区域属性
    background-clip
    专门用于指定从哪个区域开始绘制背景的,默认从border区域开始绘制背景。
<style>
    *{
        margin: 0;
        padding: 0;
    }
    ul{
        margin-top: 20px;
    }
    ul li{
        list-style: none;
        width: 100px;
        height: 100px;
        float: left;
        text-align: center;
        line-height: 100px;
        border: 20px dashed #000;
        padding: 50px;
        margin-left: 20px;
        background-color: peachpuff;
    }
    ul li:nth-child(2){
        background-clip: padding-box;;
    }
    ul li:nth-child(3){
        background-clip: border-box;
    }
    ul li:nth-child(4){
        background-clip: content-box;
    }
</style>

<body>
<ul>
    <li>默认</li>
    <li>padding</li>
    <li>border</li>
    <li>content</li>
</ul>
</body>
  • 多重背景图片
    多张背景图片之间用","隔开即可,后添加的背景图片在下面。
    建议在编写多重背景时 拆开编写
<style>
    *{
        margin: 0;
        padding: 0;
    }
    div{
        width: 300px;
        height: 300px;
        border: 1px solid #000;
        margin: 20px auto;
        /*background:*/
        /*        url("images/100.jpg") no-repeat left top,*/
        /*        url("images/101.jpg") no-repeat right top,*/
        /*        url("images/102.jpg") no-repeat left bottom,*/
        /*        url("images/103.jpg") no-repeat right bottom,*/
        /*        url("images/104.jpg") no-repeat center center;*/
        background-image: url(images/100.jpg), url("images/101.jpg"), url("images/102.jpg"), url("images/103.jpg"), url("images/104.jpg");
        background-repeat: no-repeat, no-repeat, no-repeat, no-repeat, no-repeat;
        background-position: left top, right top, left bottom, right bottom, center center;
    }
</style>

<body>
    <div></div>
</body>
  • 图片太大如何局中显示
    如果图片宽度大于父元素宽度,可以使用margin: 0 -100%;使图片居中显示,但是父元素必须设置 text-align: center;属性。

相关文章

  • 05-CSS基础-背景和精灵图

    背景颜色 在CSS中可以通过background-color:属性设置标签的背景颜色。 取值:  单词  rgb ...

  • CSS基础-背景和精灵图

    背景相关属性 背景颜色 如何设置标签的背景颜色? 在CSS中可以通过background-color:属性设置标签...

  • CSS基础-背景和精灵图

    背景相关属性 背景颜色 在CSS中可以通过background-color:属性设置标签的背景颜色 取值:具体单词...

  • CSS基础--背景和精灵图

    背景相关属性 背景颜色 如何设置标签的背景颜色?在CSS中可以通过background-color:属性设置标签的...

  • 背景和精灵图

    背景颜色 background-color 作用: 在CSS中background-color:属性, 就是专门用...

  • 背景和精灵图

    背景相关属性 背景颜色 。如何设置标签的背景颜色?。在css中可以通过background-color:属性设置标...

  • 13-CSS基础-背景和精灵图

    背景相关属性 背景颜色 如何设置标签的背景颜色? 在CSS中可以通过background-color:属性设置标签...

  • CSS背景和精灵图

    1 背景颜色 1 如何设置标签的背景颜色2 设置父元素背景颜色会不会影响子元素背景颜色 2 背景图片 1 如何设置...

  • CSS背景和精灵图

    背景颜色:标签 #id .class { background-color:red; }取值类型:颜色单词、rg...

  • css背景和精灵图

    css背景和精灵图 背景 1.如何设置背景图片? 在CSS中有一个叫做background-image: url(...

网友评论

      本文标题:05-CSS基础-背景和精灵图

      本文链接:https://www.haomeiwen.com/subject/pccydctx.html