Home >Web Front-end >CSS Tutorial >What are the css selectors? Summary of css selector usage

What are the css selectors? Summary of css selector usage

不言
不言Original
2018-08-18 14:40:022087browse

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