css中background常用属性详解

background常用属性详解

background-color

为元素设置背景颜色

描述
color_name规定颜色值为颜色名称的背景颜色(比如 red)。
hex_number规定颜色值为十六进制值的背景颜色(比如 #ff0000)。
rgb_number规定颜色值为 rgb 代码的背景颜色(比如 rgb(255,0,0))。
transparent默认。背景颜色为透明。
inherit规定应该从父元素继承 background-color 属性的设置。
background-color: #ffff00;
background-color: yellow;
background-color: rgb(255,255,0);
background-color: transparent;

展示效果

在这里插入图片描述

background-image

为元素设置背景图像

描述
url(‘URL’)指向图像的路径。
none默认值。不显示背景图像。
inherit规定应该从父元素继承 background-image 属性的设置。
background-image:url(img/banner04.jpg) ;

展示效果

在这里插入图片描述

background-repeat

设置是否及如何重复背景图像;默认背景图像在水平和垂直方向上重复。

描述
repeat默认。背景图像将在垂直方向和水平方向重复。
repeat-x背景图像将在水平方向重复。
repeat-y背景图像将在垂直方向重复。
no-repeat背景图像将仅显示一次。
inherit规定应该从父元素继承 background-repeat 属性的设置。
<style>
    .box{
        float: left;
        width: 200px;
        height: 100px;
        margin-right:20px;
        background-image:url(img/banner04.jpg);
        background-size: 100px 50px;
        border: 1px solid red;    
    }
    .box1{
        background-repeat: no-repeat;				
    }
    .box2{
        background-repeat: repeat-x;
    }
    .box3{
        background-repeat: repeat-y;
    }
    .box4{
        background-repeat: repeat;
    }
</style>
<body>
    <div class="main">
        <div class="box box1"></div>
        <div class="box box2"></div>
        <div class="box box3"></div>
        <div class="box box4"></div>
    </div>

</body>

展示效果

在这里插入图片描述

background-position

设置背景图像的起始位置

描述
top left |top center|top right|center left |center center|center right|bottom left |bottom center| bottom right如果您仅规定了一个关键词,那么第二个值将是"center"。默认值:0% 0%。
x% y%第一个值是水平位置,第二个值是垂直位置。左上角是 0% 0%。右下角是 100% 100%。如果您仅规定了一个值,另一个值将是 50%。
xpos ypos第一个值是水平位置,第二个值是垂直位置。左上角是 0 0。单位是像素 (0px 0px) 或任何其他的 CSS 单位。如果您仅规定了一个值,另一个值将是50%。您可以混合使用 % 和 position 值。
<style>
    .box{
        float: left;
        width: 200px;
        height: 100px;
        margin-right:20px;
        background-image:url(img/banner04.jpg);
        background-size: 100px 50px;
        background-repeat: no-repeat;	
        border: 1px solid red;         
}
    .box1{
        /* background-position: center center; */
        background-position: center; 
    }
    .box2{
        /* background-position: 50% 50%; */
        background-position: 50% ;
    }
    .box3{
    	background-position: 50px 25px;
    }
    .box4{
    	background-position: 50% 25px;
    }
</style>
<body>
    <div class="main">
        <div class="box box1"></div>
        <div class="box box2"></div>
        <div class="box box3"></div>
        <div class="box box4"></div>
    </div>
</body>

展示效果

在这里插入图片描述

background-size

规定背景图像的尺寸

描述
length设置背景图像的高度和宽度。第一个值设置宽度,第二个值设置高度。如果只设置一个值,则第二个值会被设置为 “auto”。
percentage以父元素的百分比来设置背景图像的宽度和高度。第一个值设置宽度,第二个值设置高度。如果只设置一个值,则第二个值会被设置为 “auto”。
cover把背景图像扩展至足够大,以使背景图像完全覆盖背景区域。背景图像的某些部分也许无法显示在背景定位区域中。
contain把图像图像扩展至最大尺寸,以使其宽度和高度完全适应内容区域。
<style>
    .box{
        float: left;
        width: 200px;
        height: 100px;
        margin-right:20px;
        background-image:url(img/banner04.jpg);
        background-repeat: no-repeat;	
        border: 1px solid red;
    }
    .box1{
        /* background-size: 100px 50px; */
        background-size: 50% 50%;
    }
    .box2{
        /* background-size: 100px; */
        background-size: 50%;
    }
    .box3{
        background-size: cover;
    }
    .box4{
        background-size: contain;
    }

</style>
</head>
<body>
    <div class="main">
        <div class="box box1"></div>
        <div class="box box2"></div>
        <div class="box box3"></div>
        <div class="box box4"></div>
    </div>
</body>

展示效果

在这里插入图片描述

background-attachment

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

描述
scroll默认值。背景图像会随着页面其余部分的滚动而移动。
fixed当页面的其余部分滚动时,背景图像不会移动。
inherit规定应该从父元素继承 background-attachment 属性的设置。
background-attachment: fixed;
background-attachment: scroll; 

效果

在这里插入图片描述

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值