简体   繁体   English

仅在带图像的链接上进行CSS转换

[英]CSS transition on links with images only

How can I apply transition only on the links which have images instead of text. 如何仅在具有图像而非文本的链接上应用转换。 For example only in the second link: 例如,仅在第二个链接中:

<a href="#">no image</a>
<a href="#"><img src="http://i.qkme.me/35rb4r.jpg"></a>

The following CSS will apply transition on all links: 以下CSS将在所有链接上应用转换:

a{   
    -webkit-transition: opacity 0.5s;
    -moz-transition:    opacity 0.5s;
    -o-transition:      opacity 0.5s;
}

a:hover{
    opacity:0.1;
}

I've tried this and doesnt work. 我试过这个并没有用。

a:hover img{
    opacity:0.1;
}

Here's jsfiddle: http://jsfiddle.net/wg5b3/ 这是jsfiddle: http//jsfiddle.net/wg5b3/

You cannot select a parent element via css. 您无法通过css选择父元素。 But you really can style the image directly, ie 但你真的可以直接设置图像样式,即

a > img {transition: opacity 0.5s;}
a > img:hover {opacity 0.1;}
a > img:hover{
    opacity:0.1;
    -webkit-transition: opacity 0.5s;
    -moz-transition:    opacity 0.5s;
    -o-transition:      opacity 0.5s; 
}
a img:hover {
    opacity:0.1;
}

You may try this as well: 你也可以试试这个:

a{   
    -webkit-transition: opacity 0.5s;
    -moz-transition:    opacity 0.5s;
    -o-transition:      opacity 0.5s;
}

a:hover #hello{
    opacity:0.1;
}

<a href="#">no image</a>
<a href="#" id="hello" ><img src="http://i.qkme.me/35rb4r.jpg"></a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM