Css選擇器的分類
這次實驗主要熟悉css中的後代選擇器:nth-child,nth-of-type , >, (空格), + 之類的用法:
複合選擇器:
後代選擇器: p span {font-size: 14px;} 選擇p元素的所有子孫元素中的span元素
子女選擇器: p>span {font-size: 14px;} 選擇p元素的所有子女元素中的span元素
組合選擇器: p#start {font-size: 14px;} 選擇id為start的p元素(交集)
群組選擇器: em, .even {font-size: 14px;} 選擇em元素或者類名為even的元素
相鄰兄弟選擇器: h1 + p
後續兄弟選擇器: h1 ~ p {color:red;} 選擇在 h1之後所有兄弟元素中的p元素
否定選擇器: p:not(#start){ color:red;} 選擇所有id不是start的p元素
nth-child選擇器(結構偽類):
p:first-child {color:red;} p的雙親的第一個子女
p:last-child {color:red;} p的雙親的最後一個子女
p:only-child {color:red;} p的雙親的唯一一個子女
p:nth-child(5) {color:red;} p的雙親的第5個子女
p:nth-child(3n+1) {color:red;} p的雙親的選擇第1、 4、 7、 ...個子女
p:nth-last-child(5) {color:red;} p的雙親的倒數第5個子女
nth-of-type選擇器(結構偽類) :
p:first-of-type {color:red;} p的雙親的第一個子女(只計算p元素)
p:last-of-type {color:red;} p的雙親的最後一個子女
p:only-of-type {color:red;} p的雙親的唯一一個子女
p:nth-of-type(5)
p:nth-of-type(odd) {color:red;} p的雙親的第奇數個子女 (even,odd)
p:nth-of-type(3n) {color:red;} p的雙親的選擇第3、 6、 ...個子女
p:nth-last-of-type(5) {color:red;} p的雙親的倒數第5個子女
nth-child的其它用法:
:nth-child(n+6) 選中從第6個開始的子元素
:nth-child(-n+9) 選中從第1個到第9個子元素
:nth-child(n+4):nth-child(-n+8) 選中第4-8個子元素
:nth-child(n+2):nth-child(-n+9):nth-child(odd) 選中的子元素是從第2位到第9位,並
且只包含奇數位。
實驗CSS程式碼如下:
- <styletype="text/css">
- a:link{text-decoration:none;color:black;} <!--偽類-->
- li:nth-child(even) a:link{text-decoration:none;color:green;} <!-- li雙親的第偶數個子女元素 未被訪問的連結 -->
- a:visited{text-decoration:none;color:black;} <!-- 訪問過的連結 -->
- li:nth-child(even) a:visited{text-decoration:none;color:green;} <!-- li雙親的第偶數個子女元素中 被訪問過的連結 -->
- a:hover{text-decoration:underline;color:blue;}
- li:nth-child(even) a:hover{text-decoration:underline;color:blue;}
- a:active{text-decoration:underline;color:red;}
- li:nth-child(even) a:active{text-decoration:none;color:green;}
- <style>
- <styletype="text/css">
- input:focus,textarea:focus{background-color:yellow;} <!--選擇獲得焦點的 input 或者 textarea元素 -->
- button:disabled,input:disabled{color:#CCC;} <!--禁用的button或者input元素 -->
- input:checked+span{color:red;} <!-- 被選中的input元素的相鄰兄弟span-->
- textarea::selection{color:white;background-color:blue;}<!-- 選中的文字-->
- <style>
- <styletype="text/css">
- a{color:blue;text-decoration:underline;} <!-- -->
- li:nth-child(even) a{color:green;}<!-- li的雙親的第偶數個子女中的後代a元素-->
- li:nth-child(n+3):nth-child(-n+9):nth-child(odd) a{color:red;}<!--選中的子元素是從第3位到第9位,並且只包含奇數位 -->
- <style>
- <styletype="text/css">
- a:link,a:visited{color:blue;text-decoration:none;}
- a:active{color:#532301;text-decoration:none;}
- p{text-align:left;text-indent:2em;}
- p:first-letter{font-size:1.5em} <!--選擇每個 <p> 元素的首字母-->
- p:first-line{font-style:italic;} <!--選擇每個 <p> 元素的首行 -->
- p:hover{color:green;}
- p:nth-of-type(even){font-weight:bold;} <!--p的雙親的第偶數個子女 -->
- .ref{vertical-align:super;font-size:10px;} <!-- 選擇ref類 -->
- <!-- 該屬性定義行內元素的基線相對於該元素所在行的基線的垂直對齊。super垂直對齊文字的上標-->
- <style>
- <styletype="text/css">
- p:nth-child(n+3){text-indent:2em;} <!-- 從第3個開始的子女元素-->
- p:first-letter{color:blue;font-size:1.5em;} <!-- p元素的第一個字母 -->
- span{display:none;} <!--display屬性用於定義建立佈局時元素生成的顯示框型別 ,none此元素不會被顯示。-->
- span:first-child{display:inline;} <!-- span的雙親的第一個子女,顯示為內聯元素,元素前後沒有換行符-->
- p:hover span{display:inline;} <!--滑鼠置於上方的p元素中的子孫span元素 -->
- p:nth-child(n+3){text-align:left;} <!-- p的第3個開始的子元素-->
- <style>
- <styletype="text/css">
- tr:first-child > td{background-color:#DDD}<!-- tr雙親的第一個子女元素 的 子孫td元素-->
- td:nth-child(even){background-color:rgb(200,200,255);}<!-- td的雙親的第偶數個子女元素-->
- tr:nth-of-type(2) td:nth-child(-n+3){color:grey;}<!--tr雙親的第2個子女 的 子孫td元素 中的 第1個到第3個子元素 -->
- tr:nth-of-type(2) td:nth-child(n+4):nth-child(-n+7):after,tr:nth-of-type(3) td:nth-child(-n+4):after{content:"*";}
- <!-- tr雙親的第2個子女的 子孫中 的td元素第4個到第7個的子元素的內容之後插入“*”-->
- <!-- tr雙親的第3個子女的 子孫中 的td元素第4個到第7個的子元素的內容之後插入“*”-->
- <style>
- <styletype="text/css">
- tr:nth-of-type(odd)>td:nth-of-type(odd),tr:nth-of-type(even)>td:nth-of-type(even){background-color:rgb(200,200,200);}
- <!--tr的雙親的第奇數個子女 的後代 td元素中為基數的, 同。。 -->
- tr:nth-of-type(odd) >td:nth-of-type(even),tr:nth-of-type(even) >td:nth-of-type(odd){background-color:rgb(100,100,100);}
- tr:nth-of-type(odd)>td:nth-of-type(odd):hover,tr:nth-of-type(even)>td:nth-of-type(even):hover{background-color:rgb(200,200,240);}
- <!-- tr的雙親的第奇數個子女 的後代 td元素中為基數的 並且滑鼠放置其中的-->
- tr:nth-of-type(odd) >td:nth-of-type(even):hover,tr:nth-of-type(even) >td:nth-of-type(odd):hover{background-color:rgb(100,100,160);}
- tr:nth-child(n+7) td>img:hover{border:dashed 1px yellow;margin:auto}<!-- tr的第7個以後的子女元素中的td元素 的img子女元素 hover時 -->
- tr:nth-child(-n+2) td>img:hover{border:dashed 1px red;margin:auto}
- <style>