A brief introduction to CSS selectors
Foreword: This is the author’s own understanding and organization after studying. If there are any errors or questions, please correct them and I will continue update!
Selector weight
If the selector weight is represented by 4 digits, then:
The weight of the element selector is 1; the weight of
id selector is 100;
classThe weight of class selector is 10; the weight of
inline style (JS also operates inline style) is 1000;
Of course if a attribute value is set! important, it is still the most important, kill it directly;
Wildcard selector, *{...}, asterisk represents a wildcard selector, which can match any element;
Element selector, html{...}, body{...}, p{...}, the element of the document is the most basic selector, element selector ;
id selector, #value, set an id attribute to the element, the value is value;
class class selector, .value {...}, set a class attribute to the element, the value is value;
Attribute selector, the attribute selector is based on the attribute and attribute value of the element. There are many ways to select elements:
Simple attribute selector: img[title], get the img element with the alt attribute set; #wrapper[outter], Get the element whose id is wrapper and has the attribute wrapper;
Specific attribute selector: input[type="button"], get the input element of attribute value button; #wrapper[outter= "abc"], get the element whose id is wrapper and the value of attribute outer is abc;
Partial attribute selector: p[class ^="a"], get The p element whose class attribute value starts with a; p[class $="b"], gets the p element whose class attribute value ends with b;
GroupSelector, separate the selectors to be grouped with commas; #wrapper, #content{...}, get the element with the id of wrapper and the element with the id of content, two Get all;
Descendant selector, separate the selectors to be grouped with spaces, #wrapper #content{...}, in the element with the id of wrapper Inside the object , get the element with the id of content, may be a child element or a descendant element;
child element Selector , separate the selectors to be grouped with a greater than sign, #wrapper > #content{...}, inside the element object with the id of wrapper, Get the ## with the id of content #Child element, only search one level down; The child element selector is different from the descendant selector. The descendant selector contains the child element selector, and the descendant selector will continue to search downwards;
- Sibling element selector:
- ##Adjacent sibling selector
(IE6 and the following are not supported), separate the selectors to be grouped with a plus sign, .wrapper + .content{...}, after obtaining the element with class wrapper, the next element with class content, and The difference between the universal sibling selector is that the adjacent sibling selector only obtains the element immediately following the current element . Even if there is the same element later, it cannot be selected;
Universal sibling selector (not supported by IE7 and below), separate the selectors to be grouped with tildes, .wrapper ~ .content{...}, get class After the element that is wrapper, - all
elements with class content are different from the adjacent sibling selectors;
Pseudo class Selector (not supported by IE8 and below):
- # #:root selects the root element of the document, that is, the html element;
- input
- element
- p:nth-of-type(n), The selection is the nth
p element of its parent element. This is calculated n in the order of the p elements inside the parent element. Then get; n is an integer, and starts from 1, it can also be a formula, or it can be the keywords even (even number), odd (odd number);
- p
:first-of-type, selects the first p element of its parent element, which has the same effect as p:nth-of-type(1);
- p
:last-of-type, the selection is the last p element of its parent element, note that there is no nth in front;
- p:nth-last -of-type(n), the selection is the nth p element of its parent element, but
is counting from the last child forward , note that there is nth in front;
- p:nth-child(n), the selection is the nth
child element of its parent element, and it must be a p element, this is according to the parent element Calculate the order of the sub-elements inside the element n, and then check whether the n-th sub-element is a p element; There is a situation where the n-th sub-element may not be p, and then cannot be obtained, so it would be better to use nth-of-type(n);
- p
:first-child, the selection is its parent element The 1st element of and is a p element, the same as p:nth-child(1);
- p:last-child, selects the last element of its parent element, And it is a p element;
- p:nth-last-child(n), the selection is the nth child element of its parent element, and it must be a p element, but
is Count from the last sub-item forward, note that there is nth in front;
- There are many others...
- Pseudo element selector:
:first-letter, specifies the style of the first letter of an element, all Leading punctuation should be applied with this style along with the first letter, and can only be associated with block-level elements ;
- ## :first-line
, sets the style of the first line of text in the element, can only be associated with block-level elements;
- :after
, IE7 and below does not support it, insert the generated content at the beginning of the element content;
- :before
, IE7 and below does not support it, insert it at the beginning of the element content Finally, insert the generated content;
::selection, matching the part selected by the user; - only supports double colon writing
, only supports both color and background color Style;
The above is the detailed content of A brief introduction to 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



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.

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.

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.

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-

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

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.

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 use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text
