<div id="content">我是id選擇器須要選中的元素</div>複製代碼
#content{
color: #fff;
background: #000;
}複製代碼
<ul>
<li class="list-item"></li>
<li class="list-item"></li>
<li class="list-item"></li>
<li class="list-item"></li>
</ul>複製代碼
.list-item{
border-bottom: 1px solid #ccc;
}複製代碼
*{
margin: 0;
padding: 0;
}複製代碼
h1{
font-size: 20px;
}複製代碼
Tips:標籤選擇器一般用來重置某些標籤的樣式,標籤選擇器的效率也不是很高,但要好過通配選擇器。
a[href]{
color: red;
}複製代碼
<!-- HTML: -->
<a href="/">返回主頁</a>
// 下面的CSS會使全部帶href的a標籤字體變紅色:
a[href]{
color: red;
}複製代碼
<!-- HTML: -->
<input type="text" value="大花碗里扣個大花活蛤蟆"/>
// CSS:
input[type=text]{
color: red;
}複製代碼
<!-- HTML: -->
<input class="input text" type="text" value="大花碗里扣個大花活蛤蟆"/>
// CSS:
input[class=input]{
color: red;
}複製代碼
<!-- HTML: -->
<input class="input text" type="text" value="大花碗里扣個大花活蛤蟆"/>
// CSS:
input[class~=input]{
color: red;
}複製代碼
<!-- HTML: -->
<div class="article">我是article</div>
<div class="article-title">我是article-title</div>
<div class="article-content">我是article-content</div>
<div class="article_footer">我是article_footer,不是以artical-開頭的</div>
// CSS:
div[class|=article]{
color: red;
}複製代碼
<!-- HTML: -->
<div class="article">我是article</div>
<div class="article-title">我是article-title</div>
<div class="article-content">我是article-content</div>
<div class="article_footer">我是article_footer,不是以artical-開頭的</div>
// CSS:
div[class^=article]{
color: red;
}複製代碼
<!-- HTML: -->
<button class="btn btn-disabled">禁用的按鈕</button>
<select class="select select-disabled city-select"></select>
<input class="input input-disabled" value="禁用的輸入框"/>
// CSS:
[class$=disabled]{
display: none;
}複製代碼
<!-- HTML: -->
<button class="btn btn-disabled">禁用的按鈕</button>
<select class="select select-disabled city-select"></select>
<input class="input input-disabled" value="禁用的輸入框"/>
// CSS:
[class*=disabled]{
display: none;
}複製代碼
Tips:1. 屬性選擇器要作文本的匹配,因此效率也不會高。2. 在使用屬性選擇器時,儘可能要給它設置上生效的範圍,若是隻用了個 [href] 至關於要在全部元素裏找帶 href 的元素,效率會很低。若是用 a [href] 會好的多,若是用 .link [href] 就更好了。這種組合方式咱們在下一節講解。 bash
3. 屬性選擇器很靈活,若是能使用 CSS 代替 JS 解決一些需求,能夠不用太糾結性能的問題,用 JS 實現也同樣要耗費資源的。性能