Home Web Front-end CSS Tutorial What selectors are there in css?

What selectors are there in css?

Apr 14, 2021 pm 07:03 PM
css Selector

CSS selectors include: 1. Category selector; 2. Tag selector; 3. ID selector; 4. Descendant selector; 5. Sub-selector; 6. Pseudo-class selector; 7 , Universal selector; 8. Group selector; 9. Adjacent sibling selector; 10. Attribute selector; 11. Pseudo element selector.

What selectors are there in css?

The operating environment of this tutorial: Windows7 system, CSS3&&HTML5 version, Dell G3 computer.

css selector introduction:

1. Category selector

The class selector selects based on the class name, preceded by "." .

Example:

.demoDiv{
color:#FF0000;
}
Copy after login

2. Tag selector

A complete HTML page is composed of many different tags, and the tag selector determines which tags are used Corresponding CSS styles.

The declaration of the p tag style in the style.css file is as follows:

p{
font-size:12px;
background:#900;
color:090;
}
Copy after login

3. ID selector

The ID selector can be an HTML element marked with a specific ID Specify a specific style. Select elements based on element ID, which is unique, which means that the same ID can only appear once in the same document page.

is preceded by a "#" sign and can be defined in the style as follows:

#demoDiv{
color:#FF0000;
}
Copy after login

4. Descendant selector

Descendant selector is also called inclusion The selector is used to select the descendants of a specific element or element group. The selection of the parent element is placed in front and the selection of child elements is placed in the back, separated by a space in the middle.

<style>
.father.child{
color:#0000CC;
}
</style>
<p class="father">
黑色
<label class="child">蓝色
<b>也是蓝色</b>
</label>
</p>
Copy after login

5. Child selector

Please note the difference between this selector and the descendant selector. The child selector only refers to its direct descendants, or your It can be understood as acting on the first descendant of the child element. The descendant selector acts on all child descendant elements. Descendant selectors select with spaces, while child selectors select with ">".

Let’s look at the following code:

Example Source Code

CSS:

#links a {color:red;}
#links > a {color:blue;}
Copy after login

HTML:

<p id="links">
<a href="#">HTML中文网</a>>
<span><a href="#">CSS布局实例</a></span>
<span><a href="#">CSS教程</a></span>
</p>
Copy after login

6. Pseudo-class selection The handler

Sometimes you need to use other conditions outside the document to apply the style of the element, such as mouse hover, etc. At this time we need to use pseudo classes. The following is the pseudo-class definition for the linked application.

a:link{
color:#999999;
}
a:visited{
color:#FFFF00;
}
a:hover{
color:#006600;
}
/* IE不支持,用Firefox浏览可以看到效果 */
input:focus{
background:# E0F1F5;
}
Copy after login

7. Universal selector

The universal selector is represented by *. For example:

*{
font-size: 12px;
}
Copy after login

means that the font size of all elements is 12px; at the same time, the universal selector can also be combined with descendant selectors.

8. Group selector

When several elements have the same style attributes, they can call a statement together and separate the elements with commas. For example:

p, td, li {
line-height:20px;
color:#c00;
}
#main p, #sider span {
color:#000;
line-height:26px;
}
.#main p span {
color:#f60;
}
.text1 h1,#sider h3,.art_title h2 {
font-weight:100;
}
Copy after login

Using the group selector will greatly simplify the CSS code. Elements with multiple identical attributes can be merged into groups for selection, and the same CSS attributes can be defined, which greatly improves coding. efficiency, and also reduces the size of CSS files.

9. Adjacent sibling selector

In addition to the above child selector and descendant selector, we may also want to find one of the two brothers, such as a title h1 The element is followed by two paragraph p elements. We want to locate the first paragraph p element and apply styles to it. We can use the adjacent sibling selector.

10. Attribute selector

You can define css by judging whether a certain attribute of the html tag exists.

Attribute selector is matched based on the attributes of the element. Its attributes can be standard attributes or custom attributes

11. Pseudo-element selector

All pseudo-element selectors must be placed at the end of the selector in which the pseudo-element appears, which means that the pseudo-element selector cannot be followed by any derived selector.

Recommended learning: css video tutorial

The above is the detailed content of What selectors are there in css?. 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

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
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 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
1664
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
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.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

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

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 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 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 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-

See all articles