Home Web Front-end CSS Tutorial What types of css selectors are there? A brief introduction to common css selectors

What types of css selectors are there? A brief introduction to common css selectors

Aug 09, 2018 pm 03:59 PM
css selector Common selectors

This article brings you information about how to use css selectors. What types are there? A brief introduction to commonly used CSS selectors has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

css tag selector

Function: Select all specified elements in the page

Syntax: Tag name: {}

id selector

Function: drill the only element in the element through its id attribute value

Syntax: #id{}

css Class selector

Function: Select a group of elements through the element's class attribute value

Syntax: .class attribute value{}

can be set for one element at the same time Multiple class attribute values, separated by spaces

Selector grouping (union selector)

Function: Selector grouping can be used at the same time Select elements corresponding to multiple selectors

Syntax: Selector 1, Selector 2, Selector N{}

Example: Select the id as p3, and the class attribute value contains p2 and h1 tags

1

2

3

#p3,.p2,h1{

                 background-color: yellow;

           }

Copy after login

css wildcard selector

Function: Select all elements on the page

Syntax: *{}

css intersection selector (composite selector)

Function: You can select elements that satisfy multiple selectors at the same time

Syntax: Selector 1 Selector 2 Selector N{ }

Note: Because id can uniquely determine an element, do not use the intersection selector for id

Example: Select one of the span

1

2

3

span.p4{

                                    background-color:#4169E1;

                           }

Copy after login

elements whose class attribute value contains p4 Relationship between:

Parent element: an element that directly contains child elements.

Child elements: Elements directly contained by the parent element.

Ancestor element: An element that directly or briefly contains descendant elements. The parent element is also an ancestor element.

Descendant elements: Elements that are directly or indirectly contained by ancestor elements, and child elements are also descendant elements.

Sibling elements: Elements that have the same parent element.

css descendant element selector

Function: Select the descendant elements of the specified element

Syntax: Ancestor element descendant element{}

Example: Select span in div

1

2

3

div span {

                                      color: chartreuse;

                              }

Copy after login

css child element selector (not supported by IE6 and below browsers)

Function: Select the specified parent element Child element

Syntax: Parent element > Child element

Example: Select the span in the div

1

2

3

div>span{

             background-color: yellow;

    }

Copy after login

Pseudo-class selector is used to represent a special type of element status.

For example: visited hyperlinks, ordinary hyperlinks, and focused text boxes

When we need to set styles for elements in these special states, we can use them The style defined by the pseudo class for the connection

Normal link: a:link

Visited link: a:visited (only color can be defined)

The link that the mouse slides over : a:hover

The link being clicked: a:active

The order between a:link and a:visited is not specified, but they must be in front of a:hover and a:active , a:hover must be in front of a:active

hover and active can also be set for other elements such as p:hover p:active, but ie6 and below do not support it

Other pseudo Class:

:focus Get focus

:before Before the specified element

:after After the specified element

::selection The selected element ( This should be used in Firefox::-moz-selection)

Use pseudo-elements to represent some special positions in the element

:first-letter: the first character

:fist-line : The first line of characters

:before : Represents the front part of the element

Generally, before needs to be used in conjunction with the content style,

You can add some content to the position of before or after through content

:after : represents the last side of the element

Set the first character in the p tag to yellow 25px

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

p:first-letter{

                     color:yellow;

                     font-size: 25px;

                 }

                p:first-line{

                    background: #FF0000;

                }

               将content的内容添加到p元素的最前面

                p:before{

                    content: "ABC";

                }

                将content的内容添加到p元素的最后面

                p:after{

                    content: "DEF";

                }

Copy after login

css attribute selector

Function: You can select the specified element based on the attribute or attribute value in the element

Syntax: [Attribute name] selection Elements with specified attributes

[Attribute name=Attribute value]Select elements with specified attribute values

[Attribute name^=Attribute value]Select elements whose attribute values ​​start with the specified content

[Attribute name$=Attribute value]Select elements whose attribute value ends with the specified content

[Attribute value*=Attribute value]Select elements whose attribute value contains the specified content

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

/*为具有title属性的p元素设置背景颜色*/

            p[title]{

                 color: darkorchid;

            }

            /*为title属性值为hello的元素设置一个背景颜色*/

            p[title=hello]{

                 background-color: cornflowerblue;

            }

            /*为title属性是ab开头的元素设置一个背景颜色*/

            p[title^="ab"]{

                 background-color: chartreuse;

            }

            p[title$="d"]{

                 font-size: 28px;

            }

Copy after login

Pseudo-class child element selector

:first-child: The first child element can be selected

:last-child: The last element can be selected

:nth-child : You can select a child element at any position

You can specify a parameter after this selector to specify which element to select

even: even number

odd: odd number Similar, but xxx-child selects from all elements, xxx-of-type selects from the specified type

Example: Select the first p tag

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

p:first-child{

                           color:coral;

                      }

                     选中第3个p标签

                     p:nth-child(3){

                           color:chartreuse;

                     }

                     设置表格奇偶行背景颜色不同

                     tr:nth(even){

                                background-color:pink;

                     }

                     

                     tr:nth(odd){

                                background-color:skyblue;

                     }

Copy after login

and select the next sibling element Device

Function: You can select the specified sibling element immediately after an element

语法:前一个+后一个

例:选中p标签后的相邻的兄弟span(p和span不一定相邻)

1

2

3

p+span{

                                   color:red;

                          }

Copy after login

选中后边的所有兄弟元素

语法:前一个~后边所有

否定伪类:

作用:从选种的元素中剔除某些元素

语法: :not(选择器)

例:为所有的p元素设置一个背景颜色,出了class为hello或hello2的元素

1

2

3

p:not(.hello):not(.hello2){

                    background-color: antiquewhite;

            }

Copy after login

相关文章推荐:

什么是css?css三种样式以及文字属性的介绍

CSS布局有哪些?css常见的布局方式(附代码)

The above is the detailed content of What types of css selectors are there? A brief introduction to common css selectors. 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
1 months 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 resize HTML textbox How to resize HTML textbox Feb 20, 2024 am 10:03 AM

Setting the size of HTML text boxes is a very common operation in front-end development. This article explains how to set the size of a text box and provides specific code examples. In HTML, you can use CSS to set the size of a text box. The specific code is as follows: input[type="text&quot

How to adjust a WordPress theme to avoid misaligned display How to adjust a WordPress theme to avoid misaligned display Mar 05, 2024 pm 02:03 PM

How to adjust WordPress themes to avoid misaligned display requires specific code examples. As a powerful CMS system, WordPress is loved by many website developers and webmasters. However, when using WordPress to create a website, you often encounter the problem of theme misalignment, which affects the user experience and page beauty. Therefore, it is very important to properly adjust your WordPress theme to avoid misaligned display. This article will introduce how to adjust the theme through specific code examples.

What is css selector priority What is css selector priority Apr 25, 2024 pm 05:30 PM

CSS selector priority is determined in the following order: Specificity (ID > Class > Type > Wildcard) Source order (Inline > Internal style sheet > External style sheet > User agent style sheet) Declaration order (latest declarations take precedence) Importance (!important forces the priority to increase)

What are the elements in the excluded section of css selector What are the elements in the excluded section of css selector Apr 06, 2024 am 02:42 AM

The :not() selector can be used to exclude elements under certain conditions, and its syntax is :not(selector) {style rule}. Examples: :not(p) excludes all non-paragraph elements, li:not(.active) excludes inactive list items, :not(table) excludes non-table elements, div:not([data-role="primary"]) Exclude div elements with non-primary roles.

What exactly does H5 page production mean? What exactly does H5 page production mean? Apr 06, 2025 am 07:18 AM

H5 page production refers to the creation of cross-platform compatible web pages using technologies such as HTML5, CSS3 and JavaScript. Its core lies in the browser's parsing code, rendering structure, style and interactive functions. Common technologies include animation effects, responsive design, and data interaction. To avoid errors, developers should be debugged; performance optimization and best practices include image format optimization, request reduction and code specifications, etc. to improve loading speed and code quality.

css selectors which are advanced selectors css selectors which are advanced selectors Oct 07, 2023 pm 02:59 PM

Advanced selectors in CSS selectors include descendant selectors, child element selectors, adjacent sibling selectors, universal sibling selectors, attribute selectors, class selectors, ID selectors, pseudo-class selectors and pseudo-element selectors wait. Detailed introduction: 1. The descendant selector uses a space-separated selector to select the descendant elements of an element; 2. The child element selector uses a selector separated by a greater than sign to select the direct child elements of an element; 3. Adjacent sibling selectors use selectors separated by a plus sign to select the first sibling element immediately following an element, and so on.

Get a deep understanding of the weight and precedence of CSS selector wildcards Get a deep understanding of the weight and precedence of CSS selector wildcards Dec 26, 2023 pm 01:36 PM

In-depth understanding of the weight and priority of CSS selector wildcards In CSS style sheets, selectors are an important tool for specifying which HTML elements the style applies to. The selector's priority and weight determine which style is applied when multiple rules apply to an HTML element at the same time. Wildcard selectors are a common selector in CSS. It is represented by the "*" symbol, which means it matches all HTML elements. Wildcard selectors are simple but can be very useful in certain situations. However, the weight and precedence of wildcard selectors also

Learn the basic syntax of using CSS selectors Learn the basic syntax of using CSS selectors Jan 13, 2024 am 11:44 AM

To master basic CSS selector syntax, specific code examples are required. CSS selectors are a very important part of front-end development. They can be used to select and modify various elements of HTML documents. Mastering basic CSS selector syntax is crucial to writing efficient stylesheets. This article will introduce some common CSS selectors and corresponding code examples. Element selector The element selector is the most basic selector, which can select the corresponding element by its tag name. For example, to select all paragraphs (p elements), you can use

See all articles