Home Web Front-end CSS Tutorial Detailed explanation of CSS Selector usage

Detailed explanation of CSS Selector usage

Mar 21, 2018 pm 05:03 PM
css selector

This time I will bring you a detailed explanation of the use of CSS Selector. What are the precautions for using CSS Selector? The following is a practical case, let's take a look.

The selector defined in CSS1

Type selector

is used to select the specified type element (actually it is html tag selector), common usage is as follows:

body {
    /*对 body 元素定义样式*/
}
body,p {
    /*同时选择多种标签元素*/
}
Copy after login

ID selector

is used to select the specified ID The common usage of html elements is as follows:

<p id="nav">
    
</p>
<style>
    #nav {
        /*定义 ID 为 nav 的元素的样式*/
    }
</style>
Copy after login

Because the rendering order of CSS is from right to left, and the ID is completely unique, the previous type selector can be omitted.

Class selector

is used to select html elements with specified class names. Common usage methods are as follows:

<p class="nav">
    
</p>
<style>
    .nav {
        /*定义 class 为 nav 的元素的样式*/
    }
</style>
Copy after login

Include selector

is used to select hierarchically nested elements. Common usage methods are as follows:

<p class="nav">
    <p class="nav-tools">
        
    </p>
</p>
<p class="nav">
    <p>
        <p class="nav-tools">
        
        </p>
    </p>
</p>
<style>
    .nav .nav-tools {
        /*定义元素的父级元素 class 包含 nav,且子元素class 包含 nav-tools 的元素*/
    }
</style>
Copy after login

It should be noted that the inclusion selector does not care about the level, as long as the subsequent selector is included Just the one in the previous element. As in the above example, both nav-tools will be selected by the selector!

Pseudo-class selector

:link——Link pseudo-class selector

Use To define the style of a link when it is not visited, common usage methods are as follows:

<p class="nav">
    <p class="nav-tools">
        <ul>
            <li><a href="#"></a></li>
        </ul>
    </p>
</p>
<style>
    a:link {
        text-decoration: none;
        color: blue;
    }
</style>
Copy after login

:visited——Link pseudo-class selector

is used to define that it has been visited The link style, common usage methods are as follows:

<style>
    a:visited {
        text-decoration: none;
        color: red;
    }
</style>
Copy after login

:active - user operation pseudo-class selector

is used to define the activated element style, common usage methods As follows:

<style>
    a:active {
        text-decoration: none;
        color: green;
    }
</style>
Copy after login

:hover——User operation pseudo-class selector

is used to define the style of the element when the mouse passes over it. Common usage methods are as follows:

<style>
    a:hover {
        text-decoration: none;
        background-color: #F4F4F4;
    }
</style>
Copy after login

:focus——User operation pseudo-class selector

is used to define the style of the element that receives focus. Common usage methods are as follows:

<style>
    input:focus {
        text-decoration: none;
        background-color: #F4F4F4;
    }
</style>
Copy after login

: :first-line

is used to define the style of the first line of text within an element. Common usage methods are as follows:

<p class="doc">
    <p>层叠样式表(英文全称:Cascading Style Sheets)是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标准通用标记语言的一个子集)等文件样式的计算机语言。CSS不仅可以静态地修饰网页,还可以配合各种脚本语言动态地对网页各元素进行格式化。</p>
</p>
<style>
    .doc {
        width: 360px;
    }
    .doc>p::first-line {
        color: red;
    }
</style>
Copy after login

::=first-letter

is used to define the style of the first character within the element. Common usage methods are as follows:

<style>
    .doc {
        width: 360px;
    }
    .doc>p::first-letter {
        font-size: 2em;
        color: red;
    }
</style>
Copy after login

Selector defined in CSS2

*——Wildcard selector

is used to define the common style of all elements in the DOM. Common usage methods are as follows:

<p class="nav">
    <p>
        <p class="nav-tools">
        
        </p>
    </p>
</p>
<style>
    .nav * {
        margin: 0;
    }
</style>
Copy after login

If you want to reset the default For styles, it is not recommended to use wildcard selectors

[attribute]——Attribute selector

is used to define elements containing attributes as attributes The style of the element, common usage methods are as follows:

<p class="nav">
    <p>
        <p class="nav-tools">
            <ul>
                <li active>Menu</li>
                <li>Index</li>
            </ul>
        </p>
    </p>
</p>
<style>
    li[active] {
        color: red;
    }
</style>
Copy after login

[attribute="value"] - Attribute selector

is used to define the value of the element attribute as specified The value style of Separate attribute value elements with spaces. Common usage methods are as follows:

<p class="nav">
    <p>
        <p class="nav-tools">
            <ul>
                <li active="active">Menu</li>
                <li active>Index</li>
            </ul>
        </p>
    </p>
</p>
<style>
    li[active="active"] {
        color: red;
    }
</style>
Copy after login

As shown above, only the foreground color of the first li will be defined as red!

[attribute|="value"] - Attribute selector

is used to define attribute values ​​that contain the specified value and are linked by a hyphen (-) , common usage methods are as follows:

<p class="nav">
    <p>
        <p class="nav-tools">
            <ul>
                <li active="test active">Menu</li>
                <li active="active-test">Index</li>
            </ul>
        </p>
    </p>
</p>
<style>
    li[active~="active"] {
        color: red;
    }
</style>
Copy after login

As shown above: only the foreground color of the second li will be defined as red!

:first-child——structural pseudo-class selector

is used to define the style of the first element of the element. Common usage methods are as follows:

<p class="nav">
    <p>
        <p class="nav-tools">
            <ul>
                <li active="test active">Menu</li>
                <li active="active-test">Index</li>
            </ul>
        </p>
    </p>
</p>
<style>
    li[active|="active"] {
        color: red;
    }
</style>
Copy after login

Note: first-child is the first element that acts on the same level and has the same label. As shown above, if you want to define the style of the first li, you need to use li:first-child instead of ul:first-child!

:lang(en)

用于定义属性为 lang=“en” 的元素样式,常见使用方法如下:

<p>
    <p lang="en">Hello World</p>
</p>
<style>
    p:lang(en) {
        color: red;
    }
</style>
Copy after login

::before

用于定义元素之前的内容和样式,常见使用方法如下:

<p>
    <a>World</a>
</p>
<style>
    a::before {
        content: "Hello ";
    }
</style>
Copy after login

::after

用于定义元素之后的内容和样式,常见使用方法如下:

<p>
    <a>Hello</a>
</p>
<style>
    a::after {
        content: "World";
    }
</style>
Copy after login

p > p

用于定义元素的第一级子元素的样式,常见方法使用如下:

<p class="nav">
    <p>
        <p class="nav-tools">
            <ul>
                <li active="test active">Menu</li>
                <li active="active-test">Index</li>
            </ul>
        </p>
    </p>
</p>
<style>
    .nav-tools > ul {
        background-color: red;
    }
    .nav-tools > li {
        /*这个不会生效,因为 li 不是 ul 的直接子元素*/
    }
</style>
Copy after login

h1 + p

用于定义元素相邻的元素样式,常见使用方法如下:

<p>
    <h1>CSS</h1>
    <p>层叠样式表(英文全称:Cascading Style Sheets)是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标准通用标记语言的一个子集)等文件样式的计算机语言。</p>
</p>
<style>
    h1 + p {
        color: red;
    }
</style>
Copy after login

CSS3 新增属性选择器

[foo^="bar"]

用于定义元素属性以 bar 开头的元素的样式

<p>
    <a href="http://www.betterde.com">Betterde Inc.</a>
    <a href="https://www.betterde.com">Betterde Inc.</a>
</p>
<style>
    a[href^="https"] {
        color:green;
    }
</style>
Copy after login

如上所示,标记 https 链接为绿色。

[foo$="bar"]

用于定义元素属性以 bar 结尾的元素的样式

<p>
    <a href="http://www.betterde.com/logo.png">logo.png</a>
    <a href="http://www.betterde.com/style.css">style.css</a>
    <a href="http://www.betterde.com/main.js">main.js</a>
</p>
<style>
    a[href$="png"] {
        background: url(system/filetype/png.png) no-repeat left center;
        padding-left: 18px;
    }
    
    a[href$="css"] {
        background: url(system/filetype/css.png) no-repeat left center;
        padding-left: 18px;
    }
    
    a[href$="js"] {
        background: url(system/filetype/js.png) no-repeat left center;
        padding-left: 18px;
    }
</style>
Copy after login

如上所示,识别超链接的所链接的文件格式,并在前面添加文件类型图标!

[foo*="bar"]

用于定义元素属性中包含 bar 的元素的样式,需要注意的是,这里是包含,也就是说无论是什么样的组合,只要属性值还有这bar 这三个连续字母的都会被选中!

<p>
    <h1 class="title big full-right"></h1>
    <h2 class="title big full-right"></h1>
    <h1 class="big-title"></h1>
</p>
<style>
    a[class*="title"] {
        color: red;
    }
</style>
Copy after login

如上所示:p 内的三个元素都将会被渲染为红色字体!

虽然 CSS3 中任然保留 CSS2 中定义的属性选择器,但是建议使用 CSS3 的属性选择器来替代!

结构伪类选择器

:root

用于定义 html 标签元素的样式

:nth-child(n)

用于定义子元素的样式,n 表示第几个子元素。n 可以是数字,或关键字odd、even或公式。常见使用方法如下:

<table>
    <tbody>
        <tr>
            <td>name</td>
            <td>gender</td>
            <td>age</td>
        </tr>
        <tr>
            <td>George</td>
            <td>Male</td>
            <td>23</td>
        </tr>
        <tr>
            <td>Kevin</td>
            <td>Male</td>
            <td>28</td>
        </tr>
        <tr>
            <td>Angule</td>
            <td>Male</td>
            <td>23</td>
        </tr>
    </tbody>
</table>
<style>
    tr:nth-child(even) {
        background-color: red; 
    }
</style>
Copy after login

:nth-last-child(n)

与 :nth-child(n)用法相同,只是排序方式是从后往前!

:nth-of-type(n)

用于定义相同元素的第 n 个元素的样式,常见使用方法如下:

<table>
    <tbody>
        <tr>
            <td>name</td>
            <td>gender</td>
            <td>age</td>
        </tr>
        <tr>
            <td>George</td>
            <td>Male</td>
            <td>23</td>
        </tr>
        <tr>
            <td>Kevin</td>
            <td>Male</td>
            <td>28</td>
        </tr>
        <tr>
            <td>Angule</td>
            <td>Male</td>
            <td>23</td>
        </tr>
    </tbody>
</table>
<style>
    tr:nth-of-type(even) {
        background-color: red; 
    }
</style>
Copy after login

:nth-last-of-type(n)

与 :nth-of-type(n)用法相同,只是排序方式是从后往前!

:last-child

用于定义最后一个元素的样式,常见使用方法如下:

<table>
    <tbody>
        <tr>
            <td>name</td>
            <td>gender</td>
            <td>age</td>
        </tr>
        <tr>
            <td>George</td>
            <td>Male</td>
            <td>23</td>
        </tr>
        <tr>
            <td>Kevin</td>
            <td>Male</td>
            <td>28</td>
        </tr>
        <tr>
            <td>Angule</td>
            <td>Male</td>
            <td>23</td>
        </tr>
    </tbody>
</table>
<style>
    tr:last-child {
        background-color: red; 
    }
</style>
Copy after login

:first-of-type

定义第一个元素相同类型元素的样式,与 :nth-of-type(1) 效果一样

:last-of-type

定义最后一个元素相同类型元素的样式,常见使用方法如下:

<table>
    <tbody>
        <tr>
            <td>name</td>
            <td>gender</td>
            <td>age</td>
        </tr>
        <tr>
            <td>George</td>
            <td>Male</td>
            <td>23</td>
        </tr>
        <tr>
            <td>Kevin</td>
            <td>Male</td>
            <td>28</td>
        </tr>
        <tr>
            <td>Angule</td>
            <td>Male</td>
            <td>23</td>
        </tr>
    </tbody>
</table>
<style>
    tr:last-of-type {
        background-color: red; 
    }
</style>
Copy after login

:only-child

用于定义子元素只有一个且与制定元素标签相同,常见使用方法如下:

<p>
    <h1>Hello</h1>
</p>
<style>
    h1:only-child {
        /*如果 p 中还有其他任何元素,则h1不会按照该选择器中定义的样式渲染*/
    }
</style>
Copy after login

:only-of-type

用于定义只包含一个制定的标签元素的样式,常见使用方法如下:

<p>
    <h1>Hello</h1>
</p>
<style>
    h1:only-of-type {
        /*如果 p 中还有其他任何元素,则h1不会按照该选择器中定义的样式渲染*/
    }
</style>
Copy after login

:empty

用于定义,一个元素中没有包含任何子元素的样式,常见使用方法如下:

<p>
    
</p>
<style>
    p:empty {
        display: none;
    }
</style>
Copy after login

CSS3 新增的其他选择器

E ~ F

用于定义兄弟元素的样式,常见使用方法如下:

<p>
    <p>Hello</p>
</p>
<p>CSS</p>
<style>
    p ~ p {
        color: red;
    }
</style>
Copy after login

p 元素中的 p 不会被渲染为红色字体,只有跟 p 是同级的 p 才会被渲染为红色!

:not(s)

用于定义指定元素,并且过滤 s 所指定的选择器元素,常见使用方法如下:

<p>
    <p class="red">Hello</p>
    <p class="blue">World</p>
    <p>Welcome!</p>
</p>
<style>
    p:not(.red) {
        color: blue;
    }
</style>
Copy after login

注意:s 是一个简单的结构选择器,不能使用复合选择器,该选择器只匹配第一个复合条件的元素。如上所示,最后一个 p 不会被渲染为蓝色!

:target

用于定义被访问的锚链样式,常见使用方法如下:

<p>
    <p id="text-one">
        <p>这是第一个文本段</p>
    </p>
    <p id="text-two">
        <p>这是第二个文本段</p>
    </p>
</p>
<style>
    p:target {
        color: red;
    }
</style>
Copy after login

注意:当我们激活锚链时(url中包含 #text-one 或 #text-two),对应的 p 内的元素字体会被渲染为红色!

CSS3 UI 元素状态伪类选择器

:enabled

用于定义元素的 enabled 时的样式,常见使用方式如下:

<p>
    <input type="text">
</p>
<style>
    input:enabled {
        background: #ffff00;
    }
</style>
Copy after login

注意:元素默认状态为 enabled

:disabled

用于定义元素处于禁用状态时的样式,常见使用方法如下:

<p>
    <input type="text" disabled="disabled"/>
</p>
<style>
    input:disabled {
        background: #dddddd;
    }
</style>
Copy after login

:checked

用于定义元素被选中时的样式,常见使用方式如下:

<p>
    <form>
        <input type="checkbox" />
    </form>
</p>
<style>
    input:checked {
        color: green;
    }
</style>
Copy after login

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

你不知道的冷门CSS属性

href和src、link和@import有什么区别

css的绝对定位怎么兼容所有的分辨率

The above is the detailed content of Detailed explanation of CSS Selector usage. For more information, please follow other related articles on the PHP Chinese website!

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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 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)

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

How to view the date of bootstrap How to view the date of bootstrap Apr 07, 2025 pm 03:03 PM

Answer: You can use the date picker component of Bootstrap to view dates in the page. Steps: Introduce the Bootstrap framework. Create a date selector input box in HTML. Bootstrap will automatically add styles to the selector. Use JavaScript to get the selected date.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

See all articles