僞類 -- :hover, :link, :active, :visited, :first-child, :last-child, nth-child(n), :not(), :focus
僞元素 -- ::before, ::after, ::first-letter, ::first-line, ::selection
僞元素: [ 僞元素用於向某些選擇器設置特殊效果,簡單來講,僞元素建立了一個虛擬容器,這個容器不包含任何DOM元素,可是能夠包含內容,咱們能夠爲僞元素定製樣式 ]css
::first-letter -- 用於選取指定選擇器的首字母(eg. 設置指定選擇器的首字母爲大寫紅色)css3
p:first-letter { color: #f00; font-size: 32px; text-transform: capitalize; }
::first-line -- 用於選取指定選擇器的首行(eg. 設置指定選擇器的首行背景色爲紅色)web
p:first-line { color: #fff; background: #f00; }
::selection -- 用於匹配被用戶選取的部分(eg. 設置被選中的文本爲紅色字體)api
p::selection { color:#f00; } p::-moz-selection { color:#f00; }
::before -- 在指定選擇器的內容前面插入內容(eg. 在被選元素的內容以前插入新內容)
::after -- 在指定選擇器的內容後面插入內容(eg. 在被選元素的內容以後插入新內容)app
巧用 ::after/::before 僞元素製做CheckBox開關按鈕 HTML部分: <label class="agreeBtn"> <input type="checkbox" checked /> <span class="active"></span> I agree with <a href="/">terms&conditions</a> </label> CSS部分: .agreeBtn { position: relative; } .agreeBtn input { opacity: 0; } .agreeBtn span { position: relative; display: inline-block; left: -10px; top: 6px; width: 36px; height: 20px; border-radius: 30px; background-color: #eee; -webkit-transition: background-color .3s; -moz-transition: background-color .3s; -ms-transition: background-color .3s; -o-transition: background-color .3s; transition: background-color .3s; } .agreeBtn span.active { background-color: #999; } .agreeBtn span::after { position: absolute; content: ''; top: 2px; left: 2px; width: 16px; height: 16px; border-radius: 50%; box-shadow: 1px 0 3px rgba(0,0,0,0.1); background-color: #fff; -webkit-transition: .3s; -moz-transition: .3s; -ms-transition: .3s; -o-transition: .3s; transition: .3s; } .agreeBtn span.active::after { -ms-transform: translateX(16px); -moz-transform: translateX(16px); -webkit-transform: translateX(16px); -o-transform: translateX(16px); transform: translateX(16px); }
巧用 ::after/::before 僞元素更改select 元素的默認樣式 HTML部分: <div class="user_select"> <select class="form-control"> <option value="PhD" selected="selected">PhD</option> <option value="PhD Candidate">PhD Candidate</option> <option value="Master">Master</option> <option value="Master Candidate">Master Candidate</option> <option value="Bachelor">Bachelor</option> <option value="None">None</option> </select> </div> CSS部分: .usermr_content select{ appearance: none; -moz-appearance: none; -webkit-appearance: none; } .user_select{ position: relative; } .user_select::after{ position: absolute; content: ''; right: 5px; top: 11px; border-top: 6px solid #666; border-left: 3px solid transparent; border-right: 3px solid transparent; }
巧用 ::after/::before 僞元素製做左右按鈕 HTML部分: <span class="btn leftBtn"></span> <span class="btn rightBtn"></span> CSS部分: .btn{ position: relative; display: block; width: 42px; height: 42px; border-radius: 2px; background-color: #36403f; } .btn:hover{ background-color: #f55362; } .btn::after{ position: absolute; content: ''; top: 10px; left: 15px; borde: 10px solid transparent; } .leftBtn::after { border-right-color: #fff; } .rightBtn::after { border-left-color: #fff; }
巧用 ::after/::before 僞元素實現立體按鈕效果 HTML部分: <div class="loginBtn"><input type="button" value="登 錄"></div> CSS部分: .loginBtn input { width: 100%; height: 50px; border-radius: 4px; box-shadow: 0 0 4px rgba(0,0,0,.6); line-height: 50px; text-align: center; letter-spacing: 1px; color: #fff; background: -webkit-linear-gradient(top, #4585db, #396fb7); } .loginBtn{ position: relative; } .loginBtn::before { position: absolute; content: ''; top: 0; left: 0; height: 100%; width: 100%; border-radius: 4px; box-shadow: 0 -3px 0 #75adeb; }
巧用 ::after/::before僞元素製做照片翹邊陰影,使照片更具立體感。字體
巧用 ::after/::before僞元素實現標籤頁切換樣式。spa
巧用 ::after/::before僞元素content實現 + - 按鈕。3d
僞類: [ 僞類存在的意義是爲了經過選擇器找到那些不存在於DOM樹中的信息以及不能被常規CSS選擇器獲取到的信息 ]code
:hover //鼠標懸停時顯示的效果
:link //連接在正常狀況下(即頁面剛加載完成時)顯示的效果
:active //當元素處於激活狀態(鼠標在元素上按下尚未鬆開)時所顯示的效果
:visited //連接被點擊後顯示的效果
:first-child //選擇器匹配屬於其父元素的首個子元素的指定選擇器
:last-child //選擇器匹配屬於其父元素的最後一個子元素的指定選擇器
:nth-child(n) //選擇器匹配屬於其父元素的第 N 個子元素,不論元素的類型
:not() //匹配非指定選擇器的每一個元素(eg. 匹配全部類名不爲active的p元素 p:not(".active"))
:focus //元素得到光標焦點時的效果(eg. input 輸入框 input:focus)