html - 响应式设计 : Float text over image

标签 html css hover responsive floating

我还没有找到这个问题的答案。我有一个带有随 :hover 增长的图像的 div。我想在悬停期间覆盖一些静态的文本,并且不会干扰 anchor 标记。我还需要这种设计具有响应性,以便文本始终位于图像的中间。我已经尝试过 Display:Table 和 Table-cell...我的 CSS 中有些部分与我尝试的所有内容都不一致。

我已经能够将文本放置在绝对 div 中,但我不想指定文本的确切像素位置,因为这不是响应式的。

<style>
#AOB{
height: 700px;
position: relative;
padding: 30px 200px 0px;
}


.innerBlock {
float: left;
display:block;
position: relative;
}

#AOB .innerBlock {
width: 33.2%;
height: 50%;
overflow:hidden;
}

#AOB img{
height:100%;
width:  100%;

}

#AOB img:hover{
-webkit-transform:scale(1.05);
-ms-transform:scale(1.05);
transform:scale(1.05);
transition: all 0.2s ease;
}


.innerBlockText{
position:absolute;
top: 100px;
left: 100px;
z-index:1;
}



</style>

<div id="AOB">
  <div id="one" class="innerBlock">
    <a href = "http://www.google.com">
     <div class="innerBlockText">
     beach
     </div>
     <img  src="https://www.weebly.com/editor/uploads/2/1/6/7/21673280/custom_themes/537557073923231080/files/images/beach.png">
    </a> 
  </div>

  <div id="two" class="innerBlock">
    <a href = "http://www.google.com">
     <img src="https://www.weebly.com/editor/uploads/2/1/6/7/21673280/custom_themes/537557073923231080/files/images/beach.png">
    </a>
  </div>

  <div id="three" class="innerBlock">
    <a href = "http://www.google.com">
     <img src="https://www.weebly.com/editor/uploads/2/1/6/7/21673280/custom_themes/537557073923231080/files/images/beach.png">
    </a>
  </div>

  <div id="four" class="innerBlock">
    <a href = "http://www.google.com">
    <img src="https://www.weebly.com/editor/uploads/2/1/6/7/21673280/custom_themes/537557073923231080/files/images/beach.png">
    </a>
  </div>

  <div id="five" class="innerBlock">
    <a href = "http://www.google.com">
     <img src="https://www.weebly.com/editor/uploads/2/1/6/7/21673280/custom_themes/537557073923231080/files/images/beach.png">
    </a>
  </div>

  <div id="six" class="innerBlock">
    <a href = "http://www.google.com">
     <img src="https://www.weebly.com/editor/uploads/2/1/6/7/21673280/custom_themes/537557073923231080/files/images/beach.png">
    </a>
  </div>



</div>

谢谢。

最佳答案

添加

.innerBlockText {
    top: 50%;
    transform: translate(-50%, -50%);
    left: 50%;
}

(当然是把它放在你当前的选择器中)。顶部/左侧 50% mm 将其移动到相对于父级的那些位置; translate 相对于自身移动它,使其居中。

https://jsfiddle.net/9Lvmjt5g/4/

关于html - 响应式设计 : Float text over image,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45580207/

相关文章:

css - 聊天 HTML 页面建议

javascript - animate() 滚动不适用于 href

html - 如何使用 CSS 设置 HTML 段落的样式以防止它们之间出现空行?

html - 奇数表行不突出显示

html - 悬停图片效果

html - 为什么当 `td` 写入两个 div 而一个设置为 `position: absolute` 和另一个 `position: relative` 时我不能在 td 中设置宽度?

html - CSS 在提交之前不会对齐 Div

javascript - 将类添加到已经具有相同类的元素的问题

javascript - 如何在 Twig 内切换类(如果有条件)

html - 悬停时下拉菜单消失