Table of Contents
这是是h1的内容
Home Web Front-end HTML Tutorial CSS选择符详解_html/css_WEB-ITnose

CSS选择符详解_html/css_WEB-ITnose

Jun 24, 2016 am 11:22 AM

一、类型选择符

什么是类型选择符?指以网页中已有的标签类型作为名称的行径符。body是网页中的一个标签类型,div,p,span都是。
如下:

  1. body {}   
  2. div {}   
  3. p {}   
  4. span {}  

 
二、群组选择符

对于XHMTL对象,可以对一组同时进行了相同的样式指派。
使用逗号对选择符进行了分隔,这样书写的优点在于同样的样式只需要书写一次即可,减少代码量,改善CSS代码结构。
使用时应该注意”逗号”是在半角模式下,并非中文全角模式。
如下:

  1. h1,h2,h6,p,span   
  2. {   
  3. font-size:12px;   
  4. color:#FF0000;   
  5. font-family: arial;   
  6. }   
  7.    

三、包含选择符
对某对象中的子对象进行样式指点定,这样选择方式就发挥了作用。
需要注意的是,仅对此对象的子对象标签有效,对于其它单独存在或位于此对象以外的子对象,不应用此样式设置。
这样做的优点在于,帮我们避免过多的id、class设置,直接对所需的元素进行定义。
如下:

  1. h2 span   
  2. {   
  3. color:red;   
  4. }  

如下:

  1. body h1 span strong   
  2. {   
  3. font-weight:bold;   
  4. }  

 
四、id选择符

根据DOM文档对象模型原理所出现的选择符,对于一个XHTML文件,其中的每一个标签都可以使用一个id=”"的形式进行一个名称指派,但需要注意,在一个XHTML文件中id是具有唯一性而不可以重复的。
在div css布局的网页中,可以针对不同的用途进行命名,如头部为header、底部为footer。
XHTML如下:

  1.   

 

CSS如下:

  1. #content   
  2. {   
  3. font-size:14px;   
  4. line-height:120%;   
  5. }  

 
五、class选择符

其实id是对于XHTML标签的扩展,而class是对SHTML多个标签的一种组合,class直译的意思是类或类别。
对于XHTML标签使用class=”"进行名称指派。与id不同,class可以重复使用,对于多个样式相同的元素,可以直接定义为一个class。
使用class的优点已不言自明,它对CSS代码重用性有良好的体现,众多的标签均可以使用一个样式来定义而不需要每一个编写一个样式代码。
XHTML如下:

  1.   
  2.   
  3.   

 

CSS如下:

  1. .he   
  2. {   
  3. margin:10px;   
  4. background-color:red;   
  5. }  

 
六、标签指定式的选择符

如果想同时使用id和class,也想同时使用标签选择符,可以使用如下的方式:

  1. h1#content {}   
  2. /*表示所有id为content的h1标签*/  
  3. h1.p1 {}   
  4. /*表示所有class为p1的h1标签*/  

 
标签指定式选择符的精度介于标签选择符及id/class选择符之间,是常用的选择符之一。

七、组合选择符

对于上面的所有选择符而言,进行组合使用。如下:

  1. h1 .p1 {}   
  2. /*表示h1下的所有class为p1的标签*/  
  3. #content h1 {}   
  4. 表示id为content的标签下的所有h1标签   
  5. h1 .p1,#content h1 {}   
  6. /*表示h1下的所有class为p1的标签以及id为content的标签下的所有h1标签*/  
  7. h1#content h2{}   
  8. /*id为content的h1标签下的h2标签*/  

 
CSS选择符是非常自由与灵活的,可以根据页面的需要,使用各种选择符,尽量结构化与优化CSS文件.

 

############################################################

 

一.选择符模式

模式/含义/内容描述

*

匹配任意元素。(通用选择器)

E

匹配任意元素 E (例如一个类型为 E 的元素)。(类型选择器)

E F

匹配元素 E 的任意后代元素 F 。(后代选择器)

E > F

匹配元素 E 的任意子元素 F 。(子选择器)

E:first-child

当元素 E 是它的父元素中的第一个子元素时,匹配元素 E 。(:first-child 伪类)

E:link E:visited

如果 E 是一个目标还没有访问过(:link)或者已经访问过(:visited)的超链接的源锚点时匹配元素 E 。(link 伪类)

E:active E:hover E:focus

在确定的用户动作中匹配 E 。(动态伪类)

E:lang(c)

如果类型为 E 的元素使用了(人类)语言 c (文档语言确定语言是如何被确定的),则匹配该元素。(:lang() 伪类)

E + F

如果一个元素 E 直接在元素 F 之前,则匹配元素 F 。(临近选择器)

E[foo]

匹配具有”foo”属性集(不考虑它的值)的任意元素 E 。(属性选择器)

E[foo="warning"]

匹配其“foo”属性值严格等于“warning”的任意元素 E 。(属性选择器)

E[foo~="warning"]

匹配其“foo”属性值为空格分隔的值列表,并且其中一个严格等于“warning”的任意元素 E 。(属性选择器)

E[lang|="en"]

匹配其“lang”属性具有以“en”开头(从左边)的值的列表的任意元素 E 。(属性选择器)

DIV.warning

仅 HTML。用法同 DIV[class~="warning"]。(类选择器)

E#myid

匹配 ID 等于“myid”的任意元素 E 。(ID 选择器)

我们用下面的例子来解释“[s]父元素[/s]”、“[s]子元素[/s]”、“[s]父/子[/s]”及“[s]相邻[/s]”这几个概念。

这是是h1的内容

这是一个段落p的内容!这里是strong的内容这是一个段落p的内容!

从以上代码中,我们可以找出这样的关系:

h1 和 p 同为 div 的“儿子”,两者分别同 div 形成“父/子”关系。

h1,p,strong 都是 div 的“子元素”。(三者都包含在 div 之内)

div 是 h1 和 p 的“父元素”。

strong 和 p 形成“父/子”关系,strong 的“父元素”是 p 。

但 strong 和 div 并非“父/子”关系,而是“祖孙”关系,但 strong 依然是 div 的“子(孙)元素”。

div 是 h1 p strong 三者的“祖先”,三者是 div 的“子(孙)元素”。

h1 和 p 两者是相邻的。

继承上面的实例来具体演示一下E F的关系:假如,我们需要将 strong 内的内容二字变为绿色,我们可以有哪些方法呢?

div strong {color:green;} /* - 正确。strong 是 div 的“子元素”*/

p > strong {color:green;} /* - 正确。strong 和 p 是“父/子”关系*/

div > strong {color:green;} /* - 错误!strong 虽然是 div 的“子(孙)元素”,但两者乃是“祖孙”关系,而非“父/子”关系,因此不能用 > 符号连接*/

临近选择器和通用选择器:通用选择器以星号“*”表示,可以用于替代任何 tag 。

实例:

h2 + * { color:green }/*所有紧随 h2 的元素内的文字都将呈现红色*/

二.选择符分类介绍

1.通配选择符

语法:

* { sRules }

说明:

通配选择符。选定文档目录树(DOM)中的所有类型的单一对象。

假如通配选择符不是单一选择符中的唯一组成,“*”可以省略。

示例:

*[lang=fr] { font-size:14px; width:120px; }

*.div { text-decoration:none; }

2.类型选择符

语法:

E { sRules }

说明:

类型选择符。以文档语言对象(Element)类型作为选择符。

示例:

td { font-size:14px; width:120px; }

a { text-decoration:none; }

 

3.属性选择符

语法:

E [ attr ] { sRules }

E [ attr = value ] { sRules }

E [ attr ~= value ] { sRules }

E [ attr |= value ] { sRules }

说明:

属性选择符。

选择具有 attr 属性的 E

选择具有 attr 属性且属性值等于 value 的 E

选择具有 attr 属性且属性值为一用空格分隔的字词列表,其中一个等于 value 的 E 。这里的 value 不能包含空格

选择具有 attr 属性且属性值为一用连字符分隔的字词列表,由 value 开始的 E

示例:

h[title] { color: blue; }

/* 所有具有title属性的h对象 */

span[class=demo] { color: red; }

div[speed="fast"][dorun="no"] { color: red; }

a[rel~="copyright"] { color:black; }

4.包含选择符

语法:

E1 E2 { sRules }

说明:

包含选择符。选择所有被 E1 包含的 E2 。即 E1.contains(E2)==true 。

示例:

table td { font-size:14px; }

div.sub a { font-size:14px; }

5.子对象选择符

语法:

E1 > E2 { sRules }

说明:

子对象选择符。选择所有作为 E1 子对象的 E2 。

示例:

[Copy to clipboard] [ - ]CODE:

body > p { font-size:14px; }

/* 所有作为body的子对象的p对象字体尺寸为14px */

div ul>li p { font-size:14px; }

6.ID选择符

语法:

#ID { sRules }

说明:

ID选择符。以文档目录树(DOM)中作为对象的唯一标识符的 ID 作为选择符。

示例:

#note { font-size:14px; width:120px;}

7.类选择符

语法:

E.className { sRules }

说明:

类选择符。在HTML中可以使用此种选择符。其效果等同于E [ class ~= className ] 。请参阅属性选择符( Attribute Selectors )。

在IE5+,可以为对象的 class 属性(特性)指定多于一个值( className ),其方法是指定用空格隔开的一组样式表的类名。例如:

示例:

div.note { font-size:14px; }

/* 所有class属性值等于(包含)"note"的div对象字体尺寸为14px */

.dream { font-size:14px; }

/* 所有class属性值等于(包含)"note"的对象字体尺寸为14px */

8.选择符分组

语法:

E1 , E2 , E3 { sRules }

说明:

选择符分组。将同样的定义应用于多个选择符,可以将选择符以逗号分隔的方式并为组。

示例:

.td1,div a,body { font-size:14px; }

td,div,a { font-size:14px; }

9.伪类及伪对象选择符

语法:

E : Pseudo-Classes { sRules }

E : Pseudo-Elements { sRules }

说明:

伪类及伪对象选择符。

伪类选择符。请参阅伪类( Pseudo-Classes )[:link :hover :active :visited :focus :first-child :first :left :right :lang]。

伪对象选择符。请参阅伪对象( Pseudo-Elements )[:first-letter :first-line :before :after]。

示例:

div:first-letter { font-size:14px; }

a.fly :hover { font-size:14px; color:red; }

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update? Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update? Mar 04, 2025 pm 12:32 PM

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,

How do I use HTML5 form validation attributes to validate user input? How do I use HTML5 form validation attributes to validate user input? Mar 17, 2025 pm 12:27 PM

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

How to efficiently add stroke effects to PNG images on web pages? How to efficiently add stroke effects to PNG images on web pages? Mar 04, 2025 pm 02:39 PM

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

What are the best practices for cross-browser compatibility in HTML5? What are the best practices for cross-browser compatibility in HTML5? Mar 17, 2025 pm 12:20 PM

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

What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

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

What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

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

How do I use the HTML5 <time> element to represent dates and times semantically? How do I use the HTML5 <time> element to represent dates and times semantically? Mar 12, 2025 pm 04:05 PM

This article explains the HTML5 &lt;time&gt; 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

What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

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

See all articles