如何使用css在悬停时显示图像

时间:2013-08-21 15:42:24

标签: css image hover

我有links的图片库。

编辑:这是我必须使用的代码:

<div class="gallerypagetabs"><a href="http://costumingdiary.blogspot.com/2013/01/victorian-tardis-purse.html"><img alt="Free Victorian Purse Pattern" src="https://lh3.googleusercontent.com/-m4y6eS2KGRQ/Ug7TKD3sbYI/AAAAAAAAHNc/6qoeuGedjOY/s200-c/free-victorian-purse-pattern-1.jpg"><br>Free Victorian Purse Pattern</a><a href="http://costumingdiary.blogspot.com/2012/12/natural-form-victorian-overskirt.html"><img alt="Natural Form Victorian Overskirt" src="https://lh3.googleusercontent.com/-ZrTDaXEOiiU/US__mPzz3dI/AAAAAAAADWQ/eBm3tO3P8oI/s200-c/IMG_5482%255B6%255D.jpg"><br>Natural Form Victorian Overskirt</a><a href="http://costumingdiary.blogspot.com/2012/11/truly-victorian-tv221-1878-tie-back.html"><img alt="Truly Victorian TV221 1878 Underskirt Pattern Review" src="https://lh4.googleusercontent.com/-GmrRTxJ5NGY/UKfO_SQzymI/AAAAAAAAAHA/A9qx6czpyJk/s200-c/Truly-Victorian-TV221-1878-Tie-Back-%255B1%255D%255B4%255D.png"><br>Truly Victorian TV221 1878 Underskirt Pattern Review</a><a href="http://costumingdiary.blogspot.com/2012/10/truly-victorian-tv121-petticoat-pattern.html"><img alt="Truly Victorian TV121 Petticoat Pattern Review" src="http://lh4.ggpht.com/-qTsclIxCTKY/UH7fK3jqKII/AAAAAAAAodI/2GaQOVrGuuA/s200-c/Truly%252520Victorian%252520TV121%2525201879%252520Petticoat%252520with%252520Detachable%252520Train%25255B6%25255D.png?imgmax=800"><br>Truly Victorian TV121 Petticoat Pattern Review</a>..ad naseum..</div>

CSS:

.gallerypagetabs a,.gallerypagetabs p{
    float:left;
    font-size:.80em;
    height:250px;
    padding:10px;
    text-align: center;
    width:200px
}

当有人在图像上盘旋时,我想要做的是显示带有星星的透明图像。因此,如果一个人在自由维多利亚时代的钱包图案图像上盘旋,他们会看到一个图像 - 让我们说 - 五颗星,表明该模式已经获得了我5星中的5星评级。 / p>

我已经尝试过以下两种情况但没有运气。代码在悬停时显示图像,但它显示在图像的底部而不是重叠:

.gallerypagetabs a:hover{
    background-image:url('http://i.imgur.com/IKAXZKz.png');
    background-position:inherit
 }

.gallerypagetabs a:hover{
    background-image:url('http://i.imgur.com/IKAXZKz.png');
    background-position:inherit;
    z-index:10
}

有什么建议吗?我不想使用Javascript,而我想要尽可能少地添加HTML编码除了在开头添加另一个类或id之外,不能更改html。这一切都必须通过CSS完成。这就是我喜欢它的样子。谢谢你的帮助!

enter image description here

工作的代码! (谢谢cimmanon!) (将gallerypagetabs更改为gallerypatterntab以将该类与博客的其余部分隔离开来。来自Blogger的屏幕截图 - 是的Blogger喜欢重写引用之类的内容) enter image description here

3 个答案:

答案 0 :(得分:8)

您可以为此目的使用伪元素。不需要额外的标记。

http://cssdeck.com/labs/anxnrkhr

<a href="#"><img src="http://placekitten.com/100/100" /></a>

a {
  position: relative;
  display: inline-block;
}

a:hover:before {
  content: '';
  display: block;
  background: rgba(255, 0, 0, .5); /* your background */
  width: 20px; /* image width */
  height: 100px; /* image height */
  position: absolute;
  top: 0;
  right: 0;
}

答案 1 :(得分:0)

将图像放在锚点的背景上是行不通的,因为图像会隐藏它。

您可以在锚点内的absolute position中添加包含星星的元素:

<a href="http://costumingdiary.blogspot.com/2013/01/victorian-tardis-purse.html">
    <img src="https://lh3.goog...An-purse-pattern-1.jpg" alt="Free.. Pattern">
    <br>
    Free Victorian Purse Pattern
    <i class="stars"></i>
</a>

和类似的CSS(所有数字都可以变化):

a { 
    position: relative;
}

.stars {
    background-image:url('http://i.imgur.com/IKAXZKz.png');
    background-position:inherit;
    width: 20px;
    height: 80px;
    position: absolute;
    right: 10px;
    top: 10px;
    display: none;
}

a:hover .stars {
    display: block;
}

P.S - <br />应该是自我关闭的。并且最好不要仅仅使用它在图像后面创建一个新行,但可能会给图像带来这种风格:

a img { 
   display: block;
}

答案 2 :(得分:0)

这是未经测试的,但我认为你想要这样的东西:

.gallerypagetabs a, .gallerypagetabs img{
     position:absolute;
     z-index:1;
 }

 .gallerypagetabs a:hover{
     z-index:2;
 }

 .gallerypagetabs{
     position:relative;
 }

这是未经测试的,但希望能让您了解如何实现您所追求的效果。