实际上,当我悬停这些链接时,所有这些链接都会变为绿色,因为第一个悬停选择器被执行得很好。但是,对于最后一个链接" cde",我调用另一个悬停选择器来显示图像,它不起作用。
我尝试了什么:
问题:有没有办法让图片显示文字变绿?
非常感谢你的帮助。非常感谢!
.work-abcde a:hover{
color: #61c261;
}
.cde-pic{
position: relative;
width: -100px;
top: -100px;
float: right;
visibility: hidden;
}
.cde:hover .cde-pic {
position: relative;
left: -20px;
visibility: visible;
}

<div class="work-abcde">
<ul>
<li><h3>abc bcd cde</h3></li>
<li><h4><a class="abc" href="www.abc.com" rel="nofollow"> abc</a> </h4></li>
<li><h4><a class="bcd" href="www.bcd.com" rel="nofollow"> bcd </a> </h4></li>
<li><h4><a class="cde" href="www.cde.com" rel="nofollow"> cde</a> </h4> </li>
<div class="cde-pic"><img src="./Resources/cde.png" ></div>
</ul>
</div>
&#13;
答案 0 :(得分:3)
问题是 css选择器无法在纯CSS中定位父,您可以定位兄弟或子,但不能定位父(纯CSS)。
A CSS selector can contain more than one simple selector. Between the simple selectors, we can include a combinator.
descendant selector (space)
child selector (>)
adjacent sibling selector (+)
general sibling selector (~)
有关儿童/兄弟选择器的更多信息,您可以在此处阅读更多内容(2分钟阅读)https://www.w3schools.com/css/css_combinators.asp
<li>
和<div>
是兄弟姐妹。所以使用li
悬停而不是类cde
,然后您可以定位cde-pic
类:
li:hover ~ .cde-pic {
position: relative;
left: -20px;
visibility: visible;
}
.work-abcde a:hover {
color: #61c261;
}
.cde-pic {
position: relative;
width: -100px;
top: -100px;
float: right;
visibility: hidden;
}
li:hover + .cde-pic {
position: relative;
left: -20px;
visibility: visible;
}
<div class="work-abcde">
<ul>
<li>
<h3>abc bcd cde</h3>
</li>
<li>
<h4><a class="abc" href="www.abc.com" rel="nofollow"> abc</a> </h4>
</li>
<li>
<h4><a class="bcd" href="www.bcd.com" rel="nofollow"> bcd </a> </h4>
</li>
<li>
<h4><a class="cde" href="www.cde.com" rel="nofollow"> cde </a> </h4>
</li>
<div class="cde-pic">
<img src="https://dummyimage.com/200x200/000/fff">
</div>
</ul>
</div>
将<div>
移至<li>
,以便他们成为兄弟姐妹(cde
和cde-pic
)。
.work-abcde a:hover {
color: #61c261;
}
.cde-pic {
position: relative;
width: -100px;
top: -100px;
float: right;
visibility: hidden;
}
.cde:hover + .cde-pic {
position: relative;
left: -20px;
visibility: visible;
}
<div class="work-abcde">
<ul>
<li>
<h3>abc bcd cde</h3>
</li>
<li>
<h4><a class="abc" href="www.abc.com" rel="nofollow"> abc</a> </h4>
</li>
<li>
<h4><a class="bcd" href="www.bcd.com" rel="nofollow"> bcd </a> </h4>
</li>
<li>
<h4>
<a class="cde" href="www.cde.com" rel="nofollow"> cde </a>
<div class="cde-pic">
<img src="https://dummyimage.com/200x200/000/fff">
</div>
</h4>
</li>
</ul>
</div>
答案 1 :(得分:1)
你使用的这个选择器......
.cde:hover .cde-pic { ... }
...无效,因为.cde-pic
不是.cde
的子元素。
答案 2 :(得分:0)
尝试以下方法:
.work-abcde a:hover{
color: #61c261;
}
.cde-pic{
position: relative;
top: -100px;
float: right;
display:none;
}
.cde:hover:after {
content: url("//images.google.com/images/branding/googleg/1x/googleg_standard_color_128dp.png"); /* no need for qoutes */
position: relative;
display:block;
left: -20px;
}
<div class="work-abcde">
<ul>
<li><h3>abc bcd cde</h3></li>
<li><h4><a class="abc" href="www.abc.com" rel="nofollow"> abc</a> </h4></li>
<li><h4><a class="bcd" href="www.bcd.com" rel="nofollow"> bcd </a> </h4></li>
<li><h4><a class="cde" href="www.cde.com" rel="nofollow"> cde</a> </h4> </li>
</ul>
</div>
答案 3 :(得分:0)
JS的解决方案...... 这是代码:
function chbg(vis) {
document.getElementById('cde').style.visibility = vis
}
.work-abcde a:hover {
color: #61c261;
}
.cde-pic {
position: relative;
float: right;
visibility: hidden;
}
.cde:hover .cde-pic {
position: relative;
left: -20px;
visibility: visible;
}
a:hover > .cde-pic {
visibility: visible;
}
<div class="work-abcde">
<ul>
<li>
<h3>abc bcd cde</h3>
</li>
<li>
<h4><a onmouseover="chbg('visible')" onmouseout="chbg('hidden')" class="abc" href="www.abc.com" rel="nofollow"> abc</a> </h4>
</li>
<li>
<h4><a onmouseover="chbg('visible')" onmouseout="chbg('hidden')" class="bcd" href="www.bcd.com" rel="nofollow"> bcd </a> </h4>
</li>
<li>
<h4><a onmouseover="chbg('visible')" onmouseout="chbg('hidden')" class="cde" href="www.cde.com" rel="nofollow"> cde</a> </h4>
</li>
<div class="cde-pic" id="cde"><img src="http://lorempixel.com/200/200/"></div>
</ul>
</div>
希望它有所帮助!