css - 元素 :hover - text and background

标签 css

我需要在鼠标悬停时在图像上显示文本,并且我还需要它来影响不透明度。

看看这支笔 http://codepen.io/anon/pen/NNBgbQ

 <div class="flex-menu">
 <div class="menu-container">
 <img class="menu-image" src="http://www.libbyroach.ca/wp-content/uploads/2014/11/Adams-Sandwich.jpg" alt="Sandwitch">
 <div class="menu-description">

 <h4>Sandwitch</h4>
 <p>Powder marshmallow marshmallow brownie carrot cake candy bonbon. Sweet sugar plum gummies caramels tart carrot cake tiramisu cheesecake. Cheesecake biscuit jelly beans. Jelly-o icing chocolate macaroon. </p>
 </div>
 </div>
 </div>

我要找的结果:

将鼠标悬停在图像上,图像会改变其不透明度,文本会出现在图像的中间 - 任何文本,不一定是当前标题和段落。

最佳答案

开始使用 'position:abolsute 定位文本 div 以覆盖图像。

.menu-container div 获取 position:relative 以提供定位上下文。

然后将 :hover 触发器切换到包装器以同时触发两者。

.flex-menu,
.menu-container,
.menu-image {
  width: 500px;
}
.menu-container {
  position: relative;
}
.menu-description {
  display: none;
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
}
.flex-menu {
  background-color: #fd5c63;
}
.menu-image {
  transition: all 0.3s ease 0s;
  display: block;
}
.flex-menu:hover .menu-image {
  opacity: 0.2;
}
.flex-menu:hover .menu-description {
  display: block;
}
<div class="flex-menu">
  <div class="menu-container">
    <img class="menu-image" src="http://www.libbyroach.ca/wp-content/uploads/2014/11/Adams-Sandwich.jpg" alt="Sandwitch">
    <div class="menu-description">

      <h4>Sandwitch</h4>
      <p>Powder marshmallow marshmallow brownie carrot cake candy bonbon. Sweet sugar plum gummies caramels tart carrot cake tiramisu cheesecake. Cheesecake biscuit jelly beans. Jelly-o icing chocolate macaroon.</p>
    </div>
  </div>
</div>

关于css - 元素 :hover - text and background,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36792787/

相关文章:

html - CSS 列布局 - 具有动态宽度和与同级相同高度的 DIV

php - 显示来自特定 WordPress 类别的精选帖子

html - 无法摆脱顶部和底部的阴影

css - DataTables sScrollY + 空白 :nowrap; broken in Chrome 30. 0.1599.69 m

javascript - jQuery:使用 show() 显示父节点的一些节点

javascript - 添加图像并允许编辑

javascript - 如何在JavaScript中使用不同的ID动态创建CSS规则

css - 如何在 CSS 中制作 gmail 样式的配置文件菜单?

javascript - 如何使用 javascript 为每个单词设置不同颜色的样式?

html - 反转alpha png图片