我一直在玩僞選擇器,並試圖找出東西。正確的方式來使用CSS僞選擇器?
這是該元素的一般看我想工作:
<div class=simpleGallery>
<a href="...">
<img data-attachment-id="some_number" ........>
</a>
</div>
我試圖讓文字顯示與特定屬性(數據連接-ID爲例)圖片。我設法通過搜索以獨特方式結束的href來獲得它。像這樣...
.simpleGallery a[href$="GP120094-1.jpg"]:before{
content:'Hokus Pokus';
position:absolute;
bottom:25%;
opacity:0;
width:100%;
text-align:center;
color:#fff;
box-sizing:border-box;
-moz-box-sizing:border-box;
-moz-transition: all 1.2s ease;
-webkit-transition: all 1.2s ease;
transition: all 1.2s ease;
}
然後我得到的文本與顯示:
.simpleGallery a:hover:before{
opacity:1;
z-index:1;
-moz-transform: scale(1.1);
-webkit-transform: scale(1.1);
transform: scale(1.1);
}
它所有的作品,但我想知道爲什麼它不會像這樣工作:
.simpleGallery a:before img[data-attachment-id="some_number"]
它將如何通過圖像data-attachment-id代替<a>
標籤中的href完成?
爲什麼?
這是因爲:之前只能在我尋找的最後一個元素之後去?
請注意,在這裏這種情況下,實際的問題是,它是不可能的文本添加到IMG,由於IMG是_replaced element_,並在它的任何內容無法顯示。所以......你不能在img本身上使用':: before'或':: after'(它不會工作),你不得不使用':: before'或':: after來顯示額外的內容'圍繞它的元素。 –