Table of Contents
1.常用选择器
2.伪类选择器
3.伪元素选择器
4.组合选择器
5.继承
6.CSS优先级
Home Web Front-end HTML Tutorial CSS3选择器详解_html/css_WEB-ITnose

CSS3选择器详解_html/css_WEB-ITnose

Jun 21, 2016 am 09:00 AM

转载自个人博客

1.常用选择器

1.1标签选择器

p{ }/*选择标签名为p的元素*/
Copy after login

1.2类选择器

.box{ }/*选择class名为box的元素*/
Copy after login

1.3ID选择器

#pid{ }/*选择id名为pid的元素*/
Copy after login

1.4通配符选择器

*{ }/*选择页面中所有的元素*/
Copy after login

1.5选择器前缀

div.bd{}/*选中class名为bd且标签为div的元素*/
Copy after login

1.6属性选择器

[disabled]{}/*选择带有属性disabled的所有元素*/[type=button]{}/*选择type属性为button的所有元素*/[class~=sports]{}/*选择class属性包含sports所有元素*/[lang|=en]{  }/*选择以lang属性以"en"开头以及以"-"分隔的所有元素*/a[href^="#"]{ }/*选择href属性以"#"开头的a元素*/[href$="pdf"]{  }/*选择href属性以"pdf"结尾的元素*/[href*="lady.163.com"]{}/*选择href属性中包含"lady.163.com"的元素*/
Copy after login

2.伪类选择器

2.1链接伪类

/*前两个只可用于描元素(链接)*/a:link {color:#FF0000;}        /* 未被访问的链接 */a:visited {color:#00FF00;}    /* 已被访问的链接 *//*后两个可以用于其它元素*/a:hover {color:#FF00FF;}    /* 鼠标指针移动到链接上 */a:active {color:#0000FF;}    /* 正在被点击的链接 */
Copy after login

链接在进行设置时:a:hover 必须位于 a:link 和 a:visited 之后, a:active 必须位于 a:hover 之后。

2.2动态伪类

input:focus{}/*设置input元素获得焦点后的样式*/input:enabled{}/*元素可用的状态*/input:disabled{}/*元素不可用的状态,属性选择器[disabled]的简写*/input:checked{}/*单选框或者复选框选择的样式*/
Copy after login

2.3树形图上的伪类
html结构如下:

<ul>    <li>1</li>    <li>2</li>    <li><a href="#">3</a></li>    <li>4</li>    <p>5</p></ul>
Copy after login

选择符:

ul:first-child{}/*选择第一个ul下第一个标签(css2定义,支持ie7)在处理margin时很有用*//*以下全是CSS3新增的伪类*/ul:last-child{}/*选择最后一个li标签*/ul:nth-child(n){}/*选择()内的表达式如:[2n+1],或者数字所代表的li标签*/ul:nth-last-child(n)/*同上,反向选择*/ul:only-child{}/*选择ul只有一个子元素的项,这里会选中第三个li下的a标签*/ul:first-of-type{}/*选择第一个该类型的标签,会选择第一个li和p*/ul:last-of-type{}/*选择最后一个该类型的标签,会选择第四个li和p*/ul:nth-last-of-type(2n){  }/*倒数的选择(.)内的表达式[2n],或数字所代表的该类型标签。选择第一个和第三个li标签*/ul:only-of-type{}/*选择ul下中只有一个该类型的项,会选中a标签和p标签*/:root{}/*选择文档的根元素*/ul:not(li){}/*选择不含有li标签的元素,这里会选中a标签和p元素*/ul:empty{}/*匹配ul下没有任何子元素(包括text节点)的元素,这里匹配不到,把p标签中的5删除,可匹配成功*/E:target{}/*匹配相关URL(锚点)指向的元素。*/
Copy after login

3.伪元素选择器

::first-letter{}/*选择第一个字母*/::first-line{}/*选择第一行*/::before{content:"before";}/*在某个元素之前插入一些内容*/::after{content:"after";}/*在某个元素后插入一些内容*/::selection{}/*设置于被用户选中时的样式*/::placeholder{}/*设置对象文字占位符的样式*/
Copy after login

4.组合选择器

4.1后代选择器

.main h2{  }/*选择class名为mian元素内的所有h2标签*/
Copy after login

4.2子选择器

.main>h2{  }/*选择class名为mian元素内的直接关联与父元素的h2标签*/
Copy after login

4.3相连选择器

h2~p{}/*只要P标签前有h2标签便选择*/
Copy after login

4.4兄弟选择器

h2~p{}/*选择h2标签后出现的所有p标签*/
Copy after login

4.4选择器分组

h1,h2,h3{backgound-color:#ddd;}/*同时设置h1,h2,h3标签,使用","分隔*/
Copy after login

5.继承

body{font-family:"Microsoft Yahei";}/*设置页面中所有的字体为微软雅黑*/
Copy after login
  • 继承属性

    • color

    • font

    • text-align

    • list-style

    • ...

  • 非继承属性

    • background

    • border

    • position

    • ...

6.CSS优先级

  • 计算方法

    • a = 行内样式

    • b = ID选择器的数量

    • c = 类/伪类和属性选择器的数量

    • d = 标签选择器和伪元素选择器数量

value = a1000 + b100 + c*10 +d

!important强行提升优先级

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
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 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
1675
14
PHP Tutorial
1278
29
C# Tutorial
1257
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 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.

Beyond HTML: Essential Technologies for Web Development Beyond HTML: Essential Technologies for Web Development Apr 26, 2025 am 12:04 AM

To build a website with powerful functions and good user experience, HTML alone is not enough. The following technology is also required: JavaScript gives web page dynamic and interactiveness, and real-time changes are achieved by operating DOM. CSS is responsible for the style and layout of the web page to improve aesthetics and user experience. Modern frameworks and libraries such as React, Vue.js and Angular improve development efficiency and code organization structure.

What is the difference between <strong>, <b> tags and <em>, <i> tags? What is the difference between <strong>, <b> tags and <em>, <i> tags? Apr 28, 2025 pm 05:42 PM

The article discusses the differences between HTML tags , , , and , focusing on their semantic vs. presentational uses and their impact on SEO and accessibility.

See all articles