2017-08-30 94 views

回答

1

這是可能的使用:focus-within僞類選擇。

焦點在CSS僞類匹配:焦點僞類匹配或具有:焦點僞類匹配後代的任何元素。 (這包括在陰影栽樹後人。)

:focus-within @ MDN

Support Details

lable { 
 
    margin: 1em; 
 
    border: 1px solid grey; 
 
} 
 

 
label:focus-within p { 
 
    color: red; 
 
}
<label> 
 
    <p>text description</p> 
 
    <input type="text"> 
 
</label>

+0

您可以檢查p標籤的顏色沒有改變 – Durga

+0

它適用於Chrome 60和FF –

6

您不能從<input>訪問前一個同級元素(<p>)。

但是你可以採用如下方案:

label { 
 
    display:flex; 
 
} 
 
p { 
 
    order:-1; 
 
} 
 
input:focus ~ p { 
 
    color:red; 
 
}
<label> 
 
    <input type="text"> 
 
    <p>text description</p> 
 
</label>

相關問題