Home Web Front-end CSS Tutorial What is a css selector? What types of css selectors are there?

What is a css selector? What types of css selectors are there?

Sep 05, 2018 pm 03:55 PM
css Selector

css selector is undoubtedly very important in learning css, so, What is css selector? What types of css selectors are there? This is what we must master. This article will introduce to you the basic definition of css selectors and what types of css selectors there are.

What is a css selector? What types of css selectors are there?

Without further ado, let’s go directly to the topic~~

What is the css selector?

In Baidu Encyclopedia, we can see that the basic definition of css selector is: each css style definition consists of two parts, the form is as follows: [code] Selector {style} [/code ] The part before {} is the "selector". After saying such a long paragraph, in short, it is to use css to achieve one-to-one, one-to-many or many-to-one control of elements in the html page. This requires the use of CSS selectors, html pages The elements in are controlled through CSS selectors. (Recommended video tutorial: CSS tutorial)

Next we will look at a picture, which is a specific analysis of the above definition.

What is a css selector? What types of css selectors are there?

As shown in the figure, we can know:

The part before {} is the "selector", and the "selector" specifies the elements in {} The object of "style", that is, which elements in the web page are affected by "style"

The selector is usually the HTML element you need to change the style, such as:

,

,< ;h1>Waiting

Each format declaration statement consists of a pair of "attribute name: attribute value". The attribute name and attribute value are separated by colons. Each declaration statement is preceded by a semicolon in English "; "Finish.

After introducing what a css selector is, let’s take a look at what types of css selectors there are?

What are the types of css selectors?

There are many types of css selectors. Let’s take a look at the types of css selectors

1. Tag selectors (such as: body, div,p,ul,li).

2. Class selector (such as: class="head", class="head_logo").

3. ID selector (such as: id="name", id="name_txt").

4. Global selector (such as: *).

5. Combination selectors (such as: .head .head_logo, please note that the two selectors are separated by the space bar).

6. Inherit the selector (such as: div p, note that the two selectors are separated by the space bar).

7. Pseudo-class selector (for example: link style, pseudo-class of a element, 4 different states: link, visited, active, hover.).

8. Attribute selectors for string matching (^ $ * three types, corresponding to start, end, and inclusion respectively).

The most commonly used CSS selectors among the above eight CSS selectors are label selectors, .class selectors, ID selectors, attribute selectors, and pseudo-class selectors.

Below we will give an example of each of these five css selectors: (For other selector examples, please refer to css manual)

(1) Tag selector:

<html>
<head>
<meta charset="UTF-8">
<title>标签选择器</title>
<style type="text/css">
span{color: green;font-size: 20px}
</style>
</head>
<body>
<span>hello</span><br/>
<span>world</span>
</body>
</html>
Copy after login

The effect is as follows:

What is a css selector? What types of css selectors are there?

## (2) Class selector:

<html>
<head>
<meta charset="UTF-8">
<title>class选择器</title>
<style type="text/css">
    .style1{background-color: green}
    .style2{background-color: yellow}
</style>
</head>
<body>
    <div class = "style1">div1</div>
    <div class = "style1">div2</div>
    <div class = "style2">div3</div>
</body>
</html>
Copy after login

The effect is as follows:

What is a css selector? What types of css selectors are there?

(3) ID selector:

<html>
<head>
<meta charset="UTF-8">
<title>ID选择器</title>
<style type="text/css">
    #div1{background-color: yellow}
    #div2{background-color: green
    }
</style>
</head>
<body>
    <div id = "div1">div1</div>
    <div id = "div1">div1</div>
    <div id = "div2">div2</div>
</body>
</html>
Copy after login

The effect is as follows:

What is a css selector? What types of css selectors are there?

(4) Attribute selector:

<html>
<head>
<meta charset="UTF-8">
<title>属性选择器</title>
<style type="text/css">
    input[type = &#39;text&#39;] {background-color: gray}
    input[type = &#39;password&#39;] {background-color: pink}
</style>
</head>
<body>
    <form>
        name:<input type = "text"><br/>
        pass:<input type = "password">
    </form>
</body>
</html>
Copy after login

The effect is as follows:

What is a css selector? What types of css selectors are there?

(5) Pseudo-class selector:

<html>
<head>
<meta charset="UTF-8">
<title>伪类选择器</title>
    <style type="text/css">
        a:link{color:yellow ;font-size: 50px}
        a:hover{color:green;font-size: 50px}
        a:active{color:blue;font-size: 50px}
        a:visited{color:red;font-size: 50px}
    </style>
</head>
<body>
    <a href = "http://www.php.cn">点击</a>
</body>
</html>
Copy after login
Effect description:


The label is yellow when the web page is opened

When the mouse is placed on the label, the label It is green
The label is blue when the label is clicked
The label is red after clicking

Related recommendations:

css class selector and id selector

CSS selector organization

The above is the detailed content of What is a css selector? What types of css selectors are there?. 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 尊渡假赌尊渡假赌尊渡假赌

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

See all articles