CSS屬性選取器

來源:互聯網
上載者:User

標籤:

屬性選取器

 

 1 <!DOCTYPE html> 2 <html lang="zh-cn"> 3 <head> 4     <meta charset="UTF-8"> 5     <title>Document</title> 6     <link rel="stylesheet" type="text/css" href="./css/屬性選取器.css"> 7 </head> 8 <body> 9     <h1 class="hoopla">Hello</h1>10     <h1 class="hoopla fancy">Hi11     <h1 class="severe">serenity</h1>12     <h1 class="fancy">Fooling</h1>13     <span class="1cloud sun">colud</span>14     <span class="1rain sun">rain</span>15     <span class="sun">sun</span>16 </html>

 

 1 h1[class] {/*根據元素屬性來選擇元素*/ 2     color: silver; 3 } 4  5 h1[class="hoopla"]{/*根據具體屬性值選擇元素, 6                     這種格式要求必須與屬性值完全符合*/ 7     text-decoration: underline; 8 } 9 10 h1[class~="fancy"] {/*根據部分屬性值選擇元素,~ 聲明為部分選擇*/11     display: block;12     margin-right: 1000px;13     background: #ff0000;14 }
15 /*子串匹配選取器*/16 span[class*="n"]{/*選擇class屬性值中包含子串"n"的所有元素*/17 background: #f60;18 }19 span[class^="1"]{/*選擇class屬性值以"1"開頭的所有元素*/20 background: #ccc;21 }22 span[class$="sun"]{/*選擇class屬性值以"sun"結尾的所有元素*/23 text-decoration: underline;24 }

簡單屬性選擇:選擇元素某個屬性,不論屬性值是什麼。

  這裡選擇h1元素 → Class屬性 的元素,設定前景色彩為銀色

          h1[class] {/*根據元素屬性來選擇元素*/

      color : silver;

     }

 

具體屬性值選擇:除了選擇元素某個屬性,還可以進一步縮小選擇範圍,只選擇特定屬性值的元素

  這裡選擇h1元素 → Class屬性 → 具體值“hoopla” 的元素,設定文本樣式為有底線

            h1[class=”hoopla”] {/*根據具體屬性值選擇元素,這種格式要求必須與屬性值完全符合*/

         text-decoration : underline;

     }

 

部分屬性值選擇:可以根據屬性值,其中任意一個詞進行選擇

      這裡選擇h1元素 → Class屬性 → 部分值“fancy” 的元素,有兩個h1元素 含有“fancy”,這兩個元素樣式都會被設定

            h1[class~="fancy"] {/*根據部分屬性值選擇元素,~聲明為部分選擇*/

                 margin-right: 1000px;

      background: #ff0000;

    }

CSS屬性選取器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.