I don’t even know if this is possible using only CSS, but I have to ask.
PLEASE read the specifications before providing an answer, respectively. Thanks!
My markup:
<div class="wrapper">
<img src="http://placehold.it/350x150">
<p>Some text random size</p>
</div>
Obviously .wrapper will have the height of my img element if it is a block. Then I need the p element to be horizontally and vertically centered inside the wrapper. I do not have a fixed width or height for the p element.
So, regardless of the size of the paragraph or even the size of the image, it should be aligned vertically and horizontally, as shown here http://i.imgur.com/phiR48H.png or here http://i.imgur.com/Xvdt42j .png .
If I set the absolute position in the paragraph, it will not be aligned vertically because I cannot set a negative margin if I do not know the height of the paragraph. I was thinking about a table and a table-cell (vertical-align: middle;), but I only have 1 cell. Any thoughts?
Added fiddle: http://jsfiddle.net/f3x7977z/
It is important that the solution provided is backward compatible, especially in IE8 + .
Any suggestions for additional packers, for the sake of the final result, are welcome!
source
share