Web前端学习之路(二)CSS选择器与样式

一、定义

CSS,全称Cascading Style Sheets,翻译过来就是层叠样式列表。

二、引用

(一)内联样式表

举例

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	<p style="color: yellow">这是测试文本</p>
</body>
</html>

(二)内部样式表

格式

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

举例

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style type="text/css">
		p{
			color: red;
		}
	</style>
</head>
<body>
	<p>这是测试文本</p>
</body>
</html>

(三)外部样式表

实现:

  • 新建文档,编写CSS样式
  • <link rel="stylesheet" type="text/css" href="样式路径">插入到html代码中。

举例

/*样式表*/
p{
	color: blue;
}
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<link rel="stylesheet" type="text/css" href="index.css">
</head>
<body>
	<p>这是测试文本</p>
</body>
</html>

显示优先级:内联样式表 > 内部样式表 > 外部样式表,简单概括一下就是就近原则

三、选择器

选择器就是用来选择(找到)需要添加样式的柆置。

(一)基本选择器

标签选择器

标签选择器指的就是html文本里的标签。

<p>p就是标签选择器。</p>
/*样式选择器,匹配到所有p标签*/
p{
    /*样式*/
}
类选择器

类选择器,指的就是标签内class属性的属性值。

选择器定义:.

<p class="title">title就是类选择器</p>
/*类选择器,匹配到所有class属性为title的标签*/
.title{
    /*样式*/
}
ID选择器

ID选择器,指的就是标签内id属性的属性值。

选择器定义:#

<p id="p1">p1就是ID选择器</p>
/*ID选择器,匹配到所有ID属性为p1的标签*/
#p1{
   /*样式*/ 
}
通用选择器

通用选择器主要应用于会影响到整个页面的样式效果。

选择器定义:*

* {
    /*样式*/
}
属性选择器

属性选择器针对标签里的属性名来进行选择。

选择器定义:[]

/*匹配到所有拥有class属性的标签*/
[class]{
	/*样式*/
}

(二)组合选择器

联合选择器

联合选择器主要用于当需要对两个及以上的选择器实现相同的样式。

选择器定义:,

<p class="title">title就是类选择器</p>
<p id="p1">p1就是ID选择器</p>
/*匹配到所有id属性为p1或class属性为title的标签*/
#p1, .title{
    /*样式*/
}
子选择器

定义:A > B

作用域:A标签的下一级的B标签

后代选择器

定义:A B

作用域:A标签下的所有B标签

紧邻兄弟选择器

定义:A + B

作用域:A标签同级靠后的第一个B标签

一般兄弟选择器

定义:A ~ B

作用域:A标签同级靠后的所有B标签

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style type="text/css">
		ul ~ ol{
			color: red;
		}
		ul + ol{
			color: blue;
		}
		p em{
			color: yellow;
		}
		p > em{
			color: green;
		}
	</style>
</head>
<body>
	<div>
		<!-- 黑色 -->
		<ul>
			<li>List item 1</li>
			<li>List item 2</li>
			<li>List item 3</li>
		</ul>
		<!-- 黑色 -->
		<ul>
			<li>List item 1</li>
			<li>List item 2</li>
			<li>List item 3</li>
		</ul>
		<!-- 黑色 -->
		<ul>
			<li>List item 1</li>
			<li>List item 2</li>
			<li>List item 3</li>
		</ul>
		<!-- 蓝色 -->
		<ol>
			<li>List item 1</li>
			<li>List item 2</li>
			<li>List item 3</li>
		</ol>
		<!-- 红色 -->
		<ol>
			<li>List item 1</li>
			<li>List item 2</li>
			<li>List item 3</li>
		</ol>
		<!-- 红色 -->
		<ol>
			<li>List item 1</li>
			<li>List item 2</li>
			<li>List item 3</li>
		</ol>
		<p>
			<!-- 绿色 -->
			<em>1	
				<!-- 黄色 -->
				<em>1-1</em>
			</em>
		</p>
	</div>
</body>
</html>

运行结果

e0r8r4.png

(三)伪类与伪元素

伪类:通常情况下我们指的是超级链接上设置的样式。

状态伪类
属性作用
a:link未访问的链接
a:visited己访问的链接
a:hover鼠标移动到链接上(浮动、悬停)
a:active向被激活的元素添加样式
:focus选择拥有键盘输入焦点的元素
结构性伪类
属性作用
:first-child选择元素的第一个子元素
:last-child选择元素的最后一个子元素
:nth-child()选择某个元素的一个或多个特定的子元素
:nth-last-child()选择某个元素的一个或多个特定的子元素,从这个元素的最后一个子元素开始算。
:first-of-type选择一个上级元素下的第一个同类子元素
伪元素选择器
属性作用
::selection选择指定元素中被用户选中的内容
::before可以在内容之前插入新内容
::after可以在内容之后插入新内容
::first-line选择指定选择器的首行
::first-letter选择文本的第一个字符
案例代码
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style type="text/css">
		/*状态伪类*/
		a:link{
			color: blue;
		}
		a:visited{
			color: red;
		}
		a:hover{
			font-size: 20px;
		}
		a:active{
			color: green;
		}
		a:focus{
			background-color: yellow;
		}
		/*结构性伪类*/
		li:first-child{
			color: pink;
		}
		li:last-child{
			color: violet;
		}
		li:nth-child(2){
			color: orange;
		}
		li:nth-last-child(3){
			color: yellow;
		}
		p:first-of-type{
			color: green;
		}
		/*伪元素*/
		h1::before{
			content: "hello "
		}
		h1::after{
			content: "!!!"
		}
		h1::selection{
			color: red;
		}
		h1:first-line{
			color: blue;
		}
		h1:first-letter{
			background-color: yellow;
		}
	</style>
</head>
<body>
	状态伪类测试<br>
	<a href="https://www.baidu.com">百度</p></a>

	结构性伪类测试<br>
	<ul>
		<li>水果1</li>
		<li>水果2</li>
		<li>水果3</li>
		<li>水果4</li>
		<li>水果5</li>
	</ul>
	<p>动物1
		<p>动物2</p>
	</p>

	伪元素测试<br>
	<h1>WORLD</h1>
</body>
</html>

四、样式

(一)背景样式

属性属性值作用
background-color颜色值背景色
background-image图片地址背景图
background-repeatrepeat、repeat-x、repeat-y、no-repeat背景图的重复方法
background-attachmentscoll、fixed背景是否随滚动条滚动
backgound-position见下图背景图的起妈始位置
background背景样式的值是复合属性值组合

e0rYZ9.png

(二)文本样式

属性属性值作用
color颜色文本颜色
direction|tr、rt|文本的方向/书写方向
letter-spacingn px(n可为负数)字符间距
line-heightn px行高
text-alignleft、right、center、justify文本对齐方式
text-decorationnone、underline、overline、line-through文本的修饰
text-shadowh-shadow v-shadow blur color文本阴影
text-transformnone、capitalize、uppercase、lowercase改变字母大小写
text-indentn px、n em首行缩进

(三)字体样式

属性属性值作用
font-family字体字体
font-stylenormal、italic、oblique斜体
font-weightnormal、bold、100-900字体粗细
font-sizen px字体大小

(四)列表样式

属性属性值作用
list-style-typenone、disc、circle、square、decimal列表项目的外观
list-style-positioninside、outside列表符号的位置
list-style-imageurl、none列表项目设为图象
list-style同以上简写属性

e0rGqJ.png

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值