1. 程式人生 > >CSS選擇器筆記

CSS選擇器筆記

mage view IE AS ima none info sed 選中

記錄的問題:

1.比如一個標簽的class是:class="p1 p2",另外一個標簽的class是:class="p1",那麽我使用.p1會選擇到第一個標簽嗎?

答案:如果使用.p1是會同時選中兩個標簽的,下面使用例子來解釋

技術分享圖片
<html>
<head>
<style type="text/css">
.p1{
color:blue
}
</style>
</head>

<body>
<h1 class=‘p1 p2‘>這是 heading 1</h1>
<p class=‘p1‘>這是一段普通的段落。</
p> </body> </html>
View Code

顯示結果:

技術分享圖片

從中我們可以看到,都同時選到了h1和p,所以我們如果要區別這兩個時,我們可以用如下:

技術分享圖片
<html>
<head>
<style type="text/css">
.p1.p2{
color:blue
}
</style>
</head>

<body>
<h1 class=‘p1 p2‘>這是 heading 1</h1>
<p class=‘p1‘>這是一段普通的段落。</p>
</
body> </html>
View Code

顯示結果:

技術分享圖片

這樣就只選中了h1,所以當一個標簽有多個class時,使用.class1.class2來選擇

CSS選擇器筆記