css樣式表---樣式表分類、選取器

來源:互聯網
上載者:User

標籤:

一、樣式表分三類:

1.內聯樣式表。——放在元素的開始標記中。——只對當前元素起作用。
<input name="txt" style="border:0px; border-bottom:1px solid black;" type="text" />

例:

結果:

2.內嵌樣式表。——放在頁面的<head></head>中間。——可以對整個頁面。

<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>無標題文檔</title><style type="text/css">input{border:5px;                         <!--style之間的是內嵌樣式表,在head之間-->color:#3F6;border-bottom:3px solid red;}</style></head><body><input type="text" name="a1" id="a1"><input type="buttom" name="a2" id="a2" value="按鈕"><p>你好</p>               <!--input只外的不變,沒影響--></body>

執行結果:

3.外部樣式表。——放在一個單獨的.css樣式表檔案中。——可以對整個網站。

操作:建立一個CSS檔案,用來存放樣式表——>在HTML中調用樣式表,要在HTML中右鍵——>CSS樣式表——>附加樣式表。樣式表一般用link串連方式。
(1)外部樣式表的定義

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

(2)外部樣式表的調用

<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" />             <!--link串連的外部樣式表--></head><body><input type="text" name="a1" id="a1"><input type="buttom" name="a2" id="a2" value="按鈕"><p>你好</p>         </body>

執行結果:


二、樣式表的選取器:
內嵌、外部樣式表的一般文法:
選取器
{
樣式=值;
樣式=值;
樣式=值;
....
}

以下面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/><spen>你好</spen> <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.基本:
(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的名{.....}      類別選取器是想改變誰就單獨改變誰,相對於標籤選取器要更精確。

選取器:

@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/><spen class="uu">你好</spen> <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選取器:針對HTLM中相應ID的元素起作用。
#ID的名{...}
選取器:

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

執行結果:

2.複合:
(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.