2017-08-09 78 views
0

我还没有找到答案。我有一个div的图像,随着:悬停。我想叠加一些在悬停期间是静态的文本,并且不会干扰锚标记。我也需要这种设计来响应,以便文本始终位于图像的中间。我试过Display:Table and 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> 

谢谢。

回答

1

添加

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

(把它当前的选择中,当然)。顶部/左侧50%将其移到与父母相关的位置;翻译将它与其自身相关联,并将其集中。

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