Home  >  Article  >  Web Front-end  >  Detailed explanation of CSS3 complex selectors

Detailed explanation of CSS3 complex selectors

高洛峰
高洛峰Original
2017-03-24 11:22:241566browse

Today I finished watching the CSS3 complex selector part in the video, and let’s sort out the knowledge points I learned.

1. Sibling selector: at the same position level, it can be called a sibling element

a, Adjacent sibling selector:next
Following [the one immediately after the current element] (one), specifying the selector element
Syntax: Use "+" as the conjunctive symbol
eg: p+p ->p immediately following p Element

<!-- demo.html -->
<html>
    <head>
        <title></title>
    </head>
    <body>
        <p>这是第一个段落</p>
    <div id="d1">这是一个div</div>
    <span>这是一个span</span>
        <p class="p1">这是第二个段落</p>
    <b>Hello World</b>
    <p class="p2">这是第三个段落</p>
    </body>
</html>

/*demo.css*/
div+p{
    background: yellow;
}
#d1+p{
    background: red;
}
span+.p1{
    background: blue;
}

b, Universal sibling selector: next_all
Matches [all subsequent] sibling elements of a certain element that satisfy the specified selector
Syntax: Use "~" as the conjugator
eg: p~p{} -> Match all p

2, Attribute selector: Use the element attached Attributes, used in selectors as conditions for selecting elements
Syntax: [attribute-related content]
eg: [id] ->All elements with id attributes
p[id] -> ;p element with id attribute
a.[id],p[id]
b.p[id][class] ->p element with both id and class
c, p[id="p1"] ->p element with id value "p1"
d, p[class~="value"]
e, p[class^=" b"] -> Match the p tag whose class attribute value starts with b
f, p[class*="b"] -> Match the p tag whose class attribute value contains b
g, p[ class$="b"] -> Match the p tag whose class attribute value ends with b

<!-- demo.html -->     
 <html>
     <head>
         <title></title>
     </head>
     <body>
        <p class="clear p1 myself">
        这是第四个段落
        </p>
        <div class="userContent">
        文本内容
        </div>
     </body>
</html>

/*demo.css*/
p[class]{
color: #e4393c;
}
p[class~=&#39;p1&#39;]{
background-color: #cd2c2d;
color: #fff;
}
div[class ^= "us"]{
background-color: #bfb;
}
div[class$="t"]{
background-color: #bfb;
color: #333;
}

3. PseudoClass selector
a.Targetpseudo class : Highlight active HTMLAnchor point
Syntax::target
b. Element StatePseudo class: Mostly used on form elements
1 , :enabled -> Matches every enabled element
2. :disabled -> Matches every disabled element
3. :checked -> Matches selected form elements (only Applicable to checkbox, radio)
c. Structural pseudo-class
1. :first-child -> Matches the first child element that belongs to its parent element
2.: last-child -> Matches the last child element belonging to its parent element
3. :empty -> Matches elements that have no child elements (text content or spaces are also counted as child elements)
4.: only-child -> Matches the only child element that belongs to its parent element
d. Negative pseudo-class: matches elements with non-specified selectors
Syntax::not(selector)

<!-- demo01.html 目标伪类 -->
 <html>
     <head>
         <title></title>
     </head>
     <body>
    <a href="#Tom">猫和老鼠(Tom and Jerry)</a>
    <a href="#Atongmu">铁臂阿童木</a>
    <a href="#BlackCat">黑猫警长</a>
    <br>
    <a name="Tom">第一部:Tom and Jerry</a>
    <p style="height: 500px;">Tom and Jerry</p>
    <div id="Atongmu" style="height: 500px;">我是阿童木</div>
    <div id="BlackCat" style="height: 500px;">I am Mr Black Cat</div>    
     </body>
</html>

/*demo01.css*/
a:target,div:target{
    background-color: #bfb;
    font-size: 20pt;
}
<!-- demo02.html 结构伪类 -->     
 <html>
     <head>
         <title></title>
     </head>
     <body>
    <div id="d1"></div>
    <div id="d2">
        <p>This is a p</p>
    </div>
    <div id="d3">
        This id d3
    </div>
    <div id="d4">
        <b>first</b>
        <b>second</b>
        <b>third</b>
        <b>last</b>
    </div>    
     </body>
</html>

/*demo02.css*/
div{
    width: 100px;
    height: 100px;
}
b{
    display: block;
}
div:empty{
    background-color: #bfb;
}
p:only-child{
    background-color: #fbf;
}
b:first-child{
    font-size: 2em;
    color: #fbb;
}
b:last-child{
    font-size: 3em;
    font-weight: normal;
    color: #bbf;
}
<!-- demo03.html 伪元素状态伪类 -->
  <html>
      <head>
          <title></title>
      </head>
      <body>
         用户名称:<input type="text"><br>
    用户昵称:<input type="text" disabled value="请输入您的昵称">
    <br>
    性别:<input type="radio" name="rdoGender">男
              <input type="radio" name="rdoGender">女 
      </body>
 </html>
 
 /*demo03.css*/
 input:enabled{
    color: red;
}
input:disabled{
    border: 1px solid #f00;
}
input[name=rdoGender]:checked{
    background-color: #bfb;
}
<!-- demo04.html 否定伪类 -->
  <html>
      <head>
          <title></title>
      </head>
      <body>
         <div>
        用户名称:<input type="text"><br>
        用户密码:<input type="password"><br>
        <input type="submit" value="提交">
        <input type="button" value="按钮">
    </div>
      </body>
 </html>
 
 /*demo04.css*/
input:not(:last-child){
    border: 1px solid #f00;
}

4 , Pseudoelement selector: All matched are text contents
a.:first-letter ->Match the first character
b.:first -line -> Match the first line
The above two selectors, inline elements are invalid, inline blocks and block levels can
c.::selection ->Used for the text style selected by European compensation users (Firefox seems to be incompatible)

<!-- demo.html 为元素选择器 -->
  <html>
      <head>
          <title></title>
      </head>
      <body>
         <p>
        风风雨雨适合于独行,且手中无伞,不打伞有不打伞的好处。湿是我的湿,冷是我的冷,即便化作雨点般的小,那么小也是我的小。
    </p>
    <span>
        风风雨雨适合于独行,且手中无伞,不打伞有不打伞的好处。湿是我的湿,冷是我的冷,即便化作雨点般的小,那么小也是我的小。
    </span>
      </body>
 </html>
 
 /*demo.css*/
p{
    width: 200px;
    border: 1px solid #bfb;
    margin: 10% auto;
    text-indent: 5px;
}
span{
    /*float: right;*/
    /*display: inline-block;*/
    position: absolute;
    top: 300px;
    left: 500px;
    
}
p:first-letter{
    font-size: 20pt;
    color: #fbb;
}
p:first-line{
    font-style: italic;
}
span:first-line{
    font-style: italic;
    background-color: #ffb;
}
p::selection{
    background-color: #bbf;
    color: #fbf;
}

The above is everything I learned in the video. If there are any errors or deficiencies, I hope viewers will point them out and correct them in time. .

Today is the second day since I opened my blog. This is the first article I have written. I publish it here purely as my own study notes, hoping to record my growth.

The above is the detailed content of Detailed explanation of CSS3 complex selectors. 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