CSS学习1

CSS

前言

CSS:层叠样式表

样式规则:选择器 {属性:值;属性:值;}

一、类选择器

类选择器:".名字 {属性:值}"

多类名选择器:html选择器中的class可以放多个值,多个类名没有先后顺序。空格隔开即可。

注意:名字最好用英文字字母表示,可以加-

html 复制代码
<html>
	<head>
		<title>类选择器</title>
		<style>
		.c-red {
			color: red;
			}
		</style>
	</head>
	<body>
		<span class="c-red">G</span>
		<span>G</span>
		<span>G</span>
		<span>G</span>
	</body>
</html>

二、id选择器

id选择器:"#名字 {属性:值}"

与类选择器的区别:类选择器可以重复使用,id选择器类似身份证,只能用一次。

html 复制代码
<html>
	<head>
		<title>id选择器</title>
		<style>
		.c-red {
			color: red
			}
		#c-p {
			color: pink
			}
		</style>
	</head>
	<body>
		<span class="c-red">G</span>
		<span id="c-p">G</span>
		<span>G</span>
		<span>G</span>
	</body>
</html>

三、通配符选择器

通配符选择器:"* {属性:值}"

四、伪类选择器

1. 链接伪类选择器

作用:给链接添加特殊效果

:link 未访问链接

:visited 已访问链接

:hover 鼠标移动到链接上

:active 选定的链接(点击别松开)

注意:写的时候顺序不要颠倒,lovehate

html 复制代码
<html>
	<head>
		<title>链接伪类选择器</title>
		<style>
		a:link { /*未访问的状态*/
			font-size: 16px;
			color: red;
			font-weight: 700;
			}
		a:visited { /*已访问的状态*/
			font-size: 16px;
			color: green;
			font-weight: 700;
			}
		a:hover { /*鼠标移动上去*/
			font-size: 16px;
			color: orange;
			font-weight: 700;
			}
		a:active { /*选定的状态*/
			font-size: 16px;
			color: pink;
			font-weight: 700;
			}
		a {/*标签选择器,设置所有a*/
			font-size: 16px;
			color: gray;
			font-weight: 700;
			}
		</style>
	</head>
	<body>
		<div><a href="#">秒杀</a></div>
	</body>
</html>

2. 结构(位置)伪类选择器

:first-child

:last-child

:nth-child(odd) 从前往后,odd可以选择所有奇数,even(2n)选偶数,n选所有的(从0开始)

:nth-last-child 从后往前

html 复制代码
<html>
	<head>
		<title>位置伪类选择器</title>
		<style>
		li:first-child { /*第一个*/
			color: red;
			}
		li:last-child { /*最后一个*/
			color: red;
			}
		li:nth-child(4) { 
			color: red;
			}
		li:nth-last-child {
			color: deeppink;
			}
		</style>
	</head>
	<body>
		<ul>
			<li>第一个孩子</li>
			<li>第二个孩子</li>
			<li>第三个孩子</li>
			<li>第四个孩子</li>
			<li>第五个孩子</li>
		</ul>
	</body>
</html>

3. 目标伪类选择器

:target 可用于选取当前活动的目标元素

html 复制代码
<html>
	<head>
		<title>document</title>
		<style>
		:target {
			color: red;
			}
		</style>
	</head>
	<body>
		<h2>目录</h2>
		<hr/>
		1.早年经历<br/>
		<a href="#movie">2.演艺经历</a><br/>
		3.个人生活<br/>
		4.主要作品<br/>
		<hr/>
		<h3>早年经历</h3>
		<h3 id="movie">演艺经历</h3>
		<h3>个人生活</h3>
		<h3>主要作品</h3>
	</body>
</html>
相关推荐
We་ct1 分钟前
深度剖析浏览器跨域问题
开发语言·前端·浏览器·跨域·cors·同源·浏览器跨域
weixin_4277716126 分钟前
前端调试隐藏元素
前端
Amazing_Cacao1 小时前
CFCA精品可可产区认证课程风土解析(美洲):打破风味堆叠的假象,建立时间轴上的层次展开阅读系统
学习
永远不会的CC1 小时前
浙江华昱欣实习(4月23日~ 4月19日)
后端·学习
爱上好庆祝1 小时前
学习js的第五天
前端·css·学习·html·css3·js
qiaozhangchi2 小时前
求解器学习笔记
笔记·python·学习
C澒2 小时前
IntelliPro 产研协作平台:基于 AI Agent 的低代码智能化配置方案设计与实现
前端·低代码·ai编程
一袋米扛几楼982 小时前
【Git】规范化协作:详解 GitHub 工作流中的 Issue、Branch 与 Pull Request 最佳实践
前端·git·github·issue
网络点点滴2 小时前
前端与后端的区别与联系
前端
bendandawugui2 小时前
PCIe协议学习-PCIe的No Snoop Attr使用
学习