


What types of css selectors are there? A brief introduction to common css 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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
语法:前一个+后一个
例:选中p标签后的相邻的兄弟span(p和span不一定相邻)
1 2 3 |
|
选中后边的所有兄弟元素
语法:前一个~后边所有
否定伪类:
作用:从选种的元素中剔除某些元素
语法: :not(选择器)
例:为所有的p元素设置一个背景颜色,出了class为hello或hello2的元素
1 2 3 |
|
相关文章推荐:
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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"

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.

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)

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.

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.

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.

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

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
