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>
相关推荐
子兮曰4 天前
jev-ultrafast 深度解析:7 秒订机票的浏览器 Agent 是如何炼成的
前端·后端·agent
子兮曰4 天前
Jev 爆发一周:7 秒 Agent 背后的 System One 生态与三场争议
前端·后端·ai编程
一隅论数智4 天前
给AI一张“业务概念地图“:本体如何从哲学走向企业智能
大数据·人工智能·经验分享·笔记·学习·学习方法·政务
XiHongShi20164 天前
STM32F407 RTC定时器例程,建议保存
stm32·单片机·学习
前端小万4 天前
写公众号赚了 3000 块后,我做了一款叫 "一键成稿" 的软件
前端·微信小程序
爱勇宝4 天前
ZCode 开源 24 小时:一份没有历史的账本,回答不了"有没有偷代码"
前端·后端·chatglm (智谱)
三十而立洋4 天前
Cookie 详解:从产生到安全,一次讲透
前端·javascript
爱吃苹果的日记本4 天前
离散数学第六课
学习·离散数学
卡布鲁4 天前
把一个 Vite + Vue3 应用塞进 qiankun (React + Umi3) 主站:十个坑的复盘
前端·javascript·react.js