標籤{ 屬性:值; }
注意:類名的第一個字符也不能使用數字。php
單類名調用:class="類名";
css
多類名調用:class="類名1 類名2 ...";
html
<!DOCTYPE html> <html> <head> <style> .center{ text-align:center; } .col{ color:red; } .font{ font-size:18px; font-family:"Microsoft YaHei"; } </style> </head> <body> <h1 class="center">class 選擇器</h1> <p class="center col">我是一個段落。</p> <p class="center font">我是另外一個段落。</p> </body> </html>
<!DOCTYPE html> <html> <head> <style> .center{ color:blue; } p.center{ text-align:center; } p.col{ color:red; } .font{ font-size:18px; font-weight:bold; font-family:"Microsoft YaHei"; } </style> </head> <body> <h1 class="center col">class 選擇器</h1> <p class="center col">我是一個段落。</p> <p class="center col font">我是另外一個段落。</p> <h2 class="center">h2 標題</h2> </body> </html>
注意: id 屬性不能以數字開頭。css3
#id{ 屬性:值; }
*{ 屬性:值; }
選擇器1選擇器2{ 屬性:值; }
選擇器1,選擇器2{ 屬性:值; } // h1, h2, h3, h4, h5, h6, p{color:red;}
先代選擇器 後代選擇器{ 屬性:值; }
父選擇器 > 子選擇器{ 屬性:值; }
只做用於父元素內的[直接子元素];瀏覽器
若是不但願選擇任意的後代元素,而是隻選擇某個元素的子元素,那麼就使用子元素選擇器框架
<head> <style> div>a{ color:red; } </style> </head> <body> <div> <a href="">div 中第一個子元素 a,顯示爲紅色</a> <p>div 中第二個子元素 p<br/> <a>p 元素的子元素 a,該元素是 div 元素的孫元素</a> </p> <a href="">div 中第三個子元素 a,顯示爲紅色</a> </div> </body>
伯選擇器 + 仲選擇器{ 屬性:值; }
做用於緊接在伯元素後的[仲元素];字體
若是須要選擇緊接在另外一個元素後的元素,並且兩者有相同的父元素,就可使用相鄰兄弟選擇器網站
<head> <style> div+p{ background-color:red; } </style> </head> <body> <p>body 的第一個子元素 p</p> <div>body 的第二個子元素 div <p>div 中第一個子元素 p</p> <div> <p>div 中第二個子元素 div 的子元素 p</p> </div> <p>div 中第三個子元素 p,是 div 的相鄰兄弟元素,相同父級 div</p> //紅 </div> <p>body 的第三個子元素 p,是 div 的相鄰兄弟元素,相同父級 body</p> // 紅 <p>body 的第四個子元素 p,是 div 的普通兄弟元素</p> </div> </body>
<head> <style> div~p{ background-color:red; } </style> </head> <body> <p>body 的第一個子元素 p</p> <div>body 的第二個子元素 div <div>div 元素下的第一個子元素 div</div> <p>div 的相鄰兄弟元素</p> <p>div 的普通兄弟元素</p> <p>div 的普通兄弟元素</p> </div> <p>body 下 div 的相鄰兄弟元素</p> <p>body 下 div 的普通兄弟元素</p> <p>body 下 div 的普通兄弟元素</p> </body>
[屬性]{ 屬性:值; }
<head> <style> [title]{ color:red; } </style> </head> <body> <h1>h1 標題不帶有 title 屬性</h1> <h2 title="標題">h2 能夠設置樣式</h2> <a href="#" title="連接">超連接能夠設置樣式</a> </body>
標籤[屬性=值]{ 屬性:值; }
<head> <style> [title=Hello]{ color:blue; } </style> </head> <body> <h1 title="Hello world">h1 標題 title="Hello world"</h1> <h2 title="Hello">h2 能夠設置樣式</h2> <a href="#" title="Hello">超連接能夠設置樣式</a> </body>
<head> <style> input[type="text"]{ width:150px; display:block; margin-bottom:5px; background-color:yellow; } input[type="button"]{ width:120px; margin-top:5px; background-color:green; } </style> </head> <body> <form name="input" action="demo.php" method="get"> 用戶名:<input type="text" name="user" placeholder="請輸入登陸名"> 暱 稱:<input type="text" name="name" placeholder="請輸入角色名"> <input type="button" value="查詢"> </form> </body>
選擇器:僞類{ 屬性:值; }
<a>
超連接僞類:a:link
:未訪問的連接;url
a:visited
:已訪問的連接;spa
a:hover
:鼠標移動到連接;
a:active
:鼠標點擊時的鏈接;
a:hover
必須跟在 a:link
和 a:visited
後面。
a:active
必須跟在 a:hover
後面。
<head> <style> a.tint:hover{ color:red; } </style> </head> <body> <a href="#">超連接</a> <a class="tint" href="#">超連接</a> </body>
:first-child
:第一個子元素;
:last-child
:最後一個子元素;
:nth-child(n)
:第n個元素(n=1,2,3...);
:nth-last-child(n)
:倒數第n個元素(n=1,2,3...);
[n=odd
:奇數 | n=even
:偶數]
注意:
- 不是第一個HTML標籤,而是第一個HTML元素
- html元素:文本,圖像,連接;
<head> <style> p:first-child{ font-weight:bold; color:blue; } </style> </head> <body> <p>第一個 p 元素</p> // 藍 <p>第二個 p 元素</p> <p>第三個 p 元素</p> </body>
/*:target 選擇器用於選取當前活動的目標元素*/ :target{ 屬性:值; }
標籤::僞元素{ 屬性:值; }
:first-line
:向文本的首行設置特殊樣式;
注意::first-line 僞元素只能用於塊級元素,下面的屬性可應用於 :first-line 僞元素
①、font
②、color
③、background
④、line-height
⑤、clear
⑥、vertical-align
⑦:text-decoration
⑧:text-transform
⑨、letter-spacing
⑩、word-spacing
:first-letter
:向文本的首字母設置特殊樣式;
注意::first-letter 僞元素只能用於塊級元素,下面的屬性可應用於 "first-letter" 僞元素
①、font、color、background、line-height、clear、vertical-align (only if "float" is "none")、text-decoration、text-transform
以上8個屬性和 :first-line 僞元素相同,除了 letter-spacing 和 word-spacing 以外。
②、float
③、margin
④、padding
⑤、border
:before
:在標籤以前添加 content:新內容
;
標籤::before{ content:新內容; // constent:none 默認 // constent:'string' 插入文本 // constent:attr(屬性) 插入標籤屬性值 // constent:url(路徑) 插入一個外部資源 }
:after
:在標籤以後添加 content:新內容
;標籤::after{ content:新內容; }
:selection
:選中區域;<head> <style> p:first-letter{ color:red; font-size:20px; } p:first-line{ color:blue; text-decoration:underline; } </style> </head> <body> <h1>The best things in life are free, like hugs, smiles, friends, kisses, family, love and good memories.</h1> <p>The moon belongs to everyone,best things in life they're free,stars belong to everyone,they cling there for you and for me.Love can come to everyone,best things in life they're free,all of the good things,every one of the better things.</p> <p>The best things in life are free, and the second best things are very, very expensive.</p> </body>
①、表明內聯樣式,如 <p style="color:red"></p>
,權值爲 1000。
②、表明 ID 選擇器,如 #content
,權值爲 100。
③、表明類,類選擇器以及僞類和屬性選擇器,如 .main
,權值爲 10。
④、表明類型選擇器,標籤和僞元素選擇器,如 div p
,權值爲 1。
p{ /* 權值爲 1 */ color:red; } p span{ /* 權值爲 1+1=2 */ color:green; } .main{ /* 權值爲 10*/ font-size:14px; } div p .main{ /* 權值爲 1+1+10=12 */ color:purple; } #footer{ /* 權值爲 100 */ color:gray; } #footer .note p{ /* 權值爲 100+10+1=111 */ color:white; }
<head> <style> p{ color:red!important; // 權值最高,p 元素顯示紅色 } .demo{ color:green; } </style> </head> <body> <p class="demo">在實際的網站開發中,有些特殊的狀況須要爲某些樣式設置具備最高權值,這時候咱們可使用 !important 來解決。</p> </body>