CSS学习笔记4CSS的背景以及属性

CSS的背景

通过背景属性,可以给页面元素添加背景样式

背景属性可以设置背景颜色、背景图片、背景平铺、背景图片位置、背景图像固定等

背景颜色

background-color属性定义了元素的背景颜色

background-color:颜色值;

一般情况下元素背景颜色默认值是transparent(透明),我们也可以手动指定背景颜色为透明

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>背景颜色</title>
		<style type="text/css">
			div {
				width: 200px;
				height: 200px;
				/* background-color: transparent; 透明的 清澈的 */
				background-color: pink;
			}
		</style>
	</head>
	<body>
		<div></div>
	</body>
</html>

背景图片

background-image属性描述了元素的背景图像。实际开发常见于logo或者一些装饰性的小图片或者超大的背景图片,优点是非常便于控制位置(精灵图也是一种运用场景)

background-image:none | url (url)
参数值作用
none无背景图(默认的)
url使用绝对或相对指定背景图像

背景平铺

如果需要在HTML页面上对背景图片进行平铺,可以使用background-repeat属性

background-repeat:repeat | no-repeat | repeat-x | repeat-y
参数值作用
repeat背景图像在纵向和横向上平铺(默认的)
no-repeat背景图像不平铺
repeat-x背景图像在横向上平铺
repeat-y背景图像在纵向平铺
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>背景平铺</title>
		<style type="text/css">
			div {
				width: 800px;
				height: 800px;
				background-color: pink;
				background-image: url(../img/莱莎.jpg);
				/* 1.背景图片不平铺 */
				/* background-repeat: no-repeat; */
				/* 2.默认的情况下,背景图片是平埔的 */
				/* background-repeat: repeat; */
				/* 3.沿着x轴平铺 */
				/* background-repeat: repeat-x; */
				/* 4.沿着y轴平铺 */
				background-repeat: repeat-y;
				/* 页面元素既可以添加背景颜色也可以添加背景图片 只不过背景图片会压住背景颜色 */
			}
		</style>
	</head>
	<body>
		<div></div>
	</body>
</html>

在这里插入图片描述

背景图片位置

利用background-position属性可以改变图片在背景中的位置

background-position:x y;

参数代表的意思是:x坐标和y坐标,可以使用方位名词或者精神单位

参数值说明
length百分数|有浮点数字和单位标识符组成的长度值
positiontop | center | bottom | left | center | right 方位名词

参数是方位名词

  1. 如果指定的两个值都是方位名词,则两个值前后顺序无关,比如left top 和 top left 效果一致
  2. 如果只指定了一个方位名词,另一个值省略,则第二个值默认居中对齐
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>背景位置-方位名词</title>
		<style type="text/css">
			div {
				width: 600px;
				height: 600px;
				background-color: pink;
				background-image: url(../img/莱莎.jpg);
				background-repeat: no-repeat;
				/* background-position: 方位名词; */
				/* background-position: center top; */
				
				/* background-position: right center; */
				/* 如果是方位名词 right center 和 center right 效果是等价的 跟顺序没有关系 */
				
				/* background-position: center right; */
				/* 此时 水平一定是靠右侧对齐 第二个参数省略y轴是垂直居中显示的 */
				
				/* background-position: right; */
				/* 此时 第一个参数一定是top y轴 顶部对齐   第二个参数省略x轴是水平居中显示的 */
				background-position: top;
			}
		</style>
	</head>
	<body>
		<div></div>
	</body>
</html>

在这里插入图片描述

若想图片水平方向与垂直方向同时居中,如下

/* 水平与垂直同时居中 */
background-position: center;

在这里插入图片描述

参数是精确单位

  1. 如果参数是精确坐标,那么第一个肯定是x坐标,第二个一定是y坐标
  2. 如果只指定一个数值,那该数值一定是x坐标,另一个默认垂直居中
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>背景位置-方位名词</title>
		<style type="text/css">
			div {
				width: 600px;
				height: 600px;
				background-color: pink;
				background-image: url(../img/莱莎.jpg);
				background-repeat: no-repeat;
				/* 20px 50px; x轴一定是20 y轴一定是50 */
				/* background-position: 20px 50px; */
				/* background-position: 50px 20px; 与20 50 的不同 */
				
				background-position: 20px;
			}
		</style>
	</head>
	<body>
		<div></div>
	</body>
</html>

在这里插入图片描述

参数是混合单位

  1. 如果指定的两个值是精确单位和方位名词混合使用,则第一个值是x坐标,第二个值是y坐标
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>背景位置-混合单位</title>
		<style type="text/css">
			div {
				width: 600px;
				height: 600px;
				background-color: pink;
				background-image: url(../img/莱莎.jpg);
				background-repeat: no-repeat;
				/* 20px center 一定是x为20 y是center 等价于 background-position:20px; */
				/* background-position: 20px center; */
				
				/* 水平是居中对齐,垂直是20px */
				background-position: center 20px;
			}
		</style>
	</head>
	<body>
		<div></div>
	</body>
</html>

案例:小图标

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>背景方位案例</title>
		<style type="text/css">
			h3 {
				width: 116px;
				height: 40px;
				background-color: black;
				color: white;
				font-size: 14px;
				font-weight: 400;
				line-height: 40px;		
				background-image: url(../img/nav_search.png);
				background-repeat: no-repeat;
				background-position: left center;
				text-indent: 2em;
			}
		</style>
	</head>
	<body>
		<h3>成长守护平台</h3>
	</body>
</html>

在这里插入图片描述

案例:背景图片

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>超大背景图片案例</title>
		<style type="text/css">
			body {
				background-image: url(../img/王者背景图.png);
				background-repeat: no-repeat;
				background-position: center top;
			}
		</style>
	</head>
	<body>
	</body>
</html>

在这里插入图片描述

背景图像固定(背景附着)

background-attachment属性设置背景图像是否固定或者随着页面的其余部分滚动,后期可以制作视差滚动的效果。

background-attachement:scroll | fixed
参数作用
scroll背景图像是随对象内容滚动
fixed背景图像固定
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>背景固定</title>
		<style type="text/css">
			body {
				background-image: url(../img/琴瑟仙女%20娑娜1920x1080.jpg);
				background-repeat: no-repeat;
				background-position: center top;
				color: deeppink;
				font-size: 20px;
			}
		</style>
	</head>
	<body>
		<p>最爱琴女E</p>
		<p>最爱琴女E</p>
		<p>最爱琴女E</p>
		<p>最爱琴女E</p>
		<p>最爱琴女E</p>
		<p>最爱琴女E</p>
		<p>最爱琴女E</p>
		<p>最爱琴女E</p>
		<p>最爱琴女E</p>
		<p>最爱琴女E</p>
		<p>最爱琴女E</p>
		<p>最爱琴女E</p>
		<p>最爱琴女E</p>
		<p>最爱琴女E</p>
		<p>最爱琴女E</p>
		<p>最爱琴女E</p>
		<p>最爱琴女E</p>
		<p>最爱琴女E</p>
		<p>最爱琴女E</p>
		<p>最爱琴女E</p>
		<p>最爱琴女E</p>
		<p>最爱琴女E</p>
		<p>最爱琴女E</p>
		<p>最爱琴女E</p>
		<p>最爱琴女E</p>
		<p>最爱琴女E</p>
		<p>最爱琴女E</p>
		<p>最爱琴女E</p>
		<p>最爱琴女E</p>
		<p>最爱琴女E</p>
		<p>最爱琴女E</p>
		<p>最爱琴女E</p>
	</body>
</html>

在这里插入图片描述

设置固定后效果如下

body {
				background-image: url(../img/琴瑟仙女%20娑娜1920x1080.jpg);
				background-repeat: no-repeat;
				background-position: center top;
				/* 把背景图片固定住 */
				background-attachment: fixed;
				color: deeppink;
				font-size: 20px;
			}

在这里插入图片描述

背景属性复合写法

为了简化背景属性的代码,我们可以将这些属性合并简写在同一个属性background中,从而节约代码量

使用简写属性是,没有特定的书写顺序,一般习惯约定顺序如下:

background:背景颜色 背景图片地址 背景平铺 背景图像滚动 背景图片位置;

background: transparent url(image.jpg) repeat-y fixed top;
body {
				/* background-image: url(../img/琴瑟仙女%20娑娜1920x1080.jpg);
				background-repeat: no-repeat;
				background-position: center top;
			    把背景图片固定住 
				background-attachment: fixed; */
				color: deeppink;
				font-size: 20px;
				background: black url(../img/琴瑟仙女%20娑娜1920x1080.jpg) no-repeat fixed center top;
			}

背景颜色半透明

CSS3为我们提供了背景颜色半透明的效果

background: rgba(0,0,0,0.3);
  1. 最后一个参数是aipha透明度,取值范围在0~1之间
  2. 0.3的0可以省略,写为background:rgba(0,0,0,.3);
  3. 注意:背景半透明是指盒子背景半透明,盒子里面的内容不受影响
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>背景透明写法</title>
		<style type="text/css">
			div {
				width: 600px;
				height: 600px;
				/* background-color: black; */
				background: rgba(0,0,0,0.6);
			}
		</style>
	</head>
	<body>
		<div></div>
	</body>
</html>

背景总结

属性作用
background-color背景颜色预定义的颜色值/十六进制/RGB代码
background-image背景图片url(图片路径)
background-repeat是否平铺repeat/no-repeat/repeat-x/repeat-y
background-position背景位置length/position 分别是x和y坐标
background-attachment背景附着scroll(背景滚动)/ fixed(背景固定)
背景简写书写更简单背景颜色 背景图片地址 背景平铺 背景滚动 背景位置
背景色半透明背景颜色半透明background:rgba(0,0,0,0.3); 后面必须是4个值

案例:五彩导航

练习价值:

  1. 链接属于行内元素,但是此时需要宽度高度,因此需要模式转换
  2. 里面文字需要水平居中和垂直居中,因此需要单行文字垂直居中的代码
  3. 链接里面需要设置背景图片,因此需要用到背景的相关属性设置
  4. 鼠标经过变化背景图片,因此需要用到链接伪类选择器
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>五彩导航</title>
		<style type="text/css">
			.nav a {
				display: inline-block;
				width: 120px;
				height: 67px;
				background-color: deeppink;
				text-align: center;
				line-height: 60px;
				text-decoration: none;
			}
			.nav .bg1 {
				background: url(../img/904950.png) no-repeat;
			}
			.nav .bg1:hover {
				background-image: url(../img/850354.jpg);
			}
			.nav .bg2 {
				background: url(../img/1050919.png) no-repeat;
			}
			.nav .bg2:hover {
				background-image: url(../img/1050135.jpg);
			}
			.nav .bg3 {
				background: url(../img/1053217.jpg) no-repeat;
			}
			.nav .bg3:hover {
				background-image: url(../img/850354.jpg);
			}
			.nav .bg4 {
				background: url(../img/1050135.jpg) no-repeat;
			}
			.nav .bg4:hover {
				background-image: url(../img/1050919.png);
			}
			.nav .bg5 {
				background: url(../img/850354.jpg) no-repeat;
			}
			.nav .bg5:hover {
				background-image: url(../img/904950.png);
			}
		</style>
	</head>
	<body>
		<div class="nav">
			<a href="#" class="bg1">五彩导航</a>
			<a href="#" class="bg2">五彩导航</a>
			<a href="#" class="bg3">五彩导航</a>
			<a href="#" class="bg4">五彩导航</a>
			<a href="#" class="bg5">五彩导航</a>
		</div>
	</body>
</html>

在这里插入图片描述

鼠标经过第三个图片效果,其他图片效果一致

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值