CSS樣式表-------第二章:選取器

來源:互聯網
上載者:User

標籤:

 

二 、選取器 

內嵌、外部樣式表的一般文法:

選取器

{

樣式=值;

樣式=值;

樣式=值;

......

}

以下面html為例,瞭解區分一下各種樣式的選取器

<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>無標題文檔</title><link href="file:///E|/網頁/Untitled-2.css" rel="stylesheet" type="text/css" />          </head><body><input name="txt" id="a1" type="text" /><br/><span>你好</span> <br/><div><input name="txt" id="a2" type="text" /><br/><input name="txt" id="a3" type="text"  value="******"/><br/><span><input name="txt" id="a4" type="text" value="******"/><br/></span></div><p>你好</p>         </body>

 

1、標籤選取器  ------用標籤名做選取器

input{...}    div{...}    span{...}     td{...}  都可以做標籤選取器,標籤選取器控制的是一類,範圍大。

例:

@charset "utf-8";/* CSS Document */input{border:5px;                       color:#3F6;border-bottom:3px solid red;}


顯示結果:

2、類別選取器 ------在HTML元素中使用class對元素進行分類,然後使用這個class的值作為選取器。

class選取器都是以"."開頭。.class的名{...} 類別選取器相對於標籤選取器來說,控制的更準確。

 

例:選取器 .uu

@charset "utf-8";/* CSS Document */.uu{border:5px;                       color:#3F6;border-bottom:3px solid red;}


HTML表的調用為:

<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>無標題文檔</title><link href="file:///E|/網頁/Untitled-2.css" rel="stylesheet" type="text/css" /></head><body><input name="txt" type="text" id="a1" /><br/><span class="uu">你好</span> <br/><div><input name="txt" type="text" id="a2" class="uu" /><br/><input name="txt" type="text" id="a3"  value="******"/><br/><span><input name="txt" type="text" id="a4" class="uu" value="******"/><br/></span></div><p>你好</p>         </body>


顯示結果:

3、ID選取器 ------針對HTML中相對應ID的元素起作用,以"#"開頭。

#ID的名{...}

例:選取器#a3

@charset "utf-8";/* CSS Document */#a3{border:5px;                       color:#3F6;border-bottom:3px solid red;}


顯示結果:

 

 

4、複合選取器

(1)、用","隔開   -------表示並列關係,同時起作用。

 

input,#dd,.yellow,.uu{color:gray;line-height:28px;}

 

(2)、用"空格"隔開,------表示後代關係。

.uu{    color:gray;line-height:28px;}

 

div .uu         <!--div空格uu-->{background-color:blue;}<input name="txt" type="text" class="uu"/><div>                                                  <!--相當於父代--><input name="txt" type="text" />                 <!--子代--><input name="txt" type="text" class="uu" value="******"/>   <!--子代--><span>                                        <!--子代--><input name="txt" type="text" class="uu" value="******"/>   <!--孫子代--></span></div>


不管是子代,還是孫子代,都是後代,只要在div的後代中有uu的就變化。

顯示結果:

(3)class二次篩選。

標籤選取器後面緊跟著 .class選取器{...}

 

input.uu
{
border:5px double red;
}

例:

.uu{    color:gray;line-height:28px;}div.uu{background-color:red;}<body><input name="txt" type="text" class="uu"/><br/><div><input name="txt" type="text" /><br/><input name="txt" type="text" class="uu" value="******"/><br/><span><input name="txt" type="text" class="uu" value="******"/></span></div>       </body>


顯示結果:

div.uu的意思是:div存在的同時.uu也存在,屬於二次篩選。

*對比:(div空格 .uu)與div.uu的不同。

div空格.uu的意思是:div的後代中(不管是子代還是孫子代中)出現.uu,出現的項就會隨樣式表變化。

div.uu的意思是:div存在的同時.uu也存在時,才會隨樣式表變化,屬於二次篩選。

 

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.