I have a row of four images displayed next to each other, and I want to show text beneath each image when hovered over. The text should remain hidden until hover and should not affect the content’s width. However, when I position the text absolutely, I struggle to center it properly under each image. Any idea how to solve it?
Snippet of my current structure:
<div class"items">
<div class="item-single">
<a href="">
<img src="">
</a>
<p>Long text under image</p>
</div>
</div>
If I set item-single
to position:absolute;
, I cannot center text properly under each respective image. Setting item-single
to position:relative;
makes the text fit inside the div, which can cause unwanted multi-line text.
3
Answers
I added the following to the p element, and it seems to do the job. The p element is absolute, while its parent is relative.
You need to understand how the position relative and absolute work together.
By adding
margin: auto;
along with position absolute should solve your problem.This should help.