- CSS 教程
- CSS - 首页
- CSS - 路线图
- CSS - 简介
- CSS - 语法
- CSS 选择器
- CSS - 包含
- CSS - 测量单位
- CSS - 颜色
- CSS - 背景
- CSS - 字体
- CSS - 文本
- CSS - 图片
- CSS - 链接
- CSS - 表格
- CSS - 边框
- CSS - 块级边框
- CSS - 内联边框
- CSS - 外边距
- CSS - 列表
- CSS - 内填充
- CSS - 光标
- CSS - 轮廓
- CSS - 尺寸
- CSS - 滚动条
- CSS - 块内元素
- CSS - 下拉菜单
- CSS - 可见性
- CSS - 溢出
- CSS - 清除浮动
- CSS - 浮动
- CSS - 箭头
- CSS - 调整大小
- CSS - 引号
- CSS - 顺序
- CSS - 位置
- CSS - 连字符
- CSS - 悬停
- CSS - 显示
- CSS - 聚焦
- CSS - 缩放
- CSS - 位移
- CSS - 高度
- CSS - 连字符字符
- CSS - 宽度
- CSS - 透明度
- CSS - Z-index
- CSS - 底部
- CSS - 导航栏
- CSS - 叠加层
- CSS - 表单
- CSS - 对齐
- CSS - 图标
- CSS - 图片库
- CSS - 注释
- CSS - 加载器
- CSS - 属性选择器
- CSS - 组合器
- CSS - 根元素
- CSS - 盒模型
- CSS - 计数器
- CSS - 剪裁
- CSS - 书写模式
- CSS - Unicode 双向算法
- CSS - min-content
- CSS - all
- CSS - inset
- CSS - 隔离
- CSS - 滚动溢出
- CSS - justify-items
- CSS - justify-self
- CSS - tab-size
- CSS - pointer-events
- CSS - place-content
- CSS - place-items
- CSS - place-self
- CSS - max-block-size
- CSS - min-block-size
- CSS - mix-blend-mode
- CSS - max-inline-size
- CSS - min-inline-size
- CSS - offset
- CSS - accent-color
- CSS - user-select
- CSS 高级
- CSS - 网格
- CSS - 网格布局
- CSS - Flexbox
- CSS - 可见性
- CSS - 定位
- CSS - 图层
- CSS - 伪类
- CSS - 伪元素
- CSS - @规则
- CSS - 文本效果
- CSS - 分页媒体
- CSS - 打印
- CSS - 布局
- CSS - 验证
- CSS - 图片精灵
- CSS - !important
- CSS - 数据类型
- CSS3 教程
- CSS3 - 教程
- CSS - 圆角
- CSS - 边框图片
- CSS - 多重背景
- CSS - 颜色
- CSS - 渐变
- CSS - 盒阴影
- CSS - box-decoration-break
- CSS - caret-color
- CSS - 文本阴影
- CSS - 文本
- CSS - 二维变换
- CSS - 三维变换
- CSS - 过渡
- CSS - 动画
- CSS - 多列
- CSS - box-sizing
- CSS - 工具提示
- CSS - 按钮
- CSS - 分页
- CSS - 变量
- CSS - 媒体查询
- CSS - 函数
- CSS - 数学函数
- CSS - 遮罩
- CSS - 形状
- CSS - 样式图片
- CSS - 特效性
- CSS - 自定义属性
- CSS 响应式
- CSS RWD - 简介
- CSS RWD - 视口
- CSS RWD - 网格视图
- CSS RWD - 媒体查询
- CSS RWD - 图片
- CSS RWD - 视频
- CSS RWD - 框架
- CSS 工具
- CSS - PX 到 EM 转换器
- CSS - 颜色选择器和动画
- CSS 资源
- CSS - 有用资源
- CSS - 讨论
CSS 选择器
CSS 选择器用于选择您想要在网页上设置样式的 HTML 元素。它们允许您定位特定的元素或元素组以应用样式,例如颜色、字体、边距等等。
被选择器选择的元素被称为选择器的目标。
选择器类型
CSS 通用选择器
通用选择器用星号(*)表示,是一个特殊的选择器,它匹配 HTML 文档中的所有元素。这些通常用于为文档中的所有元素添加相同长度的边距和填充。
语法
* {
margin: 0;
padding: 0;
}
根据上述语法,通用选择器用于将 0 边距和填充应用于所有 HTML 元素。
示例
以下示例演示了通用选择器 (*) 的用法
<html>
<head>
<style>
* {
margin: 0;
padding: 0;
background-color: peachpuff;
color: darkgreen;
font-size: 25px;
}
</style>
</head>
<body>
<h1>Universal selector (*)</h1>
<div>
Parent element
<p>Child paragraph 1</p>
<p>Child paragraph 2</p>
</div>
<p>Paragraph 3</p>
</body>
</html>
CSS 元素选择器
元素选择器目标是 HTML 元素,例如 <h1>,<p> 等。当我们想要对文档中的所有 <p> 标签或 <h1> 标签应用类似样式时,可以使用此方法。
语法
/* Sets text color of all p tags to green */
p {
color: green;
}
/* Add underline to all h1 tags in document */
h1 {
text-decoration-line: underline;
}
示例
以下示例演示了元素选择器的用法
<html>
<head>
<style>
div {
border: 5px inset gold;
width: 300px;
text-align: center;
}
p {
color: green;
}
h1 {
text-decoration-line: underline;
}
</style>
</head>
<body>
<div>
<h1>Element selector</h1>
<p>Div with border </p>
<p>Text aligned to center</p>
<p>Paragraph with green color</p>
<p>h1 with an underline</p>
</div>
</body>
</html>
CSS 类选择器
类选择器目标是其 class 属性具有特定值的元素。CSS 中的类用"."(句点)符号表示。
语法
.sideDiv {
text-decoration-line: underline;
}
.topDiv {
color: green;
font-size: 25px;
}
示例
以下示例演示了类选择器的用法,其中.style-div,.topDivs和.bottomDivs是类选择器
<html>
<head>
<style>
.style-div {
border: 5px inset gold;
width: 300px;
text-align: center;
}
.topDivs{
font-weight: bold;
font-size: 30px;
}
.bottomDivs{
color: green;
font-size: 20px;
}
</style>
</head>
<body>
<div class="style-div">
<div class="topDivs">
Hello World
</div>
<div class="topDivs">
Learn CSS
</div>
<div class="bottomDivs">
From
</div>
<div class="bottomDivs">
TutorialsPoint
</div>
</div>
</body>
</html>
CSS ID 选择器
ID 选择器目标是其 id 属性具有特定值的单个元素。CSS 中的 ID 用"#"(井号)符号表示。相同的类可以应用于多个元素,但 ID 对每个元素都是唯一的。
语法
#style-p {
color: green;
font-size: 25px;
}
#style-h1 {
text-decoration-line: underline;
color: red;
}
示例
以下示例演示了 ID 选择器的用法,其中#style-div,#tutorial和#stylePoint是应用于元素的 ID 选择器
<html>
<head>
<style>
#style-div {
border: 5px inset gold;
width: 300px;
text-align: center;
}
#tutorial{
color: green;
font-size: 20px;
}
#stylePoint{
color: black;
font-size: 15px;
font-weight: bold;
}
</style>
</head>
<body>
<div id="style-div">
<div id="tutorial">
Tutorials
<span id="stylePoint">
Point
</span>
</div>
<p>
Here we used ids to
style different elements.
</p>
</div>
</body>
</html>
CSS 属性选择器
属性选择器根据元素上的特定属性或属性值来定位元素。
有关属性选择器的详细说明,请参阅此属性选择器文章。
语法
/* Style all anchor tag with target attribute */
a[target] {
background-color: peachpuff;
}
/* Style all anchor tag that links to tutorialspoint */
a[href="https://tutorialspoint.com"] {
background-color: peachpuff;
}
示例
以下示例演示了属性选择器的用法
<html>
<head>
<style>
a[href]{
font-size: 2em;
}
a[target] {
background-color: peachpuff;
color: blueviolet;
}
/* Attribute with value have more priority*/
/* Hence black background applied to CSS link*/
a[target="_self"] {
background-color: black;
}
</style>
</head>
<body>
<h2>Attribute selector</h2>
<p>
Styling applied to anchor element:
</p>
<a href="https://tutorialspoint.com/">
Tutorialspoint
</a>
<br><br>
<a href="/html/index.htm" target="_blank">
HTML Tutorial
</a>
<br><br>
<a href="/css/index.htm" target="_self">
CSS Tutorial
</a>
</body>
</html>
CSS 组选择器
CSS 组选择器允许我们一次对多个元素应用相同的样式。元素名称可以用逗号分隔。此方法推荐使用,因为它使 CSS 简洁并避免冗余。
语法
/* Apply same background color for h1 and h2 */
h1, h2 {
background-color: grey;
}
示例
以下示例显示如何在 CSS 中使用组选择器。
<html>
<head>
<style>
/* This applies to both <h1> and <h2> elements */
h1, h2 {
background-color: grey;
padding: 4px;
}
/*Applies to all paragraphs, elements with class*/
/*'highlight', and element with ID 'hightlightSpan'*/
p, .highlight, #hightlightSpan {
background-color: yellow;
padding: 10px;
}
</style>
</head>
<body>
<h1>CSS Selectors</h1>
<h2>Group Selectors</h2>
<p>This is a paragraph.</p>
<div class="highlight">
This is div
</div>
<br>
<span id="hightlightSpan">
This is span
</span>
</body>
</html>
CSS 伪类选择器
伪类选择器用于设置元素特定状态的样式,例如:hover用于在悬停时设置元素的样式。
有关伪类选择器的详细列表,请参阅此CSS 伪类教程。
语法
/* Change background color on hover */
a :hover {
background-color: peachpuff;
}
/* Change background color on clicking button */
button:active {
background-color: yellow;
}
/* Change border color on focusing input */
input:focus {
border-color: blue;
}
示例
以下示例演示了伪类选择器的用法
<html>
<head>
<style>
a:hover {
background-color: peachpuff;
color: green;
font-size: 2em;
}
button:active {
background-color: yellow;
}
</style>
</head>
<body>
<h2>Pseudo-class selector</h2>
<p>
Styling applied to anchor element and
button with a pseudo-class:
</p>
<a href="https://tutorialspoint.com">
Tutorialspoint
</a>
<br><br>
<button>Click Me</button>
</body>
</html>
CSS 伪元素选择器
伪元素选择器用于设置元素特定部分的样式,而不是元素本身。
有关伪元素选择器的详细列表,请参阅此CSS 伪元素教程。
语法
/* Define contents before paragraph */
a::before {
content: " ";
}
/* Style first letter of paragraph */
p::first-letter {
font-size: 2em;
}
示例
以下示例演示了伪元素选择器(::before)和(::after)的用法
<html>
<head>
<style>
/* Add and style contents before paragraph */
p::before {
content: "Note: ";
font-weight: bold;
color: red;
}
/* Add and style contents after paragraph */
p::after {
content: " [Read more]";
font-style: italic;
color: blue;
}
</style>
</head>
<body>
<h2>Pseudo-element selector</h2>
<p>This is a paragraph.</p>
</body>
</html>
CSS 后代选择器
后代选择器用于在 css 中设置作为特定指定标签子代的所有标签的样式。父元素和子元素之间的单个空格用于表示后代。
语法
div p {
color: blue;
}
以上代码将 div 元素内段落标签的文本颜色设置为蓝色。
示例
以下示例显示如何在 css 中使用后代选择器。
<!DOCTYPE html>
<html lang="en">
<head>
<style>
div{
border: 2px solid;
}
div p {
color: blue;
}
</style>
</head>
<body>
<div>
<p>
This paragraph is inside a div
and will be blue.
</p>
<section>
<p>
This paragraph is inside a
section which is inside a
div and will also be blue.
</p>
</section>
</div>
<p>
This paragraph is outside the div
and will not be blue.
</p>
</body>
</html>
CSS 子选择器
css 中的子选择器用于定位特定元素的所有直接子代。这用“>” (大于) 符号表示。
语法
div > p {
color: blue;
}
以上代码将直接位于 div 元素内的段落标签的文本颜色设置为蓝色。
示例
以下示例显示如何在 css 中使用子选择器。
<!DOCTYPE html>
<html lang="en">
<head>
<style>
div{
border: 2px solid;
}
div > p {
color: blue;
}
</style>
</head>
<body>
<div>
<p>
This paragraph is inside a div and
will be blue.
</p>
<section>
<p>
This paragraph is inside a
section which is inside a div
and will not be blue as this
is not direct child
</p>
</section>
</div>
<p>
This paragraph is outside the div
and will not be blue.
</p>
</body>
</html>
CSS 相邻兄弟选择器
在 CSS 中,相邻兄弟选择器用于定位紧跟在指定元素之前的元素。加号符号( "+" )用于表示相邻兄弟。
语法
h1 + p {
margin-top: 0;
}
以上代码将 h1 标签之后段落标签的顶部边距设置为 0。
示例
以下示例显示如何在 css 中使用相邻兄弟选择器。
<!DOCTYPE html>
<html lang="en">
<head>
<style>
div{
border: 4px solid;
}
div + p {
color: blue;
}
</style>
</head>
<body>
<p>
This paragraph is above the div
and will not be blue
</p>
<div>
<p>
This paragraph is inside a div
and will not be blue.
</p>
</div>
<p>
This paragraph 1 after the div
and will be blue.
</p>
<p>This paragraph 2 after the
div and will not be blue.
</p>
</body>
</html>
CSS 通用兄弟选择器
在 CSS 中,通用兄弟选择器用于定位在指定元素之前的全部元素。波浪号符号( "~" )用于表示通用兄弟。
语法
h1 ~ p {
color: gray;
}
以上代码将 h1 标签之后所有段落的文本颜色设置为灰色。
示例
以下示例显示如何在 css 中使用通用兄弟选择器。
<!DOCTYPE html>
<html lang="en">
<head>
<style>
div{
border: 4px solid;
}
div ~ p {
color: blue;
}
</style>
</head>
<body>
<p>
This paragraph is above the div
and will not be blue
</p>
<div>
<p>
This paragraph is inside a div
and will not be blue.
</p>
</div>
<p>
This paragraph 1 after the div
and will be blue.
</p>
<p>This paragraph 2 after the
div and will be blue.
</p>
</body>
</html>
CSS 中的嵌套选择器
CSS 嵌套允许将一个样式规则嵌套在另一个规则内,子规则的选择器相对于父规则的选择器。
CSS 嵌套选择器的特点
嵌套选择器显示了父规则和子规则之间的关系。
- 当浏览器解析嵌套选择器时,它会自动在选择器之间添加空格,从而创建一个新的 CSS 选择器规则。
- 在需要将嵌套规则附加到父规则(不带任何空格)的情况下,例如使用伪类或复合选择器时,必须立即添加&嵌套选择器才能获得所需的结果。
- 为了反转规则的上下文,可以附加&嵌套选择器。
- 可以有多个&嵌套选择器实例。
语法
nav {
& ul {
list-style: none;
& li {
display: inline-block;
& a {
text-decoration: none;
color: blue;
&:hover {
color: red;
}
}
}
}
}
示例
以下示例演示了 & 嵌套选择器 (&) 的用法
<html>
<head>
<style>
#sample {
font-family: Verdana, Geneva, Tahoma, sans-serif;
font-size: 1.5rem;
& a {
color: crimson;
&:hover,
&:focus {
color: green;
background-color: yellow;
}
}
}
</style>
</head>
<body>
<h1>& nesting selector</h1>
<p id="sample">
Hover <a href="#">over the link</a>.
</p>
</body>
</html>