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:26 AM

1.通配选择器

匹配所有元素

  *{color:red;}

2.标签选择器

匹配所有p元素

  p{color:red;}

3.ID选择器

匹配ID="div1"的元素

  #div1{color:red;}

4.类选择器

匹配class="red"的元素,CSS和HTML对大小写不敏感,但ID是大小写敏感的。

  .red{color:red;}

 

应用多个class,类名中间用空格隔开,class="red green"。元素将应用red和green的所有规则,重复规则只有一个生效,但不取决于class中类名顺序,而是CSS定义的顺序。

 1 <!DOCTYPE html> 2 <html> 3 <meta charset="UTF-8"/> 4 <head> 5 <style type="text/css"> 6 .red{ 7     background:red; 8     height:50px; 9 }10 .green{11     background:green;12 }13 </style>14 </head>15 <body>16     <div class="red">div1</div>17     <div class="green red">div2</div>18     <div class="red green">div3</div>19 </body>20 </html>
Copy after login

效果:

若将样式改为:

1 <style type="text/css">2 .green{3     background:green;4 }5 .red{6     background:red;7     height:50px;8 }9 </style>
Copy after login

效果为:

多类选择器,匹配class同时有多个特定类名的元素,优先级高于单类选择器,同样类名顺序无影响。

 1 <style type="text/css"> 2 .red{ 3     background:red; 4     height:50px; 5 } 6 .green{ 7     background:green; 8 } 9 .green.red{10     background:blue;11 }12 </style>
Copy after login

效果:

5.属性选择器

匹配有某属性的元素

  [attr]{color:red;}

匹配有某属性且属性值等于特定值的元素

  [title="div1"]{color:red;}

 1 <!DOCTYPE html> 2 <html> 3 <meta charset="UTF-8"/> 4 <head> 5 <style type="text/css"> 6 [title]{ 7     background:red; 8 } 9 [title="div1"]{10     background:blue;11 }12 </style>13 </head>14 <body>15     <div title="div1">div1</div>16     <div title="div2">div2</div>17 </body>18 </html>
Copy after login

效果:

其他属性选择器:

[attribute~=value] 用于选取属性值中包含指定词汇的元素。
[attribute|=value] 用于选取带有以指定值开头的属性值的元素,该值必须是整个单词。
[attribute^=value] 匹配属性值以指定值开头的每个元素。
[attribute$=value] 匹配属性值以指定值结尾的每个元素。
[attribute*=value] 匹配属性值中包含指定值的每个元素。

6.后代选择器与子元素选择器

后代选择器

  div span{color:red;}

子元素选择器

  div>span{color:red;}

使用以上选择器组合,后代选择器匹配特定父元素内的所有子孙元素,而子元素选择器匹配特定父元素内的一代子元素(不包括孙辈)。

 1 <!DOCTYPE html> 2 <html> 3 <meta charset="UTF-8"/> 4 <head> 5 <style type="text/css"> 6 div span{ 7     background:red; 8 } 9 div > span{10     background:blue;11 }12 div,p{13     border:1px solid black;14     padding:5px;15 }16 17 </style>18 </head>19 <body>20     <div>21         <span>div的子元素span1</span>22     </div>23     <br/>24     <div>25         <span>div的子元素span2</span>26         <p>27             <span>p的子元素、div的孙元素span3</span>28         </p>29     </div>30 </body>31 </html>
Copy after login

效果:

7.相邻兄弟选择器

匹配相邻下一个兄弟元素

  div+span{color:red;}

 1 <!DOCTYPE html> 2 <html> 3 <meta charset="UTF-8"/> 4 <head> 5 <style type="text/css"> 6 div+span{ 7     background:red; 8 } 9 div,p,span{10     border:1px solid black;11     margin:5px;12     padding:5px;13     line-height:40px;14 }15 </style>16 </head>17 <body>18     <div>div1</div>19     <span>div1的邻居span1</span>20     21     <div>div2</div>22     <p>div2的邻居p<span>p的子元素span2</span></p>23     <span>p的邻居span3</span>24 </body>25 </html>
Copy after login

效果:

8.逗号选择器

匹配多个选择器组合的结果

  h1,span{color:red;}

 1 <!DOCTYPE html> 2 <html> 3 <meta charset="UTF-8"/> 4 <head> 5 <style type="text/css"> 6 h1,span{ 7     background:red; 8 } 9 </style>10 </head>11 <body>12     <h1 id="h">h1</h1>13     <span>span</span>14 </body>15 </html>
Copy after login

效果:

9.伪类选择器

锚伪类,专门针对锚元素a的各个状态

a:link{...}

a:visited{...}

a:hover{...}

a:active{...}

定义时,hover必须在link和visited之后,active必须在hover之后。

:first-child伪类选择第一个元素

:last-child伪类选择最后一个元素

:nth-child(n)伪来选择第n个元素

:nth-last-child(n)伪类选择倒数第n个元素

:first-line伪类选择文本首行

:first-letter伪类选择文本首字

:before伪类在元素内容前插入新内容

:after伪类在元素内容后插入新内容

 1 <!DOCTYPE html> 2 <html> 3 <meta charset="UTF-8"/> 4 <head> 5 <style type="text/css"> 6 div:after{ 7     content:'|after|' 8 } 9 div:before{10     content:'|before|'11 }12 </style>13 </head>14 <body>15     <div>div</div>16     <span>span</span>17 </body>18 </html>
Copy after login

效果:

 

还有几个不太常用的CSS3伪类选择器没有例举,有时间补齐。

 

这种选择法与JQuery中的选择器非常非常类似,可见JQuery之基础选择器。

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
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)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1254
24
HTML: The Structure, CSS: The Style, JavaScript: The Behavior HTML: The Structure, CSS: The Style, JavaScript: The Behavior Apr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML, CSS, and JavaScript: Web Development Trends The Future of HTML, CSS, and JavaScript: Web Development Trends Apr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

The Future of HTML: Evolution and Trends in Web Design The Future of HTML: Evolution and Trends in Web Design Apr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

HTML vs. CSS vs. JavaScript: A Comparative Overview HTML vs. CSS vs. JavaScript: A Comparative Overview Apr 16, 2025 am 12:04 AM

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML: Building the Structure of Web Pages HTML: Building the Structure of Web Pages Apr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

HTML vs. CSS and JavaScript: Comparing Web Technologies HTML vs. CSS and JavaScript: Comparing Web Technologies Apr 23, 2025 am 12:05 AM

HTML, CSS and JavaScript are the core technologies for building modern web pages: 1. HTML defines the web page structure, 2. CSS is responsible for the appearance of the web page, 3. JavaScript provides web page dynamics and interactivity, and they work together to create a website with a good user experience.

HTML: Is It a Programming Language or Something Else? HTML: Is It a Programming Language or Something Else? Apr 15, 2025 am 12:13 AM

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

From Text to Websites: The Power of HTML From Text to Websites: The Power of HTML Apr 13, 2025 am 12:07 AM

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

See all articles