CSS 基础

1、CSS 的必要性

  • 使网页美观
  • 将 Html 页面的内容与样式分离
  • 提高 web 开发的工作效率

2、引入样式方式 — 外联式

  • 原 Html 代码
<!DOCTYPE html>
<head>
	<title>Document</title>
</head>
<body>
	<div>
		<h1>什么是快乐星球?</h1>
		<p>如果你想知道什么是快乐星球的话、我现在就带你研究</p>
	</div>
</body>
</html>
  • 在浏览器中打开

在这里插入图片描述

新建后缀为(css)的文件

  • 此文件代表将标签(h1)颜色改为红色
  • 将标签(p)颜色改为绿色
h1{
	color: red;
}

p{
	color: green;
}

将 CSS 代码引入 Html 代码中

  • 在头部里面添加代码块(link)
  • 参数(href)是文件的路径(此处为新建的 CSS 文件)
<!DOCTYPE html>
<head>
	<title>Document</title>
	<link rel="stylesheet" type="text/css" href="1.css">
</head>
<body>
	<div>
		<h1>什么是快乐星球?</h1>
		<p>如果你想知道什么是快乐星球的话、我现在就带你研究</p>
	</div>
</body>
</html>
  • 在浏览器中打开

在这里插入图片描述

3、引入样式方式 — 内嵌式

  • 在头部加入单标签(style)
  • 其中定义方式和在 CSS 文件里一样
<!DOCTYPE html>
<head>
	<title>Document</title>
	<style type="text/css">
		h1{
			color: red;
			font-size: 50px
		}
		p{
			color: green;
			font-size: 20px
		}
	</style>
</head>
<body>
	<div>
		<h1>什么是快乐星球?</h1>
		<p>如果你想知道什么是快乐星球的话、我现在就带你研究</p>
	</div>
</body>
</html>
  • 在浏览器中打开

在这里插入图片描述

4、引入样式方式 — 行内样式

  • 定义链接标签(a)
  • 标签属性(style)用来定义样式
  • 不同的样式之间用分号分开
<!DOCTYPE html>
<head>
	<title>Document</title>
</head>
<body>
	<a href="http://www.4399.com/" style="color: pink; font-size: 200px">4399</a>
</body>
</html>
  • 在浏览器中打开

在这里插入图片描述

5、常用选择器(采用外联式)

元素选择符

  • Html 文件内容
<!DOCTYPE html>
<head>
	<title>Document</title>
	<link rel="stylesheet" type="text/css" href="1.css">
</head>
<body>
	<div>
		<h1>鲜血</h1>
	</div>
</body>
</html>
  • CSS 文件内容
h1{
	color: red;
	font-family: 宋体;
	font-size: 50px;
}
  • 在浏览器中打开

在这里插入图片描述
ID 选择符

  • Html 文件内容
<!DOCTYPE html>
<head>
	<title>Document</title>
	<link rel="stylesheet" type="text/css" href="1.css">
</head>
<body>
	<div>
		<h1 id="red">鲜血</h1>
		<h1>鲜血</h1>
	</div>
</body>
</html>
  • CSS 文件内容
  • 语法:#ID
h1{
	color: red;
	font-family: 宋体;
	font-size: 50px;
}
#red{
	color: pink;
	font-family: 微软雅黑;
	font-size: 100px;
}
  • 在浏览器中打开

在这里插入图片描述
类选择符

  • Html 文件内容
<!DOCTYPE html>
<head>
	<title>Document</title>
	<link rel="stylesheet" type="text/css" href="1.css">
</head>
<body>
	<div>
		<h1 class="su">苹果</h1>
		<p class="su">菠萝</p>
	</div>
</body>
</html>
  • CSS 文件内容
  • 语法:.class名
.su{
	color: green;
	font-size: 50px;
}
  • 在浏览器中打开

在这里插入图片描述
通配选择符

  • Html 文件内容
<!DOCTYPE html>
<head>
	<title>Document</title>
	<link rel="stylesheet" type="text/css" href="1.css">
</head>
<body>
	<div>
		<h1>苹果</h1>
		<p>菠萝</p>
		<a href="http://www.4399.com/">4399</a>
	</div>
</body>
</html>
  • CSS 文件内容
  • 参数(margin)和参数(padding)是必须的
*{
	margin:0;
	padding:0;
	color: green;
	font-size: 50px;
}
  • 在浏览器中打开

在这里插入图片描述
群组选择器

  • Html 文件内容
<!DOCTYPE html>
<head>
	<title>Document</title>
	<link rel="stylesheet" type="text/css" href="1.css">
</head>
<body>
	<div>
		<h1>苹果</h1>
		<p>菠萝</p>
		<a href="http://www.4399.com/">4399</a>
		<h1 id="red">红色</h1>
	</div>
</body>
</html>
  • CSS 文件内容
h1,#red,p{
	color: red;
	font-size: 40px;
}
  • 在浏览器中打开

在这里插入图片描述
包含选择器

  • Html 文件内容
<!DOCTYPE html>
<head>
	<title>Document</title>
	<link rel="stylesheet" type="text/css" href="1.css">
</head>
<body>
	<div>
		<h1>苹果</h1>
		<p>菠萝</p>
		<h1 id="red">红色</h1>
	</div>
</body>
</html>
  • CSS 文件内容
div h1{
	color: red;
	font-size: 40px;
}
  • 在浏览器中打开
  • 语法:父选择符 子选择符

在这里插入图片描述
伪类选择器

  • Html 文件内容
<!DOCTYPE html>
<head>
	<title>Document</title>
	<link rel="stylesheet" type="text/css" href="1.css">
</head>
<body>
	<div>
		<a href="http://www.4399.com/">4399</a>
	</div>
	<div>
		<a href="http://www.7k7k.com/">7k7k</a>
	</div>
</body>
</html>
  • CSS 文件内容
  • 原链接颜色(green)
  • 鼠标覆盖链接颜色(pink),字体(100px)
a:link{color: red}
a:visited{color: green}
a:hover{color: pink;font-size: 100px}
  • 在浏览器中打开

在这里插入图片描述

6、选择器的优先级

  • 行内样式:1000
  • ID 选择器:100
  • 类选择器:10
  • 标签选择器:1

标签选择器

<!DOCTYPE html>
<head>
	<title>Document</title>
	<style type="text/css">
		h1{
			color: pink;
		}
	</style>
</head>
<body>
	<h1>你是猪!</h1>
</body>
</html>
  • 在浏览器中打开

在这里插入图片描述
类选择器

<!DOCTYPE html>
<head>
	<title>Document</title>
	<style type="text/css">
		h1{
			color: pink;
		}
		.pig{
			color: red;
		}
	</style>
</head>
<body>
	<h1 class="pig">你是猪!</h1>
</body>
</html>
  • 在浏览器中打开

在这里插入图片描述
ID 选择器

<!DOCTYPE html>
<head>
	<title>Document</title>
	<style type="text/css">
		h1{
			color: pink;
		}
		.pig{
			color: red;
		}
		#ani{
			color: green;
		}
	</style>
</head>
<body>
	<h1 class="pig" id="ani">你是猪!</h1>
</body>
</html>
  • 在浏览器中打开

在这里插入图片描述
行内样式

<!DOCTYPE html>
<head>
	<title>Document</title>
	<style type="text/css">
		h1{
			color: pink;
		}
		.pig{
			color: red;
		}
		#ani{
			color: green;
		}
	</style>
</head>
<body>
	<h1 class="pig" id="ani" style="color: black">你是猪!</h1>
</body>
</html>
  • 在浏览器中打开

在这里插入图片描述

7、常用样式举例(文本)

  • color 文字颜色
  • font-size 设置文字大小
  • font-family 字体种类
  • font-style 是否倾斜 (italic-倾斜 normal-正常(通常用来去倾斜) )
  • font-weight (是否加粗 bold-加粗 normal-正常 )
  • text-decoration(下划线-underline 上划线-overline 删除线-line-through)
<!DOCTYPE html>
<head>
	<title>Document</title>
	<style type="text/css">
		h1{
			font-size: 30px;
		}
	</style>
</head>
<body>
	<div>
		<h1 style="color: red">鲜甜草莓的秘密</h1>
		<h1 style="font-size: 20px">鲜甜草莓的秘密</h1>
		<h1 style="font-family: '宋体">鲜甜草莓的秘密</h1>
		<h1 style="font-style: italic;">鲜甜草莓的秘密</h1>
		<h1 style="font-weight: normal;">鲜甜草莓的秘密</h1>
		<h1 style="text-decoration: line-through;">鲜甜草莓的秘密</h1>
	</div>
</body>
</html>
  • 在浏览器中打开

在这里插入图片描述

8、常用样式举例(列表)

  • list-style-type(disc-实心圆 square-正方形 circle-空心圆 none-无)
  • list-style-image(url-相对路径)
  • list-style-position(outside(外边),inside(里边))
  • list-style(none-最常用)

list-style-type(普通标识)

<!DOCTYPE html>
<head>
	<title>Document</title>
	<style type="text/css">
		ul li{
			list-style-type: circle;
		}
	</style>
</head>
<body>
	<ul>
		<li>In love folly is always sweet.</li>
		<li>You may be out of my sight, but never out of my mind.</li>
		<li>Love is not a maybe thing. You know when you love someone.</li>
		<li>I’ll think of you every step of the way.</li>
	</ul>
</body>
</html>
  • 在浏览器中打开

在这里插入图片描述
list-style-image(图片标识)

<!DOCTYPE html>
<head>
	<title>Document</title>
	<style type="text/css">
		ul li{
			list-style-image: url(2.jpg);
		}
	</style>
</head>
<body>
	<ul>
		<li>In love folly is always sweet.</li>
		<li>You may be out of my sight, but never out of my mind.</li>
		<li>Love is not a maybe thing. You know when you love someone.</li>
		<li>I’ll think of you every step of the way.</li>
	</ul>
</body>
</html>
  • 在浏览器中打开

在这里插入图片描述
list-style-position(标识的位置)

  • 定义边框(border)
  • 颜色属性(rgb)
<!DOCTYPE html>
<head>
	<title>Document</title>
	<style type="text/css">
		ul li{
			list-style-image: url(2.jpg);
			list-style-position: inside;
			border: 1px rgb(1,222,43) solid;
		}
	</style>
</head>
<body>
	<ul>
		<li>In love folly is always sweet.</li>
		<li>You may be out of my sight, but never out of my mind.</li>
		<li>Love is not a maybe thing. You know when you love someone.</li>
		<li>I’ll think of you every step of the way.</li>
	</ul>
</body>
</html>
  • 在浏览器中打开

在这里插入图片描述
特别注意

  • 利用通配符将属性(padding)和属性(margin)均改为 0 像素
  • 此时位置属性(list-style-position)是必须的
<!DOCTYPE html>
<head>
	<title>Document</title>
	<style type="text/css">
		*{
			padding: 0px;
			margin: 0px;
		}
		ul li{
			list-style-image: url(2.jpg);
			list-style-position: inside;
			border: 1px rgb(1,222,43) solid;
		}
	</style>
</head>
<body>
	<ul>
		<li>In love folly is always sweet.</li>
		<li>You may be out of my sight, but never out of my mind.</li>
		<li>Love is not a maybe thing. You know when you love someone.</li>
		<li>I’ll think of you every step of the way.</li>
	</ul>
</body>
</html>
  • 在浏览器中打开

在这里插入图片描述

9、常用样式举例(背景图)

  • background-color(颜色)
  • background-image(url(相对路径))
  • background-repeat(no-repeat-不平铺 repeat-平铺 repeat-x-横向平铺 repeat-y-纵向平铺)
  • background-position(水平-left/center/right 垂直-top/center/bottom)
  • 上面四个属性缩写为一个(常用):background:背景色 url(背景图相对路径) no-repeat center top
<!DOCTYPE html>
<head>
	<title>Document</title>
	<style type="text/css">
		div{
			/*1. 背景颜色*/
			background: red;
			/*2. 背景宽度和高度*/
			width: 300px;
			height: 300px;
			/*3. 背景图片*/
			background-image: url(2.jpg);
			/*4. 背景图片是否重复*/
			background-repeat: repeat;
			/*5. 背景图片的位置*/
			background-position: top;
		}
	</style>
</head>
<body>
	<!-- 6. 盒子 -->
	<div></div>
</body>
</html>
  • 在浏览器中打开

在这里插入图片描述

10、常用样式举例(浮动属性)

  • 浮动(float)失去换行特征
  • 可以理解为块元素漂浮起来,其它的块元素可以占据空间
<!DOCTYPE html>
<head>
	<title>Document</title>
	<style type="text/css">	
		div{
			border: 1px solid black;
		}	
		#div1{
			width: 100px;
			height: 100px;
			background-color: red;
			float: left;
		}
		#div2{
			width: 200px;
			height: 200px;
			background-color: green;
		}
	</style>
</head>
<body>
	<div id="div1"></div>
	<div id="div2"></div>
</body>
</html>
  • 在浏览器中打开(一块漂浮,一块不漂浮)

在这里插入图片描述

  • 若定义了二块浮动,则它们的等级相同,不存在上下重叠
<!DOCTYPE html>
<head>
	<title>Document</title>
	<style type="text/css">	
		div{
			border: 1px solid black;
		}	
		#div1{
			width: 100px;
			height: 100px;
			background-color: red;
			float: left;
		}
		#div2{
			width: 200px;
			height: 200px;
			background-color: green;
			float: left;
		}
	</style>
</head>
<body>
	<div id="div1"></div>
	<div id="div2"></div>
</body>
</html>
  • 在浏览器中打开(二块漂浮)

在这里插入图片描述

高度塌陷

  • 若父类没有设置高度(div)而是由子类(div)撑起来的
  • 那么当子类元素漂浮起来后,父类元素会高度塌陷
<!DOCTYPE html>
<head>
	<title>Document</title>
	<style type="text/css">	
		div{
			border: 1px solid black;
		}	
		#fu{
			width: 500px;
			background-color: red;
		}
		#zi{
			width: 200px;
			height: 200px;
			background-color: green;
		}
	</style>
</head>
<body>
	<div id="fu">
		<div id="zi"></div>
	</div>
</body>
</html>
  • 在浏览器中打开

在这里插入图片描述
子类元素设置漂浮(float)

<!DOCTYPE html>
<head>
	<title>Document</title>
	<style type="text/css">	
		div{
			border: 1px solid black;
		}	
		#fu{
			width: 500px;
			background-color: red;
		}
		#zi{
			width: 200px;
			height: 200px;
			background-color: green;
			float: left;
		}
	</style>
</head>
<body>
	<div id="fu">
		<div id="zi"></div>
	</div>
</body>
</html>
  • 在浏览器中打开

在这里插入图片描述
解决方法

  • 给父类元素设置高度
  • 给父类元素添加属性(overflow)
<!DOCTYPE html>
<head>
	<title>Document</title>
	<style type="text/css">	
		div{
			border: 1px solid black;
		}	
		#fu{
			width: 500px;
			background-color: red;
			overflow: hidden;
		}
		#zi{
			width: 200px;
			height: 200px;
			background-color: green;
			float: left;
		}
	</style>
</head>
<body>
	<div id="fu">
		<div id="zi"></div>
	</div>
</body>
</html>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是我来晚了!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值