僞元素和僞類
說到這個,我們先回顧一下,僞類選擇器和僞元素選擇器,老版的瀏覽器沒有嚴格區分下面 2 種寫法。
a:after{}
a::after{}
在新的標準中,單冒號(:)用於 CSS3 僞類,雙冒號(::)用於 CSS3 僞元素,我們平時開發時可以注意一下,當然大多數瀏覽器兩種寫法都能識別。
常見僞元素和僞類
僞類
:link, :visited, :hover, :active, :focus, :first-child, :last-child, :nth-child, :nth-last-child, :not()
僞類一般用於一個元素的某個狀態,比如說鼠標懸浮,按鈕點擊,鏈接已經訪問,輸入框聚焦等,還用於選擇某個特殊元素,比如說多個元素中的第一個,最後一個,偶數,奇數等。其作用是對某個符合以上條件的元素添加一些樣式。
a:hover{
text-decoration: underline;
}
a:active {
color: blue;
}
a:link {
color: red;
}
a:visited {
color: green;
}
上面的例子展示了一個a
標籤在不同狀態下的不同樣式,在未點擊鏈接之前,a
標籤呈現紅色字體(link),在鼠標移到a
標籤上是,a
標籤出現下劃線(hover),在鼠標按下的時候,a
標籤變爲藍色(active),點擊完了之後,a
標籤變爲綠色(visited)。可以看到,僞類的作用是爲了給不同狀態的標籤添加樣式。
僞元素
::first-letter, ::first-line, ::before, ::after
在內容模塊中提到,僞元素如果沒有設置“content”屬性,僞元素是無用的。
使用僞元素插入的內容在頁面的源碼裏是不可見的,只能在 css 裏可見。
插入的元素在默認情況下是內聯元素(或者,在 html5 中,在文本語義的類別裏)。因此,爲了給插入的元素賦予高度,填充,邊距等等,你通常必須顯式地定義它是一個塊級元素。
還要注意的是典型的 CSS 繼承規則適用於插入的元素。例如,你有字體系列黑體,宋體,無襯線字體應用到 body 元素裏,然後僞元素會像其他元素一樣繼承這些字體系列。
僞元素不會自然繼承自父元素(如 padding margins)的樣式。
你的直覺是 :before 和 :after 僞元素可能是插入的內容會被注入到目標元素的前或後注入。其實不是這樣的,注入的內容將是有關聯的目標元素的子元素,但它會被置於這個元素的任何內容的“前”或“後”。
<head>
<style type="text/css">
p.box::before {
content: "#";
border: solid 1px black;
padding: 2px;
margin: 0 10px 0 0;
}
p.box::after {
content: "#";
border: solid 1px black;
padding: 2px;
margin: 0 10px 0 0;
}
</style>
</head>
<body>
<p class="box">Other content.</p>
</body>
運行效果:
可以看到,我們html
部分只寫了一個元素,但是我們利用僞元素渲染出來 3 個部分,前中後,這裏我們可以認爲,僞元素一般用來輔助html
的元素。但在內容頁面的源碼又看不到,利用僞元素可以實現很多神奇的功能,這裏不做具體講解,後面再出具體教程。
神奇的僞類:focus-within
言歸正傳,回到我們的主角focus-within
,我們知道,僞類focus
是指一個元素獲得焦點時,爲其添加樣式。focus-within
的範圍更廣,它表示一個元素獲得焦點,或該元素的後代元素獲得焦點。劃重點,它或它的後代獲得焦點。這也就意味着,它或它的後代獲得焦點,都可以觸發 :focus-within
。
這個屬性有點類似
Javascript
的事件冒泡,從可獲焦元素開始一直冒泡到根元素html
,都可以接收觸發:focus-within
事件,類似下面這個簡單的例子這樣:
<html>
<div class="box g-father">
<div class="box g-children">
<div class="box button" tabindex="1">button</div>
</div>
</div>
<div class="g-body">HTML</div>
<style>
div {
box-sizing: border-box;
}
.button,.g-children {
width: 100%;
height: 100%;
padding: 20px;
border: 1px solid;
}
.g-father {
width: 200px;
height: 200px;
padding: 20px;
border: 1px solid;
}
.g-body {
margin-top: 20px;
width: 200px;
border: 1px solid;
}
.g-body:focus-within {
background-color: #5daf34;
}
.g-father:focus-within {
background-color: #3a8ee6;
}
.g-children:focus-within{
background-color: #2c3e50;
}
.button:focus-within {
background-color: #606266;
color: red;
}
</style>
</html>
運行結果:
可以看到,在button
獲得焦點時,因爲冒泡的原因,它的父級元素全部應用了:focus-within
的樣式。這裏值得注意的是,正常的div
是不能獲得焦點的,設置 tabindex 屬性才能獲取焦點,同時按鍵盤 Tab 鍵也可讓其獲取焦點,其中 tabindex 的值越小在 tab 鍵切換的時候就會首先聚焦。根據:focus-within
的特性,我們在不利用 js 的情況下,實現很多實用性的功能。
感應用戶聚焦區域
利用focus-within
可以增加用戶的感知區域,讓用戶獲得更好的視覺反饋。
<html>
<div class="g-container">
<input type="text" placeholder="user name" class="g_input" >
<input type="text" placeholder="code" class="g_input" >
</div>
<style>
.g-container {
margin-top: 10vh;
}
.g-container {
padding: 10px;
width: 30vw;
border: 1px solid #eee;
transition: all .3s;
text-align: center;
}
.g-container:focus-within {
transform: translateY(-4px);
box-shadow: 0 0 10px #ddd;
border-color: hsl(199, 98%, 48%);
}
.g_input {
border: none;
width: 20vw;
padding: 15px;
font-size: 18px;
box-sizing: border-box;
border: 1px solid #ddd;
overflow: hidden;
transition: 0.3s;
box-shadow: 0 0 0px #ddd;
&:focus {
box-shadow: 0 0 10px #ddd;
border-color: hsl(199, 98%, 48%);
}
}
</style>
</html>
可以看到在沒有任何javascript
邏輯控制情況下,用focus-within
就實現了上面的效果。
實現離屏導航
我們先看一下效果:
可以看到是一個很棒的導航效果,而且真個實現沒有使用javascript
控制,這無疑在性能和體驗上都有不少提升。具體源碼可以看下面的地址:地址
實現 B 站,掘金等網站登錄動效切換
我們平時可能注意到了,B 站和掘金在用戶輸入密碼的時候,上面的圖片是捂着眼睛的,這裏我們也可以用focus-within
來實現。
<html>
<div class="g-wrap"></div>
<div class="g-container">
<h2>登錄</h2>
<div class="g-username">
<input maxlength="64" placeholder="請輸入手機號或郵箱" class="input">
<img src="https://b-gold-cdn.xitu.io/v3/static/img/greeting.1415c1c.png" class="g-username">
</div>
<div class="g-password">
<input type="password" maxlength="64" placeholder="請輸入密碼" class="input">
<img src="https://b-gold-cdn.xitu.io/v3/static/img/blindfold.58ce423.png" class="g-password">
</div>
<img src="https://b-gold-cdn.xitu.io/v3/static/img/normal.0447fe9.png" class="g-normal">
</div>
<style>
.g-wrap {
position: fixed;
top: 0;
left: 0;
bottom: 0;
right: 0;
background: rgba(0, 0, 0, 0.3);
}
.g-container {
position: relative;
width: 318px;
margin: 100px auto;
height: 370px;
padding: 20px;
box-sizing: border-box;
background: #fff;
z-index: 10;
}
.g-container h2 {
font-size: 20px;
font-weight: bold;
margin-bottom: 30px;
}
.g-container input {
outline: none;
padding: 10px;
width: 100%;
border: 1px solid #e9e9e9;
border-radius: 2px;
outline: none;
box-sizing: border-box;
font-size: 16px;
}
img {
position: absolute;
top: -20%;
left: 50%;
width: 120px;
height: 95px;
transform: translate(-50%, 0);
}
.g-username {
margin-bottom: 10px;
}
.g-username img {
display: none;
width: 120px;
height: 113px;
}
.g-username:focus-within ~ img {
display: none;
}
.g-username:focus-within input {
border-color: #007fff;
}
.g-username:focus-within img {
display: block;
}
.g-password {
margin-bottom: 10px;
}
.g-password img {
display: none;
width: 103px;
height: 84px;
top: -15%;
}
.g-password:focus-within ~ img {
display: none;
}
.g-password:focus-within input {
border-color: #007fff;
}
.g-password:focus-within img {
display: block;
}
</style>
</html>
可以看到,在不適用js
的情況下,也能實現動態切換圖片的效果,但是還是有一些侷限,dom
排列只能是父級向上,不能把元素放在focus
元素的子元素裏面。所以沒有js
靈活,但是代碼量更少。
focus-within 兼容性
因爲 css3 的新增特性一直存在兼容問題,這裏查詢了一下它的兼容性,看到紅色區域還是不算太慘淡,出來 ie,其他瀏覽器基本都支持了。
所有的源碼都可以在我的倉庫地址:地址
個人博客:地址
文章參考鏈接:地址
學習如逆水行舟,不進則退,前端技術飛速發展,如果每天不堅持學習,就會跟不上,我會陪着大家,每天堅持推送博文,跟大家一同進步,希望大家能關注我,第一時間收到最新文章。
公衆號: