CSS背景

时间:2020-07-11
本文章向大家介绍CSS背景,主要包括CSS背景使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

背景颜色

渐变色背景:可以做出各种各样的图形,复杂的图形可以使用这个方案,比如=>这样的图形

多背景叠加

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>background</title>
    <style>
        /* body{
            background:yellow;
        } */
        .c1{
            height:90px;
            /* 颜色的格式:可以使用 hsl对人眼更友好一点*/
            /* 1. red white 直接使用单词 */
            /* background: grey; */
            /* 2. #FF0000 16进制数 */
            /* background: #ff0000; */
            /* 3. rgb(255, 0 ,0) */
            /* background: rgb(240, 164, 0); */
            /* 4. rgba(255, 0, 0, 0.3) 比rgb加了一个透明度 */
            /* background: rgba(255, 0, 0, .1); */
            /* 5. hsl(0色项, 100% 饱和度,50% 亮度),色项是0-360 */
            /* background: hsl(0, 10%, 50%); */
            /* 6. hsla(0色项, 100% 饱和度,50% 亮度,0.1透明度),色项是0-360 */
            /* background:hsla(0, 100%, 90%, .3); */
            
            /* 背景还可以使用背景图 */
            /* 可以看到默认是从左上角开始平铺的,并且还会重复平铺,可以通过设置来改变这个默认行为 */
            background:url(./demo3-5-1.png);
        }
        .c2{
            height:90px;
            /* 背景颜色渐变,从什么颜色变到什么颜色,这是比较老的写法,使用了left,并且有-webkit */
            /* background: -webkit-linear-gradient(left, red, green); */
            /* 这个背景颜色渐变是最新的写法,没有了-webkit,并且有to right, 效果是一样的 */
            /* background: linear-gradient(to right, red, green); */
            /* 直接写to right, to left, to top的情况背景少,更多的是设置角度,角度指定是渐变的方向 */
            /* 0deg默认从下to上的,也就是to top */
            /* background: linear-gradient(to top, red, green); */
            /* 还是让UXD给设计图吧 */
            /* background: linear-gradient(135deg, red 0, green 10%, yellow 50%, blue 100%); */
            /* 多个背景叠加 */
            /* 可以使用background的渐变做出各种图形 */
            background: linear-gradient(135deg, transparent 0, transparent 49.5%, green 49.5%, green 50.5%, transparent 50.5%, transparent 100%),
                linear-gradient(45deg, transparent 0, transparent 49.5%, red 49.5%, red 50.5%, transparent 50.5%, transparent 100%);
            /* background-size: 30px 30px; */
        }
    </style>
</head>
<body>
    <div class="c1">

    </div>
    <div class="c2">

    </div>
</body>
</html>

 

 

 

使用图片作为背景

雪碧图:性能优化

 

 

 可以同时设置背景颜色和背景图,可以设置背景图的各种属性,比如背景图的是否重复显示,背景图的大小,背景图的位置。

 

base64:性能优化

多分辨率适配:

这里需要注意的是,web端的一个像素可能对应着移动端的几个像素,所以在web端1px能够显示好的图片到移动端就会变的模糊。解决办法就是使用高清图片,也就是使用图片压缩,把大的图片压缩成几倍小的图片。

比如本来的图片大小是200px * 400px, 那么2倍图就是100px * 200px(通过background-size实现),这就是图片的几倍图的由来

 

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>background</title>
    <style>
        .c1{
            height:300px;
            /* 默认图片是repeat的 */
            background: url('./demo3-5-1.png');
            /* 可以在设置背景图片时同时设置背景颜色 */
            background: red url('./demo3-5-1.png'); /* 因为这里的图片是不透明的,所以看不到red */

            /* 使用base64来优化图片,这样不会产生http请求 */
            /* background:red url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAHoAAAA2CAYAAAAIw43zAAAMJGlDQ1BJQ0MgUHJvZmlsZQAASImVlwdUU0kXx+eVJCQktEAEpITeBOlVeo1UqYKNkAQSSgiBoGJHFxVYCyoWrMiqiG0tgCwqYlcWwd4XRFCUdbFgQ+WbJICu+5XzzTnz5nfu3Lnzv/PmzXkDgHIUWyTKRFUAyBLmiaOD/ZiTE5OYpE5ABtpADRgAYzYnV+QbFRUGYBlp/17e3QKItL1uLY31z/7/WlS5vFwOAEgU5BRuLicL8lEAcGeOSJwHAKEP2o1m5okgE6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uLNt0aTSucXQDH59lDWTiZECBoNuLG9R40LAma1lCsLcwlmyoeNMjmsY5sHp84/SP5w3Bajx38/w/uficD7QAtITIeTUejRK0DHS9xom99SFdlpaNiX4gg/hI6HrSietK+Mxd0rRQTtLFoKooWdTDQa1qjbVr6kXX0oGgdxfwoLjd+xEOno07EAWlBPpj6tYCv4maotoajvWI/1IJ2V9Dqj+VDH+Et8AAb0a5EE6klUylNSUVNZpio12oZx/1Hvbhikh95NoeSyAgcz0hAdJg3OUO3Cc7QfLj60sPQh4njDHe9QUm3mTdaKdVkp6N0j3AGWO5w9ztlp+5Vb9zghvW6MHFXAOnPUpNjVo7keXpyqB9t70fwyvgH0/CCLkCNxPBAJOoikBwVATW3o54joNslQUtNxbY8pob6cPv1MG7OqMiMoEFlTgrStX6SKbr7nQy0pIwgU2wonpanIUywAZIxlWmWB21zqC0gN12xlDcztu2BQx4M9JrXaJ6byluDe2UHoNM4qMybOCk5B21zrCdHJr3gyCQfbBaTJi+EB/Mr+38Ieje5697JabS2mzEujKhQtTcWpWlR0Pi6Bj5nNmFkkEztfWNoMLnevVVnJqE4RSvanAlfbjWPw2DohSk5g+zWg7guywC5yGmlv8jhpm4uAivIKpDq74f8XRroQjQIVC9Dty4uYNpsxohxBl0Tv9EyJ+vupMMLp2K2IX17ABd3ataMz0MTuDq+6MSPvouBptdK0ZYMtKLx0SfPQNNrpWhLBlrR+OiTZ6DptVK0JQOtaHz0yTPQ9Fop2pKBVjQ++uQZaHqtFG3JQCsaH33yDDS9Voq2ZKAVjY8+eQaaXitFW/4FdhzkzmcYrBcAAAAASUVORK5CYII=); */
            /* no-repeat是不要平铺,可以设置repeat-x,repeat-y */
            background-repeat: no-repeat;

            /* 如果设置了no-repeat,可以设置背景图的位置,background-position的值为在横向什么位置,在众向什么位置 */
            background-position: center top;
            /* 也可以直接设置px来实现定位 */
            /* background-position: 20px 30px; */
            /* 可以设置背景图片的size, */
            background-size:100px 50px;
        }
        .c2{
            /* background: red; */
            width:40px;
            height:40px;
            /* 如果容器的大小比背景图的自身大小要小,会默认从左上角进行平铺。 */
            /* 目标是把小图标作为背景 */
            /* 这里c2 c3就是把许多张图片放到一张图片上以减少http请求,然后再通过background-position来进行偏移设置来使用确定的图片 */
            background:url(./demo3-5-2.png) no-repeat;
            background-position: -17px -5px;
            background-size: 261px 113px;
        }
        .c3{
            width:40px;
            height:40px;
            
            background:url(./demo3-5-2.png) no-repeat;
            background-position: -169px -23px;
            background-size: 261px 113px;
        }
    </style>
</head>
<body>
    <div class="c1">

    </div>
    <div class="c2">

    </div>
    <div class="c3">

    </div>
</body>
</html>

 

$flag 上一页 下一页