CSS3--選取器

來源:互聯網
上載者:User

標籤:

子項目選取器:

  div>p{background:yellow;}

相鄰的後兄弟選取器(必須相鄰)

  h1+p{padding-top:20px;}

後兄弟選取器(同級的當前元素後面的元素)

  div~p{color:red;}

屬性選取器:(屬性可以自己定義,例如:class=abc)

  [abc=value] 屬性值為value的元素

  [abc~="value1"]屬性值包含value1的元素(多個值之間用空格區分)

  [abc*="val"]模糊比對 屬性值中包含val的元素

  [abc^="val"]匹配屬性值以val開頭

  [abc$="val"]匹配屬性值以val結尾

  [abc=val]屬性值為val的元素

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>css3選取器 | 屬性選取器</title>
  <style type="text/css">
    /*li { border: 1px solid red; }*/
    /*li.active { border: 1px solid red; }*/
    /*li[class] { border: 1px solid red; }*/
    /*li[class=‘active‘] { border: 1px solid red; }*/
    /*li[class=‘active clear‘] { border: 1px solid red; }*/
    /*li[class~=‘active‘] { border: 1px solid red; }*/
    /*li[abc] { border: 1px solid red; }*/
    /*li[abc~=‘d‘] { border: 1px solid red; }*/
    li[class*=‘c‘] { border: 1px solid red; }
    /*li[class^=‘act‘] { border: 1px solid red; }*/
    /*li[class$=‘ve‘] { border: 1px solid red; }*/
   /* li[class|=‘act‘] { border: 1px solid red; }*/
  </style>
</head>
<body>
 
  <ul class="list">
    <li>
      <h2>第一級菜單</h2>
      <ul>
        <li>
          <h2>第二級菜單</h2>
        </li>
        <li class="act">
          <h2>第二級菜單</h2>
        </li>
        <li>
          <h2>第二級菜單</h2>
        </li>
        <li class="act focus">
          <h2>第二級菜單</h2>
        </li>
        <li>
          <h2>第二級菜單</h2>
        </li>
      </ul>
    </li>
    <li abc="d f"><h2>第一級菜單</h2></li>
    <li class="clear active"><h2>第一級菜單</h2></li>
    <li><h2>第一級菜單</h2></li>
    <li class="abc"><h2>第一級菜單</h2></li>
  </ul>

</body>
</html>

 

 

偽類別選取器:

  input:disabled

  a:hover/:active/a:visited/a:link

  :before/:after

  :nth-child(n)第n個元素(p:nth-child,限制標籤名)

  :nth-last-child(n)倒數第n個元素(p:nth-last-child)

  :nth-of-type(n)所有符合選取器要求的第n個元素

  :nth-last-of-type()同上,倒序

  :first-child/:last-child第一個/最後一個子項目

  p:first-child第一個子項目並且標籤名必須為p

  p:last-child最後一個子項目,並且標籤名必須為p

 

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>偽類別選取器</title>
  <style type="text/css">
    /*body { background: pink; }*/
    /*:nth-child(2) {
      border: 1px solid #333;
    }

    div :nth-child(3){
      color: green;
    }*/

    /*div:nth-child(3){
      color: green;
    }*/

    /*div p:nth-child(2){
       div 的第二個子標籤,並且標籤類型為 p標籤的元素
      color: green;
    }*/

    /*:first-child {
      font-size: 20px;
    }*/

    /*p:first-child {
      font-size: 20px;
    }*/
    /*div :first-child {
      font-size: 30px; color: red;
    }*/

/*   :nth-child(3) {
      color: red; font-size: 20px;
    }*/

   /*:nth-of-type(3) {
      color: red; font-size: 20px;
    }*/

    .div1 :nth-child(odd) {
      color: red;
    }

    /*#div1 .div2*/

    /*
      偽類別選取器:

    */
  </style>
</head>
<body>

  <div class="div1">
    <h2>fdsafafafaf</h2>
    <p>123131231</p>
    <div>fdsf123131313</div>
    <h2>fdsafafafaf</h2>
    <p>123131231</p>
    <div>
      <span>fdsf123131313</span>
      <p>fsafaffafaf</p>
      <div>123131313</div>
    </div>
    <h2>fdsafafafaf</h2>
    <p>123131231</p>
    <div>fdsf123131313</div>
  </div>
 
  <div class="div2">
    <h2>fdsafafafaf</h2>
    <p>123131231</p>
    <div>fdsf123131313</div>
    <h2>fdsafafafaf</h2>
    <p>123131231</p>
    <div>
      <span>fdsf123131313</span>
      <p>fsafaffafaf</p>
      <div>123131313</div>
    </div>
    <h2>fdsafafafaf</h2>
    <p>123131231</p>
    <div>fdsf123131313</div>
  </div>
 
</body>
</html>

 

CSS3--選取器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.