search
HomeWeb Front-endCSS TutorialWhat are the css selectors? Summary of css selector usage

This article brings you what are the css selectors? A summary of the use of CSS selectors has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. Selector (device) uses basic syntax
Selector { Attribute 1: Attribute value 1;
Attribute 2: Attribute value 2;
Attribute 3: Attribute value 3;
       …..: …….;
}
2. Tag selector (directly use HTML tags as selectors)
second.html

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>标记选择器</title>
    <link href="second.css" type="text/css"   rel="stylesheet">
</head>
<body>
    <p> 标记选择器(直接使用html标签做选择器)</p>
</body>
</html>

second.css

p{
    font-size:34px;   
    text-align:left;    
    text-transform:uppercase;    
    font-family:黑体,宋体;    
    text-indent:3em;    
    text-decoration:line-through;    
    font-weight:bolder;    
    letter-spacing:0.5em;    
    line-height:3px;    
    margin:34px;    
    padding:23px;
    }

3. Class selector (for each The label sets a class name. CSS can set the style of the label through the class name. Different labels can have the same class name, and they can share the same style.) - In CSS, label name.class name or .class name is used to express

second.html

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>类选择器</title>
    <link href="second.css" type="text/css"   rel="stylesheet">
</head>
<body>
    <p  class="text"> 类选择器(为每一个标签设置一个类名,CSS可通过类名为标签设置样式,不同标签类名可以相同,
    相同可以共用同一个样式)---CSS中用标签名.类名或者.类名表示    </p>
</body>
</html>

second.css

.text{
    font-variant:small-caps;/*小型大写字母*/
    font-family:宋体,黑体;    
    font-style:italic;    
    text-transform:lowercase;    
    font-size:35px;    
    text-indent:3em;    
    text-decoration:overline;    
    font-weight:bold;    
    letter-spacing: 1px;    
    line-height:3em;    
    font-variant:small-caps;    
    text-align:right;
    }

4.ID selector (set a unique ID for each HTML tag, CSS can set the style for the tag through the id)
——#id value { } is used in CSS to represent

second.html

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>ID选择器</title>
    <link href="second.css" type="text/css"   rel="stylesheet">
    </head>
    <body>
    <p  class="text" id="textp1"> ID选择器(为每一个HTML标签设置一个唯一的ID,CSS可以通过id来为标签设置样式)
    ----CSS中用#id值{ }来表示    </p>
    </body>
    </html>

second.css

.text{
    font-variant:small-caps;/*小型大写字母*/
    font-family:宋体,黑体;    
    font-style:italic;    
    text-transform:lowercase;    
    font-size:35px;    
    text-indent:3em;    
    text-decoration:overline;
    }
#textp1{
    font-weight:bold;    
    letter-spacing: 1px;    
    line-height:3em;    
    font-variant:small-caps;    
    text-align:right;
    }

5. Contains selectors (descendant selectors: can span generation, cross tags): Contain another tag in one tag
second.html

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>包含选择器</title>
    <link href="second.css" type="text/css"   rel="stylesheet">
    </head>
    <body>
    <p  class="text" id="textp1">  <a href="#" class="link">ID选择器</a>
        (为每一个html标签设置一个唯一的ID,CSS可以通过id来为标签设置样式)
     ----CSS中用#id值{ }来表示    </p>
     </body>
     </html>

second.css

p a{
    
    text-decoration:none;    
    font-size:25px;    
    background-color:pink;
    }
.text{
    font-size:21px;    
    font-style:normal;    
    text-transform:lowercase;    
    text-indent:3em;    
    font-weight:bold;    
    letter-spacing:0.7em;    
    line-height:27px;
    }

6. Sub-selector (not supported by IE browser): No other tags can be included in the middle; in CSS, tag 1 > tag 2 (sub-tag) is used to represent

second.html

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>子选择器</title>
    <link href="second.css" type="text/css"   rel="stylesheet">
    </head>
    <body>
    <p  class="text" id="textp1"> 
             <a href="#" class="link">子选择器</a>
            (IE浏览器不支持):中间不能包含其他标签;CSS中 标签1 >标签2(子标签) 表示 
    </p>
    </body>
    </html>

second.css

p>a{
    text-decoration:none;    
    font-family:黑体,宋体;
    }
p{    
font-size: 23px;
}

7. Adjacent Selector: (CSS style is applied to the last tag) Commonly used to set column attributes of the table – CSS uses tag 1….Tag n represents

second.html

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>相邻选择器</title>
    <link href="second.css" type="text/css"   rel="stylesheet">
    </head>
    <body>
    <p  class="text" > 
            子选择器:
            (IE浏览器不支持):中间不能包含其他标签;CSS中 标签1 >标签2(子标签) 表示 
    </p>

    <p class="text1">
        相邻选择器(CSS样式作用在最后一个标签)常用设置表格的列属性--CSS用标签1+....标签n表示    </p>
        </body>
        </html>

second.css

.text+.text1{
    color:pink;    
    background-color:gray;    
    font-size:34px;
    }

8. Union selector: Set styles for multiple tags together, separated by commas

second.css

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>并集选择器</title>
    <link href="second.css" type="text/css"   rel="stylesheet">
    </head>
    <body>
    <p  class="text" > 
            子选择器:
            (IE浏览器不支持):中间不能包含其他标签;CSS中 标签1 >标签2(子标签) 表示 
    </p>

    <p class="text1">
        并集选择器:多个标签一起设置样式,用逗号隔开    </p>
        </body>
        </html>

second.css

.text,.text1{
    color:pink;
    background-color:gray;    
    font-size:34px;}

9. Pseudo class (a certain state of an element, often used to describe the state of a hyperlink: link (unvisited state); visited (visited state); hover (mouse passing state); active (clicking state) CSS tags Name: Status representation

second.html

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>伪类</title>
    <link href="second.css" type="text/css"   rel="stylesheet">
    </head>
    <body>
    <a href="" class="a1">伪类</a>
    </body>
    </html>

second.css

a:link {
    color:blue;    
    font-size:53px;    
    text-decoration: none;
    }
a:visited {
    color:red;    
    font-size:193px;
    }
a:hover  {
    color:green;    
    font-size:73px;
    }
a:active  {
    color:gray;    
    font-size:183px;
    }

10. Pseudo element (state of an element in an object) tag name in CSS : Pseudo-class representation
Commonly used ones are (first-letter [acts on the first letter of text], first-line [acts on the first line of text])
second.html

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>伪类</title>
    <link href="second.css" type="text/css"   rel="stylesheet">
    </head>
    <body>
    <p>伪元素(某个对象中某个元素的状态)CSS中用标签名:
 伪元素常用的有(first-letter[作用于文字首字],first-line[作用于文字首行])  
 <br>伪元素(某个对象中某个元素的状态)CSS中用标签名:
 伪元素常用的有(first-letter[作用于文字首字],first-line[作用于文字首行])    </p>
 </body>
 </html>

second.css

p:first-letter{
    font-size:200%;
    }
p:first-line{
    color:red;
    }

11 Style priority: Inline selector>ID selector>Class selector (pseudo-class, attribute)>Mark selector
You can use !important; to increase the priority
Related recommendations:

css class selector and id selector

##CSS descendant selector and multi-class selector

css tag selector, id selector, class selector instance

The above is the detailed content of What are the css selectors? Summary of css selector usage. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
Two Images and an API: Everything We Need for Recoloring ProductsTwo Images and an API: Everything We Need for Recoloring ProductsApr 15, 2025 am 11:27 AM

I recently found a solution to dynamically update the color of any product image. So with just one of a product, we can colorize it in different ways to show

Weekly Platform News: Impact of Third-Party Code, Passive Mixed Content, Countries with the Slowest ConnectionsWeekly Platform News: Impact of Third-Party Code, Passive Mixed Content, Countries with the Slowest ConnectionsApr 15, 2025 am 11:19 AM

In this week's roundup, Lighthouse sheds light on third-party scripts, insecure resources will get blocked on secure sites, and many country connection speeds

Options for Hosting Your Own Non-JavaScript-Based AnalyticsOptions for Hosting Your Own Non-JavaScript-Based AnalyticsApr 15, 2025 am 11:09 AM

There are loads of analytics platforms to help you track visitor and usage data on your sites. Perhaps most notably Google Analytics, which is widely used

It's All In the Head: Managing the Document Head of a React Powered Site With React HelmetIt's All In the Head: Managing the Document Head of a React Powered Site With React HelmetApr 15, 2025 am 11:01 AM

The document head might not be the most glamorous part of a website, but what goes into it is arguably just as important to the success of your website as its

What is super() in JavaScript?What is super() in JavaScript?Apr 15, 2025 am 10:59 AM

What's happening when you see some JavaScript that calls super()?.In a child class, you use super() to call its parent’s constructor and super. to access its

Comparing the Different Types of Native JavaScript PopupsComparing the Different Types of Native JavaScript PopupsApr 15, 2025 am 10:48 AM

JavaScript has a variety of built-in popup APIs that display special UI for user interaction. Famously:

Why Are Accessible Websites so Hard to Build?Why Are Accessible Websites so Hard to Build?Apr 15, 2025 am 10:45 AM

I was chatting with some front-end folks the other day about why so many companies struggle at making accessible websites. Why are accessible websites so hard

The `hidden` Attribute is Visibly WeakThe `hidden` Attribute is Visibly WeakApr 15, 2025 am 10:43 AM

There is an HTML attribute that does exactly what you think it should do:

See all articles

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)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.