【问题标题】:How to make both text and background color highlight on hover?如何在悬停时同时突出显示文本和背景颜色?
【发布时间】:2012-12-21 17:09:40
【问题描述】:

我正在尝试创建一个网格样式的投资组合页面,其中标题在悬停时突出显示。我还希望在图像上显示背景颜色。我已经创建了这两个,但无法弄清楚如何让它们同时发生。下面是 HTML 和 CSS 以及查看我的实际意思的链接。

.home_blog_box { position: relative; float: left; width: 287px; padding: 0 12px 12px 0; }
.home_blog_box img { width: 287px; height: 201px;  }
.home_blog_box h3 { position:absolute; width: 287px; height: 201px; bottom:3px; left:0px;}
.home_blog_box h3:hover {background-color: #777777; opacity:.85;}
.home_blog_box h3 a {opacity:0; color:#ffffff; text-decoration: none; font-size:30px;}
.home_blog_box h3 a:hover {opacity:1}


<div class="home_blog_box">
<?php } ?>
  <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('featured-blog'); ?></a>
    <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
</div><!--//home_blog_box-->  

谢谢!

【问题讨论】:

标签: html css wordpress hover background-color


【解决方案1】:

您可以使用 :after 或 :before 伪元素很好地做到这一点:http://jsfiddle.net/TpfA8/

示例:

<div class="image-box">
  <img src="http://placehold.it/200x150" />
  <p>title</p>
</div>

和相关的css:

.image-box {
  position: relative;
  overflow: hidden;
  width: 200px;
}

.image-box:hover:after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(200,100,100, 0.5);
}

.image-box p {
  display: none;
}

.image-box:hover p {
 display: block;
  position: absolute;
  top: 10px;
  left; 10px;
}

使用 CSS 生成的内容,您正在围绕 '' 的内容(无)创建一个框,但该元素仍然遵循框模型(有点),因此它可以获取诸如高度、宽度、位置、等等

确保为您的图像容器指定宽度,以便您可以使用溢出:隐藏;属性。

【讨论】:

  • 这很有帮助,但我正在努力将其翻译为同时将图像和文本作为帖子的链接。有什么建议?同样,当我添加 .image-box p {display:none;} 或在我的情况下 .home_blog_box a {display: none;} 它会使所有图像消失。有了它,突出显示框会显示在图像上,但没有标题。而且都不是链接。
  • 您可以将 p 标题更改为锚点。 jsfiddle.net/TpfA8/4 然后使用 :before 而不是 :after 以便位置正确发生。另外,请记住,并非每个浏览器都支持 :before 和 :after ,因此如果悬停 + 标题不起作用,请将图像包装在锚点中以备后备。您还可以结合 CSS 类进行一些现代特征检测,但这是一个不同的问题和答案:)
猜你喜欢
  • 2017-06-28
  • 2021-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-09
  • 2021-03-18
  • 2015-08-18
  • 2011-01-27
相关资源
最近更新 更多