background属性详解

background属性详解

background属性主要由以下5个属性组成,

background-color::规定要使用的背景颜色

background-image:规定要使用的背景图像

background-repeat:规定背景图像的平铺方式(取值可为no-repeat, repeat-x, repeat-y, 默认值是沿xy方向均平铺)

background-position:规定背景图像的位置(可用关键词如top,bottom,left,right的组合,或者用像素px直接指定)

background-size:图像的尺寸大小

实例如下所示:

<html>
<head>
<style type="text/css">
body
{ 
background: #ff0000 url(/i/eg_bg_03.gif) no-repeat fixed center; 
}
</style>
</head>

<body>
<p>这是一些文本。</p>
<p>这是一些文本。</p>
<p>这是一些文本。</p>
<p>这是一些文本。</p>

</body>

</html>

所以在这里,background-color就是红色的背景,这个非常容易理解!!background-image需要用url()的形式来指定图片的位置。在本案例中background-repeat设置的是no-repeat。下面我们将这个值改变。

将background-repeat设置为repeat-x之后如图所示:

可以看出是沿x方向平铺。

接着我们将background-repeat设置为repeat-y,效果如下图所示:

接着我们取默认值,即把取值取消,效果图如下所示:

从上面可以看出,是沿两个方向平铺,所以全屏都是背景图像。

现在我们试着对background-position取不同的值来看看效果。

background: #ff0000 url(/i/eg_bg_03.gif) no-repeat fixed center top; 

background: #ff0000 url(/i/eg_bg_03.gif) no-repeat fixed 20px 20px;

background: #ff0000 url(/i/eg_bg_03.gif) no-repeat fixed 10% 10%; 

background-size可以改变图片尺寸的大小,但是改属性一般不放在background的简写形式里,一般都是单独书写。

background: #ff0000 url(/i/eg_bg_03.gif) no-repeat fixed 10% 10%; 
background-size:300px 300px;

本博客由博主原创,如需转载需说明出处!谢谢支持!

猜你喜欢

转载自blog.csdn.net/Allenyhy/article/details/81487890