CSS选择器总结 - zhongJaywang
最近在www.w3school.com.cn学习了CSS选择器,下面做一个知识汇总。部分源码来自www.w3school.com.cn!
插入样式表的方法有三种:
第一种:外部样式表
1 2 3 |
|
第二种:内部样式表
1 2 3 4 5 6 7 |
|
第三种:内联样式表
1 2 3 |
|
CSS基本的选择器有四种,其它复杂的选择器都由这四种组合而成
- 元素选择器(类型选择器)
- id选择器
- 类选择器
- 属性选择器
选择器的基础语法,规则由两个主要的部分构成:选择器,以及一条或多条声明。
1 2 3 4 5 6 7 |
|
1. 元素选择器(类型选择器)
-
1
<span style=
"color: #800000;"
>h1 </span>{<span style=
"color: #ff0000;"
>font-family</span>:<span style=
"color: #0000ff;"
> sans-serif</span>;}
Copy after login
1 |
|
XML文档:
1 2 3 4 5 6 7 8 |
|
CSS样式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
|
2.id选择器
id 属性只能在每个 HTML 文档中出现一次!
HTML代码:
1 2 |
|
CSS样式:
1 2 |
|
3.类选择器
在下面的 HTML 代码中,h1 和 p 元素都有 center 类。这意味着两者都将遵守 ".center" 选择器中的规则。
1 2 3 4 5 6 7 |
|
CSS样式:
1 |
|
4.属性选择器
[attribute] | 用于选取带有指定属性的元素。 |
[attribute=value] | 用于选取带有指定属性和值的元素。 |
[attribute~=value] | 用于选取属性值中包含指定词汇的元素。 |
[attribute|=value] | 用于选取带有以指定值开头的属性值的元素,该值必须是整个单词。 |
[attribute^=value] | 匹配属性值以指定值开头的每个元素。 |
[attribute$=value] | 匹配属性值以指定值结尾的每个元素。 |
[attribute*=value] | 匹配属性值中包含指定值的每个元素。 |
为了更准确的定位HTML元素并对其添加样式,在上面四种基础选择器上面,CSS选择器还可以分成:分组选择器,派生选择器,后代选择器,子元素选择器,相邻兄弟选择器,伪类和伪元素。
*1.分组选择器
1 2 3 4 5 6 7 8 9 10 |
|
*2.派生选择器
派生选择器是通过依据元素在其位置的上下文关系来定义样式的。
HTML代码:
1 2 3 4 5 6 |
|
CSS代码:
1 2 3 4 |
|
*3.后代选择器
后代选择器又称包含选择器,可以选择某元素的任意一代的后代元素。
HTML代码:
1 2 |
|
CSS代码:
1 |
|
*4.子元素选择器
子元素选择器只可选择某元素第一代后代的选择器。
HTML代码:
1 2 |
|
CSS代码:
1 |
|
*5.相邻兄弟选择器
相邻兄弟选择器会选择某一元素紧随其后的元素,但是前提是他们拥有相同的父级。
eg1:
HTML代码:
1 2 3 4 5 |
|
CSS代码:
1 |
|
- 语法规则是h2和strong之间有一个“+”,“+”和前面的h1或者后面的strong之间的空格都是可有可无的。
- 在 以上例子中,第一个strong紧邻着h2并且他们拥有相同的父级(h1),所以h2 + strong会选择到第一个而不会选到第二个.
eg2:
HTML代码:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
CSS代码:
1 |
|
- 在以上的例子中li+li是选择紧挨着li后面的第一个
- ,所以第一个
- 不会被选择;而第二个
- 是紧挨着第一个li的,所有会被选择;第三个
- 是紧挨着第二个
- 的,也会被选择。
*6伪类
伪类的语法:
1 |
|
CSS 类也可与伪类搭配使用:
1 |
|
属性 | 描述 |
:active | 向被激活的元素添加样式。 |
:focus | 向拥有键盘输入焦点的元素添加样式。 |
:hover | 当鼠标悬浮在元素上方时,向元素添加样式。 |
:link | 向未被访问的链接添加样式。 |
:visited | 向已被访问的链接添加样式。 |
:first-child | 向元素的第一个子元素添加样式。 |
:lang | 向带有指定 lang 属性的元素添加样式。 |
超链接例子:
1 2 3 4 |
|
*7伪元素
属性 | 描述 |
:first-letter | 向文本的第一个字母添加特殊样式。 |
:first-line | 向文本的首行添加特殊样式。 |
:before | 在元素之前添加内容。 |
:after | 在元素之后添加内容。 |

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The official account web page update cache, this thing is simple and simple, and it is complicated enough to drink a pot of it. You worked hard to update the official account article, but the user still opened the old version. Who can bear the taste? In this article, let’s take a look at the twists and turns behind this and how to solve this problem gracefully. After reading it, you can easily deal with various caching problems, allowing your users to always experience the freshest content. Let’s talk about the basics first. To put it bluntly, in order to improve access speed, the browser or server stores some static resources (such as pictures, CSS, JS) or page content. Next time you access it, you can directly retrieve it from the cache without having to download it again, and it is naturally fast. But this thing is also a double-edged sword. The new version is online,

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

This article demonstrates efficient PNG border addition to webpages using CSS. It argues that CSS offers superior performance compared to JavaScript or libraries, detailing how to adjust border width, style, and color for subtle or prominent effect

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

This article explains the HTML5 <time> element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati
