前端路线--HTML篇(day05)

本文详细介绍了CSS中的背景属性,包括背景颜色、背景图片、背景平铺、背景定位、背景附着、精灵图、背景图片大小、背景简写、背景透明以及列表样式和盒模型的边框、内边距和外边距。通过实例代码展示了如何实现各种效果,帮助开发者更好地理解和运用这些属性。
摘要由CSDN通过智能技术生成

背景颜色

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
	<style>
		.box{
			width: 200px;
			height: 200px;

			/*背景颜色三种表示方式,同颜色*/
			background-color: red;
			background-color: #f00;
			background-color: rgb(255,0,0);
		}
	</style>
</head>
<body>
	<div class="box">
		
	</div>
</body>
</html>

背景图片

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
	<style>
		.box1{
			width: 600px;
			height: 600px;
			border:1px solid #000;
			float: left;
		}
		.box2{
			width: 600px;
			height: 600px;
			border:1px solid #000;
			float: left;

			background-image: url(img/aa.jpeg);
			/*背景图片*/
			/*背景图片不占用盒子空间*/
		}
	</style>
</head>
<body>
	<div class="box1">
		<img src="img/aa.jpeg" alt="">Lorem ipsum dolor sit amet consectetur adipisicing elit. Delectus nisi explicabo dolor porro enim, eum maiores aperiam, corrupti doloremque voluptatem assumenda est veritatis, illo quaerat eius? Nihil animi quis, saepe?
	</div>
	<div class="box2">
		Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quaerat unde quibusdam nisi, quas qui dignissimos magnam, deserunt a voluptatem asperiores hic, vel necessitatibus quis sequi quisquam. Vero eius itaque est.
	</div>
</body>
</html>

背景平铺

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
	<style>
		.box2{
			width: 600px;
			height: 600px;
			border:1px solid #000;

			background-image: url(img/aa.jpeg);
			/*背景图片*/
			/*背景图片不占用盒子空间*/

			background-repeat: no-repeat;
			/*背景图片是否平铺:不平铺*/
		}
	</style>
</head>
<body>
	<div class="box2">
		
	</div>
</body>
</html>

背景定位

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
	<style>
		.box2{
			width: 600px;
			height: 600px;
			border:1px solid #000;

			background-image: url(img/aa.jpeg);
			/*背景图片*/
			/*背景图片不占用盒子空间*/

			background-repeat: no-repeat;
			/*背景图片是否平铺:不平铺*/


			background-position: 0px 0px;
			/*背景图片定位:x轴坐标、Y轴坐标*/
			background-position: 100% 50%;
							/*支持百分比*/
			background-position: center bottom;
							/*支持单词坐标:top、left、right、bottom、center*/

			background-position: 100px;
			/*如果只写一个值,表示X轴坐标,Y轴默认上下居中*/
		}
	</style>
</head>
<body>
	<div class="box2">
		
	</div>
</body>
</html>

背景附着

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
	<style>
		.box1{
			width: 800px;
			height: 400px;
			margin: 100px auto;
			background-color: #f00;
		}
		.box2{
			width: 800px;
			height: 400px;
			margin: 100px auto;
			background-color: #f0f;
		}
		.box3{
			width: 800px;
			height: 400px;
			margin: 100px auto;
			background-color: #ff0;
		}

		body{
			background-image: url(img/banner.jpg);

			background-attachment: fixed;
			/*背景图片附着*/
		}
	</style>
</head>
<body>
	<div class="box1"></div>
	<div class="box2"></div>
	<div class="box3"></div>
</body>
</html>

精灵图

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
	<style>
		.box1{
			width: 16px;
			height: 16px;
			border:1px solid #000;
			background-image: url(img/spirit.png);
			background-position: -19px -44px;
		}
		.box2{
			width: 160px;
			height: 160px;
			border:1px solid #000;
			background-image: url(img/abcd.jpg);
			background-position: -366px -270px;
		}
		.box1:hover{
			background-position: -19px -65px;
		}
		/*为什么使用精灵图?*/
		/*为了有效地减少服务器接受和发送请求的次数,提高页面的加载速度*/
	</style>
</head>
<body>
	<div class="box1"></div>
	<div class="box2"></div>
</body>
</html>

背景图片大小

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
	<style>
		.box1{
			width: 700px;
			height: 700px;
			border:1px solid #000;

			background-image: url(img/aa.jpeg);
			background-repeat: no-repeat;
			
			background-size: 300px;
			/*背景图片大小:宽度、高度*/
			/*一般只调整宽度、高度等比例自动缩放*/
			background-size: 100%;
			background-size: contain;
			/*等比例放大图片。只有一个边铺满就停止*/
			background-size: cover;
			/*等比例放大图片。必须两个边都铺满才停止*/
		}
	</style>
</head>
<body>
	<div class="box1"></div>
</body>
</html>

背景简写

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
	<style>
		.box1{
			width: 700px;
			height: 700px;
			border:1px solid #000;

			/*背景简写:背景图片 背景平铺 背景定位*/
			background: url(img/aa.jpeg) no-repeat 100px 100px;

			/*background-image: url(img/aa.jpeg);
			background-repeat: no-repeat;
			background-position: 100px 100px;*/
		}
	</style>
</head>
<body>
	<div class="box1"></div>
</body>
</html>

背景透明

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
	<style>
		body{
			background-image: url(img/banner.jpg);
		}
		.box1{
			width: 200px;
			height: 200px;
			background-color: #f00;

			opacity: 0.5;
			/*透明度:取值范围0-1*/
			/*背景以及内容区域一起透明*/
		}
		.box2{
			width: 200px;
			height: 200px;
			background-color: rgba(255,0,0,0.5);
			/*只透明背景,不透明内容*/
		}
	</style>
</head>
<body>
	<div class="box1">hello world</div>
	<div class="box2">hello world</div>
</body>
</html>

列表样式

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
	<style>
		ul{
			list-style-image: url(img/icon.jpg);
			/*把列表前面的小黑点替换成图片*/

			list-style-position: inside;
			/*把列表前面的小黑点移动到内容区域*/

			list-style-type: none;
			/*把列表前面的小黑点清除*/

		}
		ul li{
			border-bottom: 1px solid #000;
		}
	</style>
</head>
<body>
	<ul>
		<li>无序列表</li>
		<li>无序列表</li>
		<li>无序列表</li>
		<li>无序列表</li>
		<li>无序列表</li>
	</ul>
</body>
</html>

盒模型–边框

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
	<style>
		/*盒子模型:content内容、padding内边距、border边框、margin外边距*/

		/*内容区域:由宽高撑开*/


		.box{
			width: 200px;
			height: 200px;

			/*边框总写*/

			/*边框三要素:粗细、风格、颜色*/
			/*border: 10px dotted red;*/
			/*solid 实线 、 dashed 虚线 、 dotted 点线 、 double 双实线*/

			/*边框分写:单独指定某一个边*/
			border-left: 10px solid #f00;
			border-top: 10px dashed #ff0;
			border-right: 10px dotted #0f0;
			border-bottom: 10px double #0ff;
		}
	</style>
</head>
<body>
	<div class="box">
		
	</div>	
</body>
</html>

盒模型–内边距

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
	<style>
		/*盒子模型:content内容、padding内边距、border边框、margin外边距*/

		/*内容区域:由宽高撑开*/


		/*内边距:内容和边框之间的距离*/
		.box{
			width: 200px;
			height: 200px;
			border: 1px solid #000;

			/*缺点:会撑大盒子模型*/
			/*内边距*/
			padding-top: 20px;
			padding-right: 30px;
			padding-bottom: 40px;
			padding-left: 50px;

			/*内边距简写*/
			padding: 20px 30px 40px 50px;
					/*上   右   下   左*/
			padding: 20px 30px 40px;
					/*上  左右  下*/
			padding: 20px 30px;
					/*上下  左右*/
			padding: 20px;
					/*上下左右*/
		}
	</style>
</head>
<body>
	<div class="box">
		hello world
	</div>	
</body>
</html>

盒模型–外边距

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
	<style>
		/*盒子模型:content内容、padding内边距、border边框、margin外边距*/

		/*内容区域:由宽高撑开*/


		/*外边距:边框以外的距离*/
		.box{
			width: 200px;
			height: 200px;
			border: 1px solid #000;

			/*外边距*/
			margin-left: 50px;
			margin-top: 100px;
			margin-bottom: 100px;

			/*外边距简写*/
			margin: 20px 30px 40px 50px;
					/*上   右   下   左*/
			margin: 20px 30px 40px;
					/*上  左右  下*/
			margin: 20px 30px;
					/*上下  左右*/
			margin: 20px;
					/*上下左右*/
		}



		.box1{
			width: 200px;
			height: 200px;
			background-color: #f00;
		}
	</style>
</head>
<body>
	<div class="box">
		hello world
	</div>	
	<div class="box1">
		
	</div>
</body>
</html>

盒模型–快速居中

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
	<style>
		/*盒子模型:content内容、padding内边距、border边框、margin外边距*/

		/*内容区域:由宽高撑开*/

		.box{
			width: 200px;
			height: 200px;
			border: 1px solid #000;


			margin: 20px auto 40px;
			/*左右可以替换成auto*/
		}



		.box1{
			width: 200px;
			height: 200px;
			background-color: #f00;

			margin: 20px auto;
					/*上下  左右*/
		}
	</style>
</head>
<body>
	<div class="box">
		hello world
	</div>	
	<div class="box1">
		
	</div>
</body>
</html>

清除浏览器默认的内外边距

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
	<style>
		/*清除浏览器默认样式*/
		*{
			/*公共样式*/
			margin: 0px;
			padding: 0px;
			list-style-type: none;
			text-decoration: none;
			color: #000;
		}
		ul{
			margin-left: 100px;
		}
	</style>
</head>
<body>
	<h1>hello world</h1>
	<p>hello world</p>
	<a href="">hello world</a>
	<ul>
		<li>无序列表</li>
		<li>无序列表</li>
		<li>无序列表</li>
		<li>无序列表</li>
	</ul>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值